天天看點

elasticsearch複合查詢一

es中資料如下所示:

{
   "took":      6,
   "timed_out": false,
   "_shards": { ... },
   "hits": {
      "total":      3,
      "max_score":  1,
      "hits": [
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "3",
            "_score":         1,
            "_source": {
               "first_name":  "Douglas",
               "last_name":   "Fir",
               "age":         35,
               "about":       "I like to build cabinets",
               "interests": [ "forestry" ]
            }
         },
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "1",
            "_score":         1,
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            "_index":         "megacorp",
            "_type":          "employee",
            "_id":            "2",
            "_score":         1,
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}
           

業務資訊全部包含在_source這個值裡面,業務需求是查詢last_name包含Sm的資料

你可以看到我們依然使用megacorp索引和employee類型,但是我們在結尾使用關鍵字_search來取代原來的文檔ID。響應内容的hits數組中包含了我們所有的三個文檔。預設情況下搜尋會傳回前10個結果。

GET /megacorp/employee/_search?q=last_name:Smith

我們在請求中依舊使用_search關鍵字,然後将查詢語句傳遞給參數q.

總結:

es中應付這樣的查詢

GET ip:9200/index名/type名/_search
           

預設查詢所有顯示10條

GET ip:9200/index名/type名/_search?q=條件包含 ....
           

.如上綠色部分

就會命中條件包含的所有資料

繼續閱讀