Skip to content

Instantly share code, notes, and snippets.

@peterdm
Last active February 5, 2023 17:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterdm/9e31be343a733c44ff0e to your computer and use it in GitHub Desktop.
Save peterdm/9e31be343a733c44ff0e to your computer and use it in GitHub Desktop.
Elasticsearch semi-equivalent of Solr "bf=product(query('cat'),query('dog'))"
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"
}
}
}
@peterdm
Copy link
Author

peterdm commented Aug 3, 2015

"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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment