Here is my query without function score:
{
"from": 200,
"size": 25,
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"nested": {
"query": {
"terms": {
"cotypes.id": [
199
]
}
},
"path": "cotypes"
}
},
{
"range": {
"relevance": {
"from": 6,
"to": null,
"include_lower": true,
"include_upper": true
}
}
}
],
"must_not": {
"terms": {
"ontologyId": [
1314696,
1314691
]
}
}
}
},
"must": {
"match": {
"name.nameStandard": {
"query": "john smith",
"type": "boolean",
"boost": 10
}
}
}
}
}
}
This query return the response in ~250ms.
But I need to add some boost factor for improve the default scoring. I modified the query to use the function score, but after that query taking too long (~1300ms)
Here is the function score query:
{
"from": 200,
"size": 25,
"query": {
"function_score": {
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"nested": {
"query": {
"terms": {
"cotypes.id": [
199
]
}
},
"path": "cotypes"
}
},
{
"range": {
"relevance": {
"from": 6,
"to": null,
"include_lower": true,
"include_upper": true
}
}
}
],
"must_not": {
"terms": {
"ontologyId": [
1314696,
1314691
]
}
}
}
},
"must": {
"match": {
"name.nameStandard": {
"query": "john smith",
"type": "boolean",
"boost": 10
}
}
}
}
},
"functions": [
{
"script_score": {
"script": {
"file": "calculate-score",
"lang": "groovy",
"params": {
"relevance_boost": 0.5
}
}
}
}
],
"boost_mode": "sum"
}
}
}
calculate-score.groovy script given below:
def penalize = 1
def penalizeClassDict = [
'226': 0.25,
'14106': 0.25,
'656': 0.25
]
for (item in doc['classIds'].values)
{
if(penalizeClassDict.containsKey(item.toString()))
penalize = penalize * penalizeClassDict[ item.toString()]
}
_score + (pow(doc['relevance'].value, relevance_boost)) * penalize
Please help me to make the query perform better!
Thank you in advance!