Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Script Score Queries Not getting Cached for K-NN painless Script for Exact Search #1098

Closed
navneet1v opened this issue Sep 6, 2023 · 1 comment · Fixed by #1367
Closed
Assignees
Labels
bug Something isn't working v2.12.0

Comments

@navneet1v
Copy link
Collaborator

What is the bug?
While trying to do the exact search via Script Score queries as suggested in this documentation: https://opensearch.org/docs/latest/search-plugins/knn/knn-score-script/ what I am observing is even after sending the search request with request_cache=true url param in the request the requests are not getting cached.

How can one reproduce the bug?
Steps to reproduce the behavior:

  1. Use the provided documentation to create a k-nn index and ingest the data in the index.
  2. Now, run the exact search query like, multiple times:
curl --request POST \
  --url 'http://localhost:9200/my-knn-index-1/_search?request_cache=true' \
  --header 'Content-Type: application/json' \
  --data '{
 "query": {
   "script_score": {
     "query": {
       "match_all": {}
     },
     "script": {
       "source": "knn_score",
       "lang": "knn",
       "params": {
         "field": "my_vector2",
         "query_value": [2.0, 3.0, 5.0, 6.0],
         "space_type": "cosinesimil"
       }
     }
   }
 }
}'
  1. Now hit the index stats api to see that request are not getting cached in the request_cache object.
curl --request GET \
  --url http://localhost:9200/my-knn-index-1/_stats

What is the expected behavior?
The expected behavior is if the request_cache parameter is passed the request should be cached. I tested this behavior with another painless script query

curl --request POST \
  --url 'http://localhost:9200/my-knn-index-1/_search?request_cache=true' \
  --header 'Content-Type: application/json' \
  --data '{
  "query": {
    "script_score": {
      "query": {
        "match": { "message": "opensearch" }
      },
      "script": {
        "source": "doc['\''price'\''].value / 10 "
      }
    }
  }
}'

and I can see the requests are getting cached.

What is your host/environment?
Happening on all envs.

Do you have any screenshots?
NA

Do you have any additional context?
On doing some deep-dive in the code I can see that the issue request is getting marked as false from this function: https://github.com/opensearch-project/OpenSearch/blob/aca2e9de7daaadee77c33e21ff5215c3f1e8600f/server/src/main/java/org/opensearch/index/query/QueryShardContext.java#L493-L499 and this

https://github.com/opensearch-project/OpenSearch/blob/aca2e9de7daaadee77c33e21ff5215c3f1e8600f/server/src/main/java/org/opensearch/index/query/QueryShardContext.java#L518-L524

@navneet1v navneet1v added bug Something isn't working untriaged and removed untriaged labels Sep 6, 2023
@vamshin vamshin moved this from Backlog to Backlog (Hot) in Vector Search RoadMap Sep 7, 2023
@vamshin vamshin added the good first issue Good for newcomers label Sep 15, 2023
@vamshin vamshin moved this from Backlog (Hot) to 2.12.0 in Vector Search RoadMap Oct 5, 2023
@vamshin vamshin removed their assignment Dec 4, 2023
@vamshin vamshin added v2.12.0 and removed good first issue Good for newcomers labels Dec 4, 2023
@junqiu-lei
Copy link
Member

In OS core level script compile, the isResultDeterministic from XScoreScriptFactory(Interface ScriptFactory) need to be true to perform expected query cache, or else it won't perform cache even if set request_cache=true.

However, in existing KNNScoringScriptEngine, we directly use KNNScoreScriptFactory(leafFactory) to new the factory, it implements from ScoreScript.LeafFactory, which doesn't contain method isResultDeterministic, so finally the kNNScriptFactory in OS core just use interface level isResultDeterministic method which by default return false.

I found a example of ExpertScriptPlugin, which factory implemented from ScoreScript.Factory and ScriptFactory interface and make isResultDeterministic method to returntrue to perform the right search cache. The leafFactory is newed by newFactory method.

So as reference from ExpertScriptPlugin on how it implements the script factory, I made the code change to k-NN and tested it works as expected finally. PR #1367 is raised to fix the issue.

@github-project-automation github-project-automation bot moved this from 2.12.0 to ✅ Done in Vector Search RoadMap Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v2.12.0
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants