Created
February 22, 2022 01:03
-
-
Save manisnesan/ec1c3c6ec204841d878239c2857a18ca to your computer and use it in GitHub Desktop.
Altering results using scripting and function scores
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Altering resuts using painless - https://www.elastic.co/guide/en/elasticsearch/painless/7.10/painless-walkthrough.html | |
### Script score on price for doc 1 | |
### In these results, doc_a gets a score of price (5.99) + 1 (due to the match_all score) = 6.99. All else gets a score of 1 (match_all score) + 1 (the non “doc_a” case in the script) = 2. | |
POST searchml_test/_search | |
{ | |
"query": {"match_all": {}}, | |
"rescore": { | |
"query": { | |
"rescore_query": { | |
"function_score": { "script_score": { | |
"script": { | |
"source": """ | |
if (doc['id.keyword'].value == "doc_a") {return doc.price.value} | |
return 1; | |
""" | |
} | |
}} | |
}, | |
"query_weight": 1.0, | |
"rescore_query_weight": 1.0 | |
}, | |
"window_size": 10 | |
} | |
} | |
### when it comes to recency, not all amounts of time are the same. Generally speaking, content from the last hour is better than the last day, which is better than the last week, which is better than the last month and so on. In other words, recency isn’t linear in the amount of time. | |
### decay functions - https://www.elastic.co/guide/en/elasticsearch/reference/7.10/query-dsl-function-score-query.html#function-decay | |
### Decaying differently for low cost vs high cost | |
### exponential decay function anchored at an origin (zero for cheap, 150 for “expensive”), and then use an exponential decay as you get further away | |
# Boost low cost items | |
POST searchml_test/_search | |
{ | |
"query": {"match_all": {}}, | |
"rescore": { | |
"query": { | |
"rescore_query": { | |
"function_score": {"functions": [ | |
{"exp": { | |
"price": {"origin": "0", "scale": "4", "decay": "0.3"} | |
}} | |
]} | |
}, | |
"query_weight": 1.0, | |
"rescore_query_weight": 1.0 | |
}, | |
"window_size": 10 | |
} | |
} | |
# Boost high cost items | |
POST searchml_test/_search | |
{ | |
"query": {"match_all": {}}, | |
"rescore": { | |
"query": { | |
"rescore_query": { | |
"function_score": {"functions": [ | |
{"exp": { | |
"price": {"origin": "150", "scale": "4", "decay": "0.3"} | |
}} | |
]} | |
}, | |
"query_weight": 1.0, | |
"rescore_query_weight": 1.0 | |
}, | |
"window_size": 10 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment