Elastic Search的聚合搜索


就是使用ES提供的aggs語法結果,使用DSL搜索的語法,實現聚合數據的統計,查詢。
ES中,如果新增document數據的時候,對應的index和type不存在,則自動創建。


1 准備源數據

PUT /products_index/phone_type/1
{
   "name":"IPHONE 8",
   "remark":"64G",
   "price":548800,
   "producer":"APPLE",
   "tags" : [ "64G", "red color", "Nano SIM" ]
}
PUT /products_index/phone_type/2
{
   "name":"IPHONE 8",
   "remark":"64G",
   "price":548800,
   "producer":"APPLE",
   "tags" : [ "64G", "golden color", "Nano SIM" ]
}
PUT /products_index/phone_type/3
{
   "name":"IPHONE 8 PLUS",
   "remark":"128G",
   "price":748800,
   "producer":"APPLE",
   "tags" : [ "128G", "red color", "Nano SIM" ]
}
PUT /products_index/phone_type/4
{
   "name":"IPHONE 8 PLUS",
   "remark":"256G",
   "price":888800,
   "producer":"APPLE",
   "tags" : [ "256G", "golden color", "Nano SIM" ]
}

 

將文本類型的fieldfielddata設置為true。用於設置ES中對倒排索引的設置,將倒排索引內容重設一份正排索引,並提供內存存儲計算能力。

正排索引,類似數據庫中的普通索引。依賴倒排索引中的數據,不做二次解析,將倒排索引解析的數據信息,建立一個索引,索引用於內存計算,如:分析,分組,字符串排序等。

PUT /products_index/_mapping/phone_type
{
   "properties" : {
      "tags" : {
         "type" : "text",
         "fielddata" : true
      }
   }
}

  

 

 

2 聚合統計
計算每個tag中的Document數量
terms : 檢索詞組的,安裝標准詞組分組,統計數據document的數量。類似數據庫中的count。
聚合搜索,語法的大體結構和DSL搜索語句類似。類似數據庫中的count。 Select count(*) from table

GET /products_index/phone_type/_search
{
   "size" : 0, # 查多少數據。
   "aggs" : { # 開始聚合,類似query,是一個命令。或api
      "group_by_tags":{ # 給聚合數據,加一個命名。自定義。
         "terms" : { # 是一個聚合api,類似數據庫中的聚合函數。解析某字段中的詞條。如:a字段的值是 test field.假設解析后詞條為test和field。那么就是根據a字段的解析詞條,test和field來統計每個數據在多少個document中存在。
             "field" : "tags"
         }
      }
   }
}

"size":0代表顯示多少計算源數據Document


3 增加搜索匹配條件的聚合統計
搜索名稱中包含PLUS的Document,並計算每個tag中的Document數量。統計是search中的一部分。一般在DSL query中使用。所以經常和條件搜索配合完成統計。

GET /products_index/phone_type/_search
{
   "size" : 0,
   "query" : {
      "match" : { "name" : "PLUS" }
   },
   "aggs" : {
      "group_by_tags":{
         "terms" : { "field" : "tags" }
      }
   }
}

  


4 聚合后實現計算
聚合嵌套

# 計算name中包含plus的document數據中的price字段平均值。
GET /products_index/phone_type/_search
{
   "query": {
     "match": {
       "name": "plus"
     }
   },
   "aggs": {
     "avg_by_price" : {
       "avg": {
         "field": "price"
       }
     }
   }
}

# 搜索包含plus的document,根據tags做詞條統計,在統計結果中,計算price平均值。聚合是可以嵌套的,內層聚合是依托於外層聚合的結果之上,實現聚合計算的。
GET /products_index/phone_type/_search
{
   "query": {
     "match": {
       "name": "plus"
     }
   },
   "aggs": {
     "group_by_tags":{
       "terms": {
         "field": "tags"
       },
       "aggs": {
         "avg_by_price": {
           "avg": {
             "field": "price"
           }
         }
       }
     }
   }
}

  

5 聚合的排序
類似SQL - select * from group by .. order by ..
聚合aggs中如果使用order排序的話,要求排序字段必須是一個aggs聚合相關的字段。聚合相關字段代表的含義是:當前聚合的子聚合命名。如:外部聚合是使用terms實現的聚合,命名為group_by_tags,其內層聚合是使用avg計算平均值,聚合名稱為avg_by_price,那么avg_by_price稱為聚合相關字段。
計算每個tag中的Document數據的price平均值,並根據price字段數據排序

GET /products_index/phone_type/_search
{
   "size" : 0,
   "aggs" : {
      "group_by_tags" : {
         "terms" : { "field" : "tags", "order":{"avg_price" : "desc"} },
         "aggs" : {
            "avg_price" : {
               "avg" : { "field" : "price" }
            }
         }
      }
   }
}

  


6 范圍分組並計算
使用price取值范圍分組,再計算分組Document中price的平均值

GET /products_index/phone_type/_search
{
   "query": {
     "match_all": {}
   },
   "_source": "price",
   "aggs": {
     "range_by_price": {
       "range": {
         "field": "price",
         "ranges": [
           {
             "from": 500000,
             "to": 600000
           },
           {
             "from": 600001,
             "to": 800000
           },
           {
             "from": 800001,
             "to": 1000000
           }
         ]
       },
       "aggs": {
         "avg_by_price": {
           "avg": {
             "field": "price"
           }
         }
       }
     }
   }
}

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM