Elasticsearch查詢DSL


Elasticsearch中,通過使用基於JSON的查詢進行搜尋。 查詢由兩個子句組成 -

  • 葉查詢子句 - 這些子句是匹配,項或範圍的,它們在特定欄位中查詢特定值。
  • 複合查詢子句 - 這些查詢是葉查詢子句和其他複合查詢的組合,用於提取所需的資訊。

Elasticsearch支援大量查詢。 查詢從查詢關鍵字開始,然後以JSON物件的形式在其中包含條件和過濾器。以下描述了不同型別的查詢 -

匹配所有查詢

這是最基本的查詢; 它返回所有內容,並為每個物件的分數為1.0。 例如,

POST http://localhost:9200/schools*/_search

請求正文

{
   "query":{
      "match_all":{}
   }
}

響應

{
   "took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
   "hits":{
      "total":5, "max_score":1.0, "hits":[
         {
            "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
            "_source":{
               "name":"Saint Paul School", "description":"ICSE Affiliation",
               "street":"Dawarka", "city":"Delhi", "state":"Delhi", 
               "zip":"110075", "location":[28.5733056, 77.0122136], "fees":5000, 
               "tags":["Good Faculty", "Great Sports"], "rating":"4.5"
            }
         },

         {
            "_index":"schools_gov", "_type":"school", "_id":"2", "_score":1.0,
            "_source":{
               "name":"Government School", "description":"State Board Affiliation",
               "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
               "location":[18.599752, 73.6821995], "fees":500, "tags":["Great Sports"],
               "rating":"4"
            }
         },

         {
            "_index":"schools", "_type":"school", "_id":"1", "_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_gov", "_type":"school", "_id":"1", "_score":1.0,
            "_source":{
               "name":"Model School", "description":"CBSE Affiliation",
               "street":"silk city", "city":"Hyderabad", "state":"AP", 
               "zip":"500030", "location":[17.3903703, 78.4752129], "fees":700, 
               "tags":["Senior Secondary", "beautiful campus"], "rating":"3"
            }
         },

         {
            "_index":"schools", "_type":"school", "_id":"3", "_score":1.0,
            "_source":{
               "name":"Crescent School", "description":"State Board Affiliation",
               "street":"Tonk Road", "city":"Jaipur", "state":"RJ", "zip":"176114",
               "location":[26.8535922, 75.7923988], "fees":2500, 
               "tags":["Well equipped labs"], "rating":"4.5"
            }
         }
      ]
   }
}

全文查詢

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

匹配查詢

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

POST http://localhost:9200/schools*/_search

請求正文

{
   "query":{
      "match" : {
         "city":"pune"
      }
   }
}

響應

{
   "took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
   "hits":{
      "total":1, "max_score":0.30685282, "hits":[{
         "_index":"schools_gov", "_type":"school", "_id":"2", "_score":0.30685282, 
         "_source":{
            "name":"Government School", "description":"State Board Afiliation",
            "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
            "location":[18.599752, 73.6821995], "fees":500, 
            "tags":["Great Sports"], "rating":"4"
         }
      }]
   }
}

multi_match查詢

此查詢將文字或短語與多個欄位匹配。 例如,

POST http://localhost:9200/schools*/_search

請求正文

{
   "query":{
      "multi_match" : {
         "query": "hyderabad",
         "fields": [ "city", "state" ]
      }
   }
}

響應

{
   "took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
   "hits":{
      "total":1, "max_score":0.09415865, "hits":[{
         "_index":"schools_gov", "_type":"school", "_id":"1", "_score":0.09415865,
         "_source":{
            "name":"Model School", " description":"CBSE Affiliation", 
            "street":"silk city", "city":"Hyderabad", "state":"AP", 
            "zip":"500030", "location":[17.3903703, 78.4752129], "fees":700, 
            "tags":["Senior Secondary", "beautiful campus"], "rating":"3"
         }
      }]
   }
}

查詢字串查詢

此查詢使用查詢解析器和query_string關鍵字。 例如,

POST http://localhost:9200/schools/_search

請求正文

{
   "query":{
      "query_string":{
         "query":"good faculty"
      }
   }
}

響應

{
   "took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0}, 
   "hits":{
      "total":1, "max_score":0.09492774, "hits":[{
         "_index":"schools", "_type":"school", "_id":"2", "_score":0.09492774, 
         "_source":{
            "name":"Saint Paul School", "description":"ICSE Affiliation",
            "street":"Dawarka", "city":"Delhi", "state":"Delhi",
            "zip":"110075", "location":[28.5733056, 77.0122136],
            "fees":5000, "tags":["Good Faculty", "Great Sports"],
            "rating":"4.5" 
         }
      }]
   }
}

期限等級查詢

這些查詢主要處理結構化資料,如數位,日期和列舉。 例如,

POST http://localhost:9200/schools/_search

請求正文

{
   "query":{
      "term":{"zip":"176115"}
   }
}

響應

{
   "took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
   "hits":{
      "total":1, "max_score":0.30685282, "hits":[{
         "_index":"schools", "_type":"school", "_id":"1", "_score":0.30685282,
         "_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"
         }
      }]
   }
}

範圍查詢

此查詢用於查詢值的範圍之間的值的物件。 為此,需要使用類似 -

  • gte ? 大於和等於
  • gt ? 大於
  • lte ? 小於和等於
  • lt ? 小於

例如 -

POST http://localhost:9200/schools*/_search

請求正文

{
   "query":{
      "range":{
         "rating":{
            "gte":3.5
         }
      }
   }
}

響應

{
   "took":31, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
   "hits":{
      "total":3, "max_score":1.0, "hits":[
         {
            "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
            "_source":{
               "name":"Saint Paul School", "description":"ICSE Affiliation",
               "street":"Dawarka", "city":"Delhi", "state":"Delhi", 
               "zip":"110075", "location":[28.5733056, 77.0122136], "fees":5000, 
               "tags":["Good Faculty", "Great Sports"], "rating":"4.5"
            }
         }, 

         {
            "_index":"schools_gov", "_type":"school", "_id":"2", "_score":1.0, 
            "_source":{
               "name":"Government School", "description":"State Board Affiliation",
               "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
               "location":[18.599752, 73.6821995] "fees":500, 
               "tags":["Great Sports"], "rating":"4"
            }
         },

         {
            "_index":"schools", "_type":"school", "_id":"3", "_score":1.0,
            "_source":{
               "name":"Crescent School", "description":"State Board Affiliation",
               "street":"Tonk Road", "city":"Jaipur", "state":"RJ", "zip":"176114", 
               "location":[26.8535922, 75.7923988], "fees":2500,
               "tags":["Well equipped labs"], "rating":"4.5"
            }
         }
      ]
   }
}

其他型別的期限級查詢是 -

  • 存在的查詢 - 如果某一個欄位有非空值。
  • 缺失的查詢 - 這與存在查詢完全相反,此查詢搜尋沒有特定欄位的物件或有空值的欄位。
  • 萬用字元或正規表示式查詢 - 此查詢使用正規表示式來查詢物件中的模式。

型別查詢 - 具有特定型別的文件。 例如,

POST http://localhost:9200/schools*/_search

請求正文

{
   "query":{
      "type" : {
         "value" : "school"
      }
   }
}

響應
存在於指定的索引中的所有學校的JSON物件。

複合查詢

這些查詢是通過使用如等,用於不同索引或具有函式呼叫等的布林運算子彼此合併的不同查詢的集合。例如,

POST http://localhost:9200/schools*/_search

請求正文

{
   "query":{
      "filtered":{
         "query":{
            "match":{
               "state":"UP"
            }
         },

         "filter":{
            "range":{
               "rating":{
                  "gte":4.0
               }
            }
         }
      }
   }
}

響應

{
   "took":16, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
   "hits":{"total":0, "max_score":null, "hits":[]}
}

連線查詢

這些查詢用於包含多個對映或文件的位置。 連線查詢有兩種型別 -

巢狀查詢
這些查詢處理巢狀對映(將在下一章閱讀了解更多內容)。

has_child和has_parent查詢

這些查詢用於檢索在查詢中匹配的文件的子文件或父文件。 例如,

POST http://localhost:9200/tutorials/_search

請求正文

{
   "query":
   {
      "has_child" : {
         "type" : "article", "query" : {
            "match" : {
               "Text" : "This is article 1 of chapter 1"
            }
         }
      }
   }
}

響應

{
   "took":21, "timed_out":false, "_shards":{"total":5, "successful":5, "failed":0},
   "hits":{
      "total":1, "max_score":1.0, "hits":[{
         "_index":"tutorials", "_type":"chapter", "_id":"1", "_score":1.0,
         "_source":{
            "Text":"this is chapter one"
         }
      }]
   }
}

地理查詢

這些查詢處理地理位置和地理點。這些查詢有助於查詢任何位置附近的學校或任何其他地理物件。需要使用地理位置資料型別。 例如,

POST http://localhost:9200/schools*/_search

請求正文

{
   "query":{
      "filtered":{
         "filter":{
            "geo_distance":{
               "distance":"100km",
               "location":[32.052098, 76.649294]
            }
         }
      }
   }
}

響應

{
   "took":6, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
   "hits":{
      "total":2, "max_score":1.0, "hits":[
         {
            "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
            "_source":{
               "name":"Saint Paul School", "description":"ICSE Affiliation",
               "street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075",
               "location":[28.5733056, 77.0122136], "fees":5000,
               "tags":["Good Faculty", "Great Sports"], "rating":"4.5"
            }
         },

         {
            "_index":"schools", "_type":"school", "_id":"1", "_score":1.0,
            "_source":{
               "name":"Central School", "description":"CBSE Affiliation",
               "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
               "location":[31.8955385, 76.8380405], "fees":2000,
               "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"
            }
         }
      ]
   }
}

注意 - 如果在執行上述範例時遇到異常,請將以下對映新增到索引。

{
   "mappings":{
      "school":{
         "_all":{
            "enabled":true
         },

         "properties":{
            "location":{
               "type":"geo_point"
            }
         }
      }
   }
}