I am trying to replicate the following curl
commando in elasticsearch-py
:
curl -XGET "localhost:9200/my-exact-knn-index/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"script_score": {
"query" : {
"bool" : {
"filter" : {
"range" : {
"my-price" : {
"gte": 0
}
}
}
}
},
"script": {
"source": "\n double value = dotProduct(params.query_vector, 'my-product-vector');\n return sigmoid(1, Math.E, -value); \n ",
"params": {
"query_vector": [-0.5, 90.0, -10, 14.8, -156.0]
}
}
}
}
}
But I am having trouble understanding how to provide the "script" part to the index.search()
call. I tried providing it in the query
parameter, and in the body
parameter (which is deprecated now), but it gives me a BadRequestError
:
query_body = {
"script_score": {
"script": {
"source": "\n double value = dotProduct(params.query_vector, '\''my-product-vector'\'');\n return sigmoid(1, Math.E, -value); \n ",
"params": {
"query_vector": [-0.5, 90.0, -10, 14.8, -156.0]
}
}
}
}
search_results = client.search(index="my-exact-knn-index", query=query_body)
>>> BadRequestError: BadRequestError(400, 'illegal_argument_exception', 'Required [query]')
Any idea how to get this done?
CodePudding user response:
I got it working by doing the following:
query_body = {
"script_score": {
"query" : {
"bool" : {
"filter" : {
"range" : {
"my-price" : {
"gte": 0
}
}
}
}
},
"script": {
"source": "\n double value = dotProduct(params.query_vector, 'my-product-vector');\n return sigmoid(1, Math.E, -value); \n ",
"params": {
"query_vector": [-0.5, 90.0, -10, 14.8, -156.0]
}
}
}
}
search_results = client.search(index="my-exact-knn-index", query=query_body)
So the query
had to come after the script_score
.
CodePudding user response:
You're almost there, you simply need a query
section:
query_body = {
"script_score": {
"query": { <----- add this
....
},
"script": {
"source": "\n double value = dotProduct(params.query_vector, '\''my-product-vector'\'');\n return sigmoid(1, Math.E, -value); \n ",
"params": {
"query_vector": [-0.5, 90.0, -10, 14.8, -156.0]
}
}
}
}
}