We have a specific use-case for our ElasticSearch instance: we store documents which contain proper names, dates of birth, addresses, ID numbers, and other related info.
We use a name-matching plugin which overrides the default scoring of ES and assigns a relevancy score between 0 and 1 based on how closely the name matches.
What we need to do is boost that score by a certain amount if other fields match. I have started to read up on ES scripting to achieve this. I need assistance on the script part of the query. Right now, our query looks like this:
{
"size":100,
"query":{
"bool":{
"should":[
{"match":{"Name":"John Smith"}}
]
}
},
"rescore":{
"window_size":100,
"query":{
"rescore_query":{
"function_score":{
"doc_score":{
"fields":{
"Name":{"query_value":"John Smith"},
"DOB":{
"function":{
"function_score":{
"script_score":{
"script":{
"lang":"painless",
"params":{
"query_value":"01-01-1999"
},
"inline":"if **<HERE'S WHERE I NEED ASSISTANCE>**"
}
}
}
}
}
}
}
}
},
"query_weight":0.0,
"rescore_query_weight":1.0
}
}
The Name field will always be required in a query and is the basis for the score, which is returned in the default _score field; for ease of demonstration, we'll just add one additional field, DOB, which if matched, should boost the score by 0.1. I believe I'm looking for something along the lines of if(query_value == doc['DOB'].value add 0.1 to _score), or something along these lines.
So, what would be the correct syntax to be entered into the inline row to achieve this? Or, if the query requires other syntax revision, please advise.
EDIT #1 - it's important to highlight that our DOB field is a text field, not a date field.
window_sizeresults - are you sure this is acceptible for your use case? It SOUNDS like you're trying to modify relevance based on presence of other fields, so I'd think you'd want to do that across the entire search space instead of just the top results from your original scoring. (2) I don't think you need a script here, as you should just be able to use a list offilterfunctions instead ofscript_scorefunctions that apply a static boost if documents match some criteria.namefield: we want to bring back the most relevantnamematches via the base query, then use the rescore query to check those results for additional fields. FYI, we first tried to solve this usingfunction_scoreanddoc_scoreonly and using theweightparameter. The problem with that is that if theDOBdid NOT match, it REDUCED the score. We don't want this.doc_score(unable to find that documented), I do think I have a solution that doesn't require scripting and gets your desired behavior. Effectively, you can use a bool query for yourfunction_scorequery thatshouldall your secondary criteria together, then use individualweightfunctions for each criterium to set how much to add to the score for matches. I'll share a complete answerdoc_scoreis proprietary to the name-matching plugin we are using. It's not a well-documented plugin hence your inability to find anything about it. It is probably irrelevant to our discussion in any case. I look forward to your solution. If theweightfunctions do not also REDUCE the score if the additional field doesn't match, then it will work for me. Any tinkering I did withweightalso reduced the score when the field did not match, which we don't want - we want to boost only. Thanks again.