Elasticsearch-query-dsl

提供:Dev Guides
移動先:案内検索

Elasticsearch-DSLのクエリ

Elasticsearchでは、JSONに基づくクエリを使用して検索が実行されます。 クエリは2つの句で構成されています-

  • Leaf Query Clauses -これらの句は、特定のフィールドで特定の値を検索する一致、用語、または範囲です。
  • 複合クエリ句-これらのクエリは、目的の情報を抽出するためのリーフクエリ句と他の複合クエリの組み合わせです。

Elasticsearchは多数のクエリをサポートしています。 クエリはクエリキーワードで始まり、その後、JSONオブジェクトの形式で内部に条件とフィルターがあります。 さまざまな種類のクエリについては、以下で説明しています。

すべてに一致するクエリ

これは最も基本的なクエリです。すべてのコンテンツを返し、すべてのオブジェクトに対して1.0のスコアを返します。

POST/schools/_search
{
   "query":{
      "match_all":{}
   }
}

上記のコードを実行すると、次の結果が得られます-

{
   "took" : 7,
   "timed_out" : false,
   "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
         "value" : 2,
         "relation" : "eq"
      },
      "max_score" : 1.0,
      "hits" : [
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : "5",
            "_score" : 1.0,
            "_source" : {
               "name" : "Central School",
               "description" : "CBSE Affiliation",
               "street" : "Nagan",
               "city" : "paprola",
               "state" : "HP",
               "zip" : "176115",
               "location" : [
                  31.8955385,
                  76.8380405
               ],
               "fees" : 2200,
               "tags" : [
                  "Senior Secondary",
                  "beautiful campus"
               ],
               "rating" : "3.3"
            }
         },
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : "4",
            "_score" : 1.0,
            "_source" : {
               "name" : "City Best School",
               "description" : "ICSE",
               "street" : "West End",
               "city" : "Meerut",
               "state" : "UP",
               "zip" : "250002",
               "location" : [
                  28.9926174,
                  77.692485
               ],
               "fees" : 3500,
               "tags" : [
                  "fully computerized"
               ],
               "rating" : "4.5"
            }
         }
      ]
   }
}

全文クエリ

これらのクエリは、章やニュース記事などのテキスト全体を検索するために使用されます。 このクエリは、その特定のインデックスまたはドキュメントに関連付けられているアナライザーに従って機能します。 このセクションでは、さまざまなタイプの全文クエリについて説明します。

一致クエリ

このクエリは、テキストまたはフレーズを1つ以上のフィールドの値と一致させます。

POST/schools*/_search
{
   "query":{
      "match" : {
         "rating":"4.5"
      }
   }
}

上記のコードを実行すると、次のように応答が得られます-

{
   "took" : 44,
   "timed_out" : false,
   "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
         "value" : 1,
         "relation" : "eq"
      },
      "max_score" : 0.47000363,
      "hits" : [
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : "4",
            "_score" : 0.47000363,
            "_source" : {
               "name" : "City Best School",
               "description" : "ICSE",
               "street" : "West End",
               "city" : "Meerut",
               "state" : "UP",
               "zip" : "250002",
               "location" : [
                  28.9926174,
                  77.692485
               ],
               "fees" : 3500,
               "tags" : [
                  "fully computerized"
               ],
               "rating" : "4.5"
            }
         }
      ]
   }
}

マルチマッチクエリ

このクエリは、複数のフィールドを持つテキストまたはフレーズと一致します。

POST/schools*/_search
{
   "query":{
      "multi_match" : {
         "query": "paprola",
         "fields": [ "city", "state" ]
      }
   }
}

上記のコードを実行すると、次のように応答が得られます-

{
   "took" : 12,
   "timed_out" : false,
   "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
         "value" : 1,
         "relation" : "eq"
      },
      "max_score" : 0.9808292,
      "hits" : [
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : "5",
            "_score" : 0.9808292,
            "_source" : {
               "name" : "Central School",
               "description" : "CBSE Affiliation",
               "street" : "Nagan",
               "city" : "paprola",
               "state" : "HP",
               "zip" : "176115",
               "location" : [
                  31.8955385,
                  76.8380405
               ],
               "fees" : 2200,
               "tags" : [
                  "Senior Secondary",
                  "beautiful campus"
               ],
               "rating" : "3.3"
            }
         }
      ]
   }
}

クエリ文字列クエリ

このクエリは、クエリパーサーとquery_stringキーワードを使用します。

POST/schools*/_search
{
   "query":{
      "query_string":{
         "query":"beautiful"
      }
   }
}

上記のコードを実行すると、次のように応答が得られます-

{
   "took" : 60,
   "timed_out" : false,
   "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
      "value" : 1,
      "relation" : "eq"
   },
………………………………….

用語レベルのクエリ

これらのクエリは、主に数値、日付、列挙などの構造化データを処理します。

POST/schools*/_search
{
   "query":{
      "term":{"zip":"176115"}
   }
}

上記のコードを実行すると、次のように応答が得られます-

……………………………..
hits" : [
   {
      "_index" : "schools",
      "_type" : "school",
      "_id" : "5",
      "_score" : 0.9808292,
      "_source" : {
         "name" : "Central School",
         "description" : "CBSE Affiliation",
         "street" : "Nagan",
         "city" : "paprola",
         "state" : "HP",
         "zip" : "176115",
         "location" : [
            31.8955385,
            76.8380405
         ],
      }
   }
]
…………………………………………..

範囲クエリ

このクエリは、指定された値の範囲内の値を持つオブジェクトを見つけるために使用されます。 このため、次のような演算子を使用する必要があります-

  • gte -等しい
  • gt -より大きい
  • lte -より小さい
  • lt -より小さい

たとえば、以下のコードを観察します-

POST/schools*/_search
{
   "query":{
      "range":{
         "rating":{
            "gte":3.5
         }
      }
   }
}

上記のコードを実行すると、次のように応答が得られます-

{
   "took" : 24,
   "timed_out" : false,
   "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
         "value" : 1,
         "relation" : "eq"
      },
      "max_score" : 1.0,
      "hits" : [
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : "4",
            "_score" : 1.0,
            "_source" : {
               "name" : "City Best School",
               "description" : "ICSE",
               "street" : "West End",
               "city" : "Meerut",
               "state" : "UP",
               "zip" : "250002",
               "location" : [
                  28.9926174,
                  77.692485
               ],
               "fees" : 3500,
               "tags" : [
                  "fully computerized"
               ],
               "rating" : "4.5"
            }
         }
      ]
   }
}

次のような他のタイプの用語レベルのクエリも存在します-

  • Exists query -特定のフィールドにnull以外の値がある場合。
  • Missing query -これは、存在するクエリとはまったく反対です。このクエリは、特定のフィールドまたはnull値を持つフィールドのないオブジェクトを検索します。
  • ワイルドカードまたは正規表現クエリ-このクエリは正規表現を使用してオブジェクトのパターンを検索します。

複合クエリ

これらのクエリは、and、or、not、異なるインデックス、または関数呼び出しなどのブール演算子を使用して互いにマージされた異なるクエリのコレクションです。

POST/schools/_search
{
   "query": {
      "bool" : {
         "must" : {
            "term" : { "state" : "UP" }
         },
         "filter": {
            "term" : { "fees" : "2200" }
         },
         "minimum_should_match" : 1,
         "boost" : 1.0
      }
   }
}

上記のコードを実行すると、次のように応答が得られます-

{
   "took" : 6,
   "timed_out" : false,
   "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
         "value" : 0,
         "relation" : "eq"
      },
      "max_score" : null,
      "hits" : [ ]
   }
}

ジオクエリ

これらのクエリは、ジオロケーションとジオポイントを処理します。 これらのクエリは、学校など、あらゆる場所に近い地理的オブジェクトを見つけるのに役立ちます。 ジオポイントデータタイプを使用する必要があります。

PUT/geo_example
{
   "mappings": {
      "properties": {
         "location": {
            "type": "geo_shape"
         }
      }
   }
}

上記のコードを実行すると、次のように応答が得られます-

{  "acknowledged" : true,
   "shards_acknowledged" : true,
   "index" : "geo_example"
}

次に、上記で作成したインデックスにデータを投稿します。

POST/geo_example/_doc?refresh
{
   "name": "Chapter One, London, UK",
   "location": {
      "type": "point",
      "coordinates": [11.660544, 57.800286]
   }
}

上記のコードを実行すると、次のように応答が得られます-

{
   "took" : 1,
   "timed_out" : false,
   "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
   },
   "hits" : {
      "total" : {
         "value" : 2,
         "relation" : "eq"
      },
      "max_score" : 1.0,
      "hits" : [
         "_index" : "geo_example",
         "_type" : "_doc",
         "_id" : "hASWZ2oBbkdGzVfiXHKD",
         "_score" : 1.0,
         "_source" : {
            "name" : "Chapter One, London, UK",
            "location" : {
               "type" : "point",
               "coordinates" : [
                  11.660544,
                  57.800286
               ]
            }
         }
      }
   }