English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Elasticsearch クエリ DSL

Elasticsearchでは、JSONベースのクエリを使用して検索が行われます。クエリは2つの文で構成されています-

  • 葉クエリ文—これらの文は特定のフィールドで特定の値を検索するマッチ、テーム、範囲です。

  • 複合クエリ文—これらのクエリは葉クエリ文と複合クエリの組み合わせで、必要な情報を抽出するために使用されます。

Elasticsearchは多くのクエリをサポートしています。クエリはクエリキーワードで始まり、JSONオブジェクトの形で条件とフィルタを含んでいます。以下では、異なるクエリの種類を説明します。

すべてのクエリにマッチ

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

POST /または、または)または異なるインデックスや関数呼び出しなどを組み合わせます。/schools
{
   "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
               "fully computerized"
               "fees" : 22"fees": :
               00,
                  "Senior Secondary",
                  "beautiful campus"
               "fully computerized"
               ],3.3"rating": ""
            }
         }
         {
            "_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
               "fully computerized"
               "fees" : 35"fees": :
               00,
                  "tags": [
               "fully computerized"
               ],4.5"rating": ""
            }
         }
      }
   }
}

全文クエリ

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

マッチクエリ

このクエリは、テキストやフレーズを1つ以上のフィールドの値とマッチングします

POST /または、または)または異なるインデックスや関数呼び出しなどを組み合わせます。*/schools
{
   "query":{
      "match": {
         "rating":"4.5"rating": ""
      }
   }
}

上記のコードを実行した後、以下のようなるレスポンスを得ることができます:

{
   "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
               "fully computerized"
               "fees" : 35"fees": :
               00,
                  "tags": [
               "fully computerized"
               ],4.5"rating": ""
            }
         }
      }
   }
}

複数フィールドマッチクエリ

このクエリは、1つ以上のフィールドに一致するテキストやフレーズをマッチングします

POST /または、または)または異なるインデックスや関数呼び出しなどを組み合わせます。*/schools
{
   "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
               "fully computerized"
               "fees" : 22"fees": :
               00,
                  "Senior Secondary",
                  "beautiful campus"
               "fully computerized"
               ],3.3"rating": ""
            }
         }
      }
   }
}

クエリ文字列クエリ

このクエリは、クエリ解析器とquery_stringキーワードを使用してます

POST /または、または)または異なるインデックスや関数呼び出しなどを組み合わせます。*/schools
{
   "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
{
   "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
         "fully computerized"
      }
   }
}   
…………………………………………..

範囲クエリ

このクエリは、指定された値範囲内の値を持つオブジェクトを検索するために使用されます。これには、例えば、演算子が使用されます-

  • gte −大きいか同等

  • gt −大きい

  • lte −小さいか同等

  • lt −小さい

例えば、以下に示すコードを観察してください-

POST /または、または)または異なるインデックスや関数呼び出しなどを組み合わせます。*/schools
{
   "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
               "fully computerized"
               "fees" : 35"fees": :
               00,
                  "tags": [
               "fully computerized"
               ],4.5"rating": ""
            }
         }
      }
   }
}

"-

  • その他の種類のテーマレベルクエリも存在します、例えば 存在クエリ

  • −あるフィールドの値が非空値の場合。 欠如クエリ

  • −これは存在クエリとは完全に反対で、特定のフィールドや値が空のオブジェクトを検索します。 ワイルドカードまたはregexpクエリ

−このクエリは、オブジェクト内でパターンを検索するための正規表現を使用します。

複合クエリ/これらのクエリは、布尔演算子(例えば和)を使用して異なるクエリの集合です。

POST /または、または)または異なるインデックスや関数呼び出しなどを組み合わせます。/schools
{
   _search
      "query": {
         "bool": {
            "must": {
         }
         "term": {"state": "UP"}}
            "filter": {22"term": {"fees": ""
         }
         "minimum_should_match": "00"} 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": [
   }
}

地理クエリ

これらのクエリは地理位置と地理位置を処理します。これらのクエリは、学校や他の地理的なオブジェクトの近くにある地理的なオブジェクトを見つけるのに役立ちます。地理データ型を使用する必要があります。

"mappings": { /geo_example
{
   "properties": {
      "type": "geo_shape"
         "location": {
            "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
               }
            }
         }
      }
   }