LoginSignup
40
42

More than 3 years have passed since last update.

Elasticsearchのクエリ概要

Last updated at Posted at 2019-08-31

はじめに

Elasticsearchでは検索が一番重要な処理です。
全文検索によってユーザーがほしい情報を上に表示できるのは理想です。
Googleエンジンのような強力なものはできないですが、それに近づくことでも利便性は高くなります。

Leaf query(検索の基礎)

1. term query(指定単語と完全一致する)

指定した単語で精確検索。boostの値で関連スコアの増減が調整できます。

サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "term": {
            "user": {
                "value": "Kimchy",
                "boost": 1.0
            }
        }
    }
}
'

2. range query

範囲検索
サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "range" : {
            "age" : {
                "gte" : 10,
                "lte" : 20,
                "boost" : 2.0
            }
        }
    }
}
'

3. macth query(分解された単語がすべてある)

全文検索の基礎で、マッチする際にクエリ文字を分析する

サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "match" : {
            "message" : {
                "query" : "this is a test",
                "operator" : "and"
            }
        }
    }
}
'

Compound query(複合条件検索)

1. bool query

動詞 概要  英語説明
must AND検索。かつスコアが加算する  The clause (query) must appear in matching documents and will contribute to the score. 
filter AND検索。ただし、スコアが加算しない The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching. 
should OR検索 The clause (query) should appear in the matching document.
must_not NOT検索 The clause (query) must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned.

サイトにあるサンプル:

curl -X POST "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "user" : "kimchy" }
      },
      "filter": {
        "term" : { "tag" : "tech" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } }
      ],
      "minimum_should_match" : 1,
      "boost" : 1.0
    }
  }
}
'

2. boosting query

関連度の検索です。
否定的なクエリにも一致するドキュメントの関連性スコアを減らしながら、肯定的なクエリに一致するドキュメントを返します。

サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "boosting" : {
            "positive" : {
                "term" : {
                    "text" : "apple"
                }
            },
            "negative" : {
                 "term" : {
                     "text" : "pie tart fruit crumble tree"
                }
            },
            "negative_boost" : 0.5
        }
    }
}
'

・positive:肯定的な検索。必須項目です。
・negative:否定的な検索。必須項目です。
・negative_boost:0~1.0の小数です。関連スコアの計算用の数字です。

表示順位の制御で重要なクエリです。

3. constant_score query

マッチしたドキュメントはboostに設定した数字のスコアを返す

サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "constant_score" : {
            "filter" : {
                "term" : { "user" : "kimchy"}
            },
            "boost" : 1.2
        }
    }
}
'

・filter:必須項目です。
・boost:オプション項目です。デフォルト値は1.0です。

4. dis_max query(Disjunction max query)

最高の関連性スコアの計算にスコアは高くなります。

サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "dis_max" : {
            "queries" : [
                { "term" : { "title" : "Quick pets" }},
                { "term" : { "body" : "Quick pets" }}
            ],
            "tie_breaker" : 0.7
        }
    }
}
'

・queries:必須項目です。クリエの配列です。
・tie_breaker:オプション項目です。0~1.0の小数です。デフォルト値は0.0です。

5. function_score query

スコアを調整するクエリです。

サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "function_score": {
          "query": { "match_all": {} },
          "boost": "5", 
          "functions": [
              {
                  "filter": { "match": { "test": "bar" } },
                  "random_score": {}, 
                  "weight": 23
              },
              {
                  "filter": { "match": { "test": "cat" } },
                  "weight": 42
              }
          ],
          "max_boost": 42,
          "score_mode": "max",
          "boost_mode": "multiply",
          "min_score" : 42
        }
    }
}
'

機能が煩雑で下記のページにあります。
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

score_mode:
image.png

boost_mode:
image.png

などなど

全文検索

全文検索の詳細は下記のページにあります。
https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html

1. intervals query

期間検索など、詳細は下記ページにあります。
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-intervals-query.html

2. Match query

文字列、数字、日付、boolなどでマッチする検索です。詳細は下記にページにあります。
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

3. Match boolean prefix query

4. Match phrase query

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
精度の制御できます。
サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "match_phrase" : {
            "message" : {
                "query" : "this is a test",
                "analyzer" : "my_analyzer"
            }
        }
    }
}
'

5. Match phrase prefix query

6. Multi-match query

名前通り、複数フィールドのmatchクエリです。
サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "multi_match" : {
      "query":    "Will Smith",
      "fields": [ "title", "*_name" ] 
    }
  }
}
'

7. Common Terms Query

8. Query string query

正しい文法が必要です。

サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "query_string" : {
            "query" : "(new york city) OR (big apple)",
            "default_field" : "content"
        }
    }
}
'

9. Simple query string query

query string queryと似ていますが、文法不正でもエラーにならないです。

サイトにあるサンプル:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "simple_query_string" : {
        "query": "\"fried eggs\" +(eggplant | potato) -frittata",
        "fields": ["title^5", "body"],
        "default_operator": "and"
    }
  }
}
'

精確でヒットするのはいろん工夫が必要です。

以上

40
42
1

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
40
42