script score とは
- ドキュメントが持っているフィールドになんらかの処理をした結果をスコアに反映したいときなどに使える。
- SQLのストアドと一緒なので、ES側に計算(負荷)が増えることになるので、そこは注意
ESのインデックス(データ)
- フィールド:fruit_name
- 青森りんご/長野りんご/愛媛みかん/etc..
- フィールド:fruit_type
- りんご/みかん/etc..
- フィールド:fruit_area_code
- 1:青森/2:長野/etc..
サンプルクエリ
長野県産(fruit_area_code=2
)りんごを上位に表示したい( _score
を1.5倍にする)例
GET es_index_data/_search
{
"query": {
"function_score": {
"query": {
"bool": {
"must": {
"multi_match": {
"query": "りんご",
"type": "cross_fields",
"fields": [
"fruit_name",
"fruit_type"
],
"operator": "and",
"tie_breaker": 0.2
}
}
}
},
"script_score": {
"script": {
"lang": "painless",
"source": "def val = doc['fruit_area_code'].value;if(val == 2){ return _score * 1.5}return _score"
}
}
}
}
}