ES 查詢

1、構造運行環境

打開kibana進入Dev tools,創建索引,並插入測試數據,代碼如下:

PUT /logs/_doc/1
{
  "Name":"燕麥",
  "Desc":"燕麥商品描述",
  "Price":111,
  "Tags":["Breakfast","Carbon","Cheap"]
}

PUT /logs/_doc/2
{
  "Name":"牛奶",
  "Desc":"牛奶商品描述",
  "Price":222,
  "Tags":["Breakfast","Nutrition","Expensive"]
}

PUT /logs/_doc/3
{
  "Name":"麪包",
  "Desc":"牛奶商品描述",
  "Price":333,
  "Tags":["Breakfast","Barley","Cheap","Carbon"]
}

PUT /logs/_doc/4
{
  "Name":"玉米",
  "Desc":"玉米商品描述",
  "Price":444,
  "Tags":["Breakfast","Vegetables","Cheap","Carbon"]
}

PUT /logs/_doc/5
{
  "Name":"葡萄",
  "Desc":"葡萄商品描述",
  "Price":555,
  "Tags":["Breakfast","Fruits","Expensive","Carbon"]
}

執行以上代碼,並執行搜索,查看數據是否插入,簡單搜索代碼如下:

GET /logs/_search

或者

GET /logs/_search
{
  "query": {
    "match_all": {}
  }
}

注:這裏相當於select * from 表名 where 1=1,match_all相當於匹配所有.

查詢結果集如下:

{
  "took": 2,//當前請求花費的時間
  "timed_out": false,//當前請求是否超時
//當前請求的分片情況
"_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 5,//當前請求查詢到5條結果 "relation": "eq" //當前查詢的關係是等於 }, "max_score": 1, //當前查詢的評分,最大是1.0 "hits": [ { "_index": "logs", "_id": "1", "_score": 1, "_source": { "Name": "燕麥", "Desc": "燕麥商品描述", "Price": 111, "Tags": [ "Breakfast", "Carbon", "Cheap" ] } }, { "_index": "logs", "_id": "2", "_score": 1, "_source": { "Name": "牛奶", "Desc": "牛奶商品描述", "Price": 222, "Tags": [ "Breakfast", "Nutrition", "Expensive" ] } }, { "_index": "logs", "_id": "3", "_score": 1, "_source": { "Name": "麪包", "Desc": "牛奶商品描述", "Price": 333, "Tags": [ "Breakfast", "Barley", "Cheap", "Carbon" ] } }, { "_index": "logs", "_id": "4", "_score": 1, "_source": { "Name": "玉米", "Desc": "玉米商品描述", "Price": 444, "Tags": [ "Breakfast", "Vegetables", "Cheap", "Carbon" ] } }, "_index": "logs", "_id": "5", "_score": 1, "_source": { "Name": "葡萄", "Desc": "葡萄商品描述", "Price": 555, "Tags": [ "Breakfast", "Fruits", "Expensive", "Carbon" ] } } ] } }

這裏插入成功了.

 

2、相關度評分

1中的demo查詢結果集中有一個max_score字段就是相關度評分,當使用查詢接口時沒有指定排序字段,那麼ES就會按照每條記錄的評分進行排序.相關度評分中涉及到兩種算法,會在後續的文章中進行介紹.

 

3、元數據

1中的demo查詢結果集有一個source字段就是元數據,其大致結構如下

"_source": {
          "Name": "燕麥",
          "Desc": "燕麥商品描述",
          "Price": 111,
          "Tags": [
            "Breakfast",
            "Carbon",
            "Cheap"
          ]
        }

結果類似與關係型數據庫中的表字段和相關的值.

 

3.1、禁用元數據(source)

3.1.1 優缺點

優點:節省存儲開銷

缺點:不支持update、update_by_query、reindex_api、不支持高亮、不支持reindex,更改mapping分析器以及版本升級、通過索引時查看原文檔進行聚合查詢會失效、導致自動修復索引的功能實現.

注:如果單純爲了介紹存儲開銷,可以使用壓縮索引,比禁用source更好.

 

3.1.2 通過創建索引時指定mapping配置來控制source

缺點:通過創建索引時指定mapping配置,來強制限制souce的字段查詢的方式或者在mapping中禁用source,設置之後將無法修改.

(1)、完全禁用source

PUT /logs
{
  "mappings": {
    "_source": {
      "enabled": false
    }
  }
}

(2)、禁用source中的部分字段

PUT /logs
{
  "mappings": {
    "_source": {
      "includes": [
        "Name",
        "Tags"
      ],
      "excludes": [
        "Price",
        "Desc"
      ]
    }
  }
}

通過指定includes和excludes來展示和禁用source中的字段

注:不推薦使用mapping的方式來控制source!!!!!!!

 

3.1.3 通過在查詢條件來控制source

(1)、查詢時禁用mapping

GET /logs/_search
{
  "_source":false,
  "query": {
    "match_all": {}
  }
}

這時結果集中不會包含mapping等相關信息,只包含index、id、score等相關信息.

 

(2)、常規搜索

GET /logs/_search
{
  "_source": ["Name","Tags"], 
  "query": {
    "match_all": {}
  }
}

通過在api中指定_source來控制查詢結果返回的字段,類似與關係型數據庫中的 select Name,Tags from logs;

 

(3)、通配符查找

刪除1demo中的索引,新建以下索引

PUT /logs/_doc/1
{
  "Name":"燕麥",
  "Desc":"燕麥商品描述",
  "Price":111,
  "Items":{
    "Name":"子名稱",
    "Price":222
  },
  "Tags":["Breakfast","Carbon","Cheap"]
}

logs索引中包含一個Items的對象屬性,如果此時搜索只希望查Items的相關信息,可以執行以下操作

GET /logs/_search
{
  "_source":["Items.*"],
  "query": {
    "match_all": {}
  }
}

返回的結果如下:

   "hits": [
      {
        "_index": "logs",
        "_id": "1",
        "_score": 1,
        "_source": {
          "Items": {
            "Price": 222,
            "Name": "子名稱"
          }
        }
      }
    ]

 

(4)、includes查看和exincludes查找

GET /logs/_search
{
  "_source":{
    "includes": ["Items.*","Price","Desc"],
    "excludes": ["Name"]
  },
  "query": {
    "match_all": {}
  }
}

這裏可以指定查詢包含哪些字段和不包含哪些字段

 

4、QueryString 查詢

4.1 查詢所有

GET /logs/_search

4.2 分頁搜索

GET /logs/_search?from=0&size=3&sort=Price:desc

查詢從第from條開始,一共查size條數據,排序條件時Price按desc排序.

4.3 精準匹配

GET /logs/_search?q=Name:牛奶

這裏查詢的就是log中Name索引集合中值爲牛奶的document.        

注:ES默認會爲所有的字段創建倒排索引,如果通過q=字段:字段值的形式進行搜索,ES會去指定字段的索引集合查找相關的值並返回.

4.4 all搜索

GET /logs/_search?q=111

重點注意:ES默認會爲所有的字段創建倒排索引,所以如4.3中一樣,查詢條件沒有以q=字段:字段值的形式進行搜索,ES掃描所有建立了倒排索引的字段.所以這裏的結果集如下:

    "hits": [
      {
        "_index": "logs",
        "_id": "2",
        "_score": 1.3167865,
        "_source": {
          "Name": "牛奶",
          "Desc": "牛奶商品描述111",
          "Price": 222,
          "Tags": [
            "Breakfast",
            "Nutrition",
            "Expensive"
          ]
        }
      },
      {
        "_index": "logs",
        "_id": "1",
        "_score": 1,
        "_source": {
          "Name": "燕麥",
          "Desc": "燕麥商品描述",
          "Price": 111,
          "Tags": [
            "Breakfast",
            "Carbon",
            "Cheap"
          ]
        }
      }
    ]

Price和Desc中包含111的記錄都被檢索出來了.

注:這裏需要注意精準匹配的問題.demo中Desc字段會被分詞,在進行匹配,但是Price並不會.!!!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章