使用function_score進行分組處理,利用分組函數script_score進行自定義分值處理,
注意:使用script功能需要在配置中打開腳本功能:
script.inline: on
script.indexed: on
script.engine.groovy.inline.aggs: on
script.file: on
下圖中的查詢表示,查詢termid為1238218的記錄,或者displayname包含“寶寶”的記錄;
termid為1238218的記錄排序分人為設定為100,displayname等於“寶寶”的記錄分值設定為99,其他按照displaynamelen排序;
{
"query": { "function_score": { "filter": { "bool": { "should": [ { "term": { "termid": "1238218" } }, { "match_phrase": { "displayname": "寶寶" } } ] } }, "functions": [ { "filter": { "term": { "termid": "1238218" } }, "weight": 100 }, { "filter": { "term": { "displayname": "寶寶" } }, "weight": 99 }, { "script_score": { "script": "_score + doc['level'].value" } } ], "score_mode": "first" } } }