Elasticsearch - 查詢DSL



在 Elasticsearch 中,搜尋是透過使用基於 JSON 的查詢來執行的。一個查詢由兩個子句組成:

  • 葉子查詢子句 - 這些子句是匹配、術語或範圍,它們在特定欄位中查詢特定值。

  • 複合查詢子句 - 這些查詢是葉子查詢子句和其他複合查詢的組合,用於提取所需的資訊。

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

全文查詢

這些查詢用於搜尋全文文字,例如章節或新聞文章。此查詢根據與該特定索引或文件關聯的分析器工作。在本節中,我們將討論不同型別的全文查詢。

匹配查詢

此查詢將文字或短語與一個或多個欄位的值進行匹配。

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

還存在其他型別的術語級查詢,例如:

  • 存在查詢 - 如果某個欄位具有非空值。

  • 缺失查詢 - 這與存在查詢完全相反,此查詢搜尋缺少特定欄位或欄位值為 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
               ]
            }
         }
      }
   }
廣告