Last active
February 5, 2023 17:05
-
-
Save peterdm/9e31be343a733c44ff0e to your computer and use it in GitHub Desktop.
Elasticsearch semi-equivalent of Solr "bf=product(query('cat'),query('dog'))"
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
GET /my_index/my_type/_search | |
{ | |
"query": { | |
"function_score": { | |
"query": { <ORIGINAL QUERY> }, | |
"functions": [ | |
{ | |
"filter" : { | |
"term" : { "_all" : "cat"} | |
}, | |
"script_score": { | |
"params": { | |
"field": "_all", | |
"term": "cat" | |
}, | |
"script": "tf = _index[field][term].tf(); idf = (1 + log ( _index.numDocs() / (_index[field][term].df() + 1))); return sqrt(tf) * pow(idf,2)" | |
} | |
}, | |
{ | |
"filter" : { | |
"term" : { "_all" : "dog"} | |
}, | |
"script_score": { | |
"params": { | |
"field": "_all", | |
"term": "dog" | |
}, | |
"script": "tf = _index[field][term].tf(); idf = (1 + log ( _index.numDocs() / (_index[field][term].df() + 1))); return sqrt(tf) * pow(idf,2)" | |
} | |
} | |
], | |
"score_mode": "multiply", | |
"boost_mode": "sum" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"score_mode": "multiply"
will take the product of all the functions defined in the"functions": [...]
block and then"boost_mode": "sum"
will add that to the score of the<ORIGINAL QUERY>
bf=...
in Solr also adds the function result to the original query score.Solr's
query()
will also perform field-length normalization and query normalization which this will not.(see Lucene's Practical Scoring Function)
(Elasticsearch doesn't seem to provide scripting access to the field-length encoding provided in
norms
).The scoring behavior here should be similar to the OP's Solr query if
omitNorms=true
were specified on the fields searched.