0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Elasticsearchで特定フィールの条件マッチしたものを上位に表示させたい

Posted at

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"
        }
      }
    }
  }
}

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?