haystack icon indicating copy to clipboard operation
haystack copied to clipboard

Changing Elastic Search Docstore refresh_type=false with Embedding Retriever, resulting in error during query time

Open monika1800 opened this issue 5 years ago • 16 comments

Describe the bug Unable to run query_by_embedding after indexing the documents with Elastic Search Docstore refresh_type=false with Embedding Retriever.

Error message A document doesn't have a value for a vector field.

{"error":{"root_cause":[{"type":"script_exception","reason":"runtime error","script_stack":["org.elasticsearch.xpack.vectors.query.ScoreScriptUtils$DenseVectorFunction.getEncodedVector(ScoreScriptUtils.java:100)","org.elasticsearch.xpack.vectors.query.ScoreScriptUtils$CosineSimilarity.cosineSimilarity(ScoreScriptUtils.java:179)","cosineSimilarity(params.query_vector,'embedding') + 1000"," ^---- HERE"],"script":"cosineSimilarity(params.query_vector,'embedding') + 1000","lang":"painless","position":{"offset":37,"start":0,"end":56}}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"1july_test4","node":"j7lOBAVgT625FixOyow97Q","reason":{"type":"script_exception","reason":"runtime error","script_stack":["org.elasticsearch.xpack.vectors.query.ScoreScriptUtils$DenseVectorFunction.getEncodedVector(ScoreScriptUtils.java:100)","org.elasticsearch.xpack.vectors.query.ScoreScriptUtils$CosineSimilarity.cosineSimilarity(ScoreScriptUtils.java:179)","cosineSimilarity(params.query_vector,'embedding') + 1000"," ^---- HERE"],"script":"cosineSimilarity(params.query_vector,'embedding') + 1000","lang":"painless","position":{"offset":37,"start":0,"end":56},"caused_by":{"type":"illegal_argument_exception","reason":"A document doesn't have a value for a vector field!"}}}]},"status":400}

raise HTTP_EXCEPTIONS.get(status_code, TransportError)( elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'runtime error')

monika1800 avatar Jul 01 '21 13:07 monika1800

Hey, when i tried i was able to make it work. In the above case it looks like embedding field was not indexed in elastic. Was update embedding called ? When I tried with independent colab i was able to make it work with refresh_type='False'.

Threepointone4 avatar Jul 02 '21 04:07 Threepointone4

Or one of the document vector generation failed maybe, Can you see whether all records have embedding. Maybe that is also one possibility. Maybe indexing 1 document and seeing whether it got embedding or not and testing might help

Threepointone4 avatar Jul 02 '21 04:07 Threepointone4

https://colab.research.google.com/drive/1pom2rA1tqhhsrJL5EZRnPOnHyaABd8-Z?usp=sharing

I have done a small setup for this, let me know if some changes required in this.

Threepointone4 avatar Jul 02 '21 04:07 Threepointone4

@tholor when refresh_type is false ( during soft commit ) . document gets indexed in background, inside update embedding , when get all document is called , only document which were indexed at that time will be updated. This understanding is correct right ?

So should there be a feature which accounts refresh_type and calls update embedding after some data gets indexed or once all data is done.

Threepointone4 avatar Jul 04 '21 07:07 Threepointone4

I could see two things that might cause the problem here due to the chosen refresh_type:

  1. update_embeddings() is called before write_documents() has finished the commit in elasticsearch => update_embeddings could miss out on a document and not create an embedding at all for it
  2. query_by_embedding is called before update_embeddings has finished updating all docs in elasticsearch => query fails because there are docs without a vector

@monika1800 Can you please verify which of those two cases is happening in your situation? If it is 2) the error should disappear when you wait for a few seconds and then just run query_by_embedding again. If it is 1), adding a sleep command of ~ 10 sec between write_documents() and update_embeddings() should resolve your problem.

We can then see if we want to automatically resolve this issue (e.g. by calling the refresh API of elasticsearch https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html - but I am not really sure at this point if it's really worth the effort)

tholor avatar Jul 06 '21 13:07 tholor

Hi @tholor,

Thanks for giving more understanding on this issue.

In the first case, I shall experiment with adding a sleep command of 10secs between write_documents() and update_embeddings() and report the findings here.

In the second scenario, I tried calling query_by_embedding after 24 hours but it still gave the same error.

monika1800 avatar Jul 06 '21 14:07 monika1800

Thanks @tholor. Adding 10sec delay between write_documents() and update_embeddings() has made it work as expected for 1k document.

monika1800 avatar Jul 06 '21 14:07 monika1800

Ok nice, then we isolated the root cause. I guess you could even decrease this sleep to 1 sec as this is usually the default refresh interval of elastic.

Is there any particular reason why you use refresh_type=false?

If we want to fix this for future users, we could probably add a mandatory call to elastic's refresh API in the very start of running update_embeddings(). Is any of you interested in opening a PR for this @monika1800 @Threepointone4 ?

tholor avatar Jul 06 '21 14:07 tholor

@tholor I wanted to experiment with refresh_type = False, to reduce the response time while indexing a large set of content on Elastic Search document Store.

Based on the findings refresh_type=wait_for was looking quite expensive.

These were the findings,

with 1k (100kb) data, it was able to finish indexing & update embeddings in 30 sec.

But when I tried with 10k (5MB) and 50k (11MB) data, the response time was 720sec and 1500sec respectively.

monika1800 avatar Jul 06 '21 15:07 monika1800

Hey @tholor we should do something like this right ? image

Threepointone4 avatar Jul 06 '21 17:07 Threepointone4

Yep, I believe that should work (haven't checked elastic's refresh API though). Maybe @monika1800 can confirm that this fix works as intended and has the same outcome as the artificial sleep command?

tholor avatar Jul 06 '21 18:07 tholor

@Threepointone4 @tholor I think it can be added in write_documents function.

Just wondering what in case update_embeddings, write_labels, update_document_meta is called. This may need to be refreshed at these places as well?

monika1800 avatar Jul 07 '21 09:07 monika1800

I was able to reproduce the issue and the solution as well. when 20k records are indexed and called update embedding right after. image As you can see only 15k around records are sent

With the solution i have added , image 20k was reflecting during update embedding. image 200k results. also

Threepointone4 avatar Jul 07 '21 13:07 Threepointone4

Seems a bit weird: in your first screenshot there are 154k docs (not 15k). Not sure if this is really reproducing the issue here :thinking:. Maybe you had other docs in your document store before calling write_documents() with the 20k?

tholor avatar Jul 07 '21 15:07 tholor

@tholor Sorry that was mistake from my side, I didn't call delete document on that run. I have tried again and here are the results, adding colab also for reference image

https://colab.research.google.com/drive/1pom2rA1tqhhsrJL5EZRnPOnHyaABd8-Z?usp=sharing#scrollTo=jUQ1mqD9CKAi

Let me know if you see some issues. Thanks.

Threepointone4 avatar Jul 07 '21 16:07 Threepointone4

Fixed in #1259

julian-risch avatar Oct 13 '21 09:10 julian-risch