LoginSignup
3
0

More than 1 year has passed since last update.

Elasticsearchのもやもや: minimum_should_matchのデフォルト値は?

Last updated at Posted at 2022-08-26

はじめに

Elasticsearchを使っていて、もやもやした内容を、
メモ書きレベルで、思うがまま書いてみます。

minimum_should_matchパラメータとは

複数キーワードを使って検索を行う際、
最低何個以上のキーワードがマッチしたら検索ヒットとみなすか
を指定するためのパラメータ。

このminimum_should_matchのデフォルト値が、
クエリによって変わってしまうので、モヤモヤ感が。。。

Elasticsearch 7.x以降のリファレンス

If the bool query includes at least one should clause and no must or filter clauses,
the default value is 1. Otherwise, the default value is 0

minimum_should_matchのデフォルト値を確認してみた

  • ドキュメントを登録
PUT /my_index/_doc/1
{
  "user_name": "太郎",
  "date": "2022-10-01T10:00:00",
  "message": "Hello, Elasticsearch"
}
  • boolクエリにshould句のみ含まれている場合
    • minimum_should_matchのデフォルト値は1
    • いずれの検索ワードも含まれていないため、結果ヒットしない
GET /my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "message": "keyword1"
          }
        },
        {
          "match": {
            "message": "keyword2"
          }
        }
      ]
    }
  }
}
  • boolクエリに、must句とshould句が含まれている場合
    • minimum_should_matchのデフォルト値は0
    • shouldでいずれの検索ワードも含まれていなくても、結果ヒットする
GET /my_index/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "message": "Elasticsearch"
        }
      },
      "should": [
        {
          "match": {
            "message": "keyword1"
          }
        },
        {
          "match": {
            "message": "keyword2"
          }
        }
      ]
    }
  }
}

おわりに

minimum_should_matchのデフォルト値についてでした。
Elasticsearchバージョンにより挙動が変わるので(7.0を境に)、
気を付けたいところです。

3
0
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
3
0