haystack icon indicating copy to clipboard operation
haystack copied to clipboard

MostSimilarDocumentsPipeline -Functionality to get top n documents by document Id rather than a query

Open monika1800 opened this issue 5 years ago • 8 comments

What

Is there an existing functionality to get top n documents by document Id rather than a query or is it possible to combine existing functionality to achieve this?

monika1800 avatar May 25 '21 07:05 monika1800

Hey @monika1800

If you just want to get documents I would not use our query functionality of the retrievers. Retrievers always want to combine a search query to retrieve relevant documents.

What you are looking for is the inside our document store directly. We have document_store.get_document_by_id() and document_store.get_all_documents().

I guess you are splitting up your documents and want the individual part when referring to "document_id". This you should be able to achieve with:

document_store.get_all_documents(filters={"name": ["example_doc1.txt"]}

Of course when inserting the documents into the document store you need to add the "name" as a metainformation. If you dont know how to do this please add the code how you convert/load and insert the docs, so I can help better.

Timoeller avatar May 27 '21 09:05 Timoeller

@monika1800 Let us know if the above comment already resolved your issue or if you are maybe rather looking for a way to find the n most similar document for a given document with id ABC. That would be a slightly different use case, but definitely achievable with a retriever (e.g. EmbeddingRetriever).

tholor avatar May 28 '21 19:05 tholor

Thanks @Timoeller.

@tholor Yes my use case was to find the n most similar document for a given document with id ABC.

I was able to achieve this with get_all_documents filtering for a given Id embedding using query_by_embedding.

monika1800 avatar Jun 01 '21 09:06 monika1800

Nice.

Would you like to share the code that you used here on github so others can find it? I think this is a pretty common use case, so people will be interested in your solution. Can we close the issue then as well?

Timoeller avatar Jun 01 '21 11:06 Timoeller

get_docs_params = cfg.get('get_docs_params', {
        		"filters": {"Id": ["a2W3l000001Q6pmEAC"]}, #multiple filters can be added, here I am providing the document Id to fetch the relevant document embedding.
        		"return_embedding": true, #This is set to true to get embedding for documents
        		"index": "sim_es2"
        	})
query_by_embed_params = cfg.get('query_by_embed_params', {"return_embedding": false,
        	"top_k": 2, #we can give top n number of documents to fetch the similar documents from the docstore
        	"index": "sim_es2"})

docs_gad = docstore.get_all_documents(**get_docs_params) #this function will return the data for the list of records given in filters

for d in docs_gad:
    query_emb = d.embedding #to get embedding of each document
    query_by_embed_params.update({"query_emb": query_emb})
    results = docstore.query_by_embedding(**query_by_embed_params)
    result_list.append(results) 
   
#results are in the form of [[],[],[]], where each list contains the similar documents for eahc of the id

monika1800 avatar Jun 02 '21 09:06 monika1800

@Timoeller I have added the relevant piece of code. Thank you.

monika1800 avatar Jun 09 '21 10:06 monika1800

Thanks for sharing! I will keep this issue open, as I think we should simplify this in Haystack with something like a "MostSimilarDocumentsPipeline" that gets IDs as input and returns for each ID the most similar top_k docs.

tholor avatar Jun 09 '21 11:06 tholor

@tholor Assign.

akkefa avatar Jul 15 '21 11:07 akkefa