这是我的没有功能分数的查询:
{
"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
}
}
}
}
}
}
此查询将在约 250 毫秒内返回响应。
但我需要添加一些提升因子来提高默认得分。我修改了查询以使用函数得分,但之后查询花费了太长时间(~3000ms)
这是分数查询函数:
{
"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 脚本如下:
def penalize = 1
def penalizeClassDict = [
'226': 0.25,
'14106': 0.25,
'656': 0.25
]
for (item in _source.cotypes)
{
if(penalizeClassDict.containsKey(item.id.toString()))
penalize = penalize * penalizeClassDict[ item.id.toString()]
}
_score + (pow(_source.relevance, relevance_boost)) * 1
请帮助我使查询执行得更好!
先感谢您!