Elasticsearch master (42)

In-depth aggregated data analysis _Global bucket: Sales of a single brand versus all brands

Aggregation, scope, an aggregation operation that must be performed within the search result scope of query

The first result is aggregated based on the query search results. As a result, all data is aggregated

 GET /waws_tvs/sales/_search 
 {
   "size": 0."query": {
     "term": {
       "brand": {
         "value": "Changhong"}}},"aggs": {
     "single_brand_avg_price": {
       "avg": {
         "field": "price"}},"all": {
       "global": {},
       "aggs": {
         "all_brand_avg_price": {
           "avg": {
             "field": "price"
           }
         }
       }
     }
   }
 }
Copy the code
  • Global: is the global bucket, which is the scope that brings all the data into the aggregation, regardless of the previous Query

Results show

 {
   "took": 3."timed_out": false,
   "_shards": {
     "total": 5."successful": 5."failed": 0
   },
   "hits": {
     "total": 3."max_score": 0."hits": []},"aggregations": {
     "all": {
       "doc_count": 8."all_brand_avg_price": {
         "value": 2650}},"single_brand_avg_price": {
       "value": 1666.6666666666667}}}Copy the code
  • Single_brand_avg_price: the average price of Changhong brand is executed for query search results
  • All. all_brand_avg_price: Get the average price for all brands

Elasticsearch master (43)

In-depth aggregation data analysis _ filter + aggregation: statistics of the average price of TV more than 1200

  • Search + Aggregate
  • Filtration + polymerization
 GET /waws_tvs/sales/_search 
 {
   "size": 0."query": {
     "constant_score": {
       "filter": {
         "range": {
           "price": {
             "gte": 1200}}}}},"aggs": {
     "avg_price": {
       "avg": {
         "field": "price"}}}} {"took": 10."timed_out": false,
   "_shards": {
     "total": 5."successful": 5."failed": 0
   },
   "hits": {
     "total": 7."max_score": 0."hits": []},"aggregations": {
     "avg_price": {
       "value": 2885.714285714286}}}Copy the code

Elasticsearch master (44)

In-depth aggregated data analysis _bucket filter: Statistics the average price of a brand in the last month

 GET /waws_tvs/sales/_search 
 {
   "size": 0."query": {
     "term": {
       "brand": {
         "value": "Changhong"}}},"aggs": {
     "recent_150d": {
       "filter": {
         "range": {
           "sold_date": {
             "gte": "now-150d"}}},"aggs": {
         "recent_150d_avg_price": {
           "avg": {
             "field": "price"}}}},"recent_140d": {
       "filter": {
         "range": {
           "sold_date": {
             "gte": "now-140d"}}},"aggs": {
         "recent_140d_avg_price": {
           "avg": {
             "field": "price"}}}},"recent_130d": {
       "filter": {
         "range": {
           "sold_date": {
             "gte": "now-130d"}}},"aggs": {
         "recent_130d_avg_price": {
           "avg": {
             "field": "price"}}}}}} {"took": 3."timed_out": false,
   "_shards": {
     "total": 5."successful": 5."failed": 0
   },
   "hits": {
     "total": 3."max_score": 0."hits": []},"aggregations": {
     "recent_130d": {
       "doc_count": 0."recent_130d_avg_price": {
         "value": null
       }
     },
     "recent_140d": {
       "doc_count": 0."recent_140d_avg_price": {
         "value": null
       }
     },
     "recent_150d": {
       "doc_count": 0."recent_150d_avg_price": {
         "value": null
       }
     }
   }
 }
Copy the code
  • Aggs. Filter, for aggregation to do

    • If a filter in a query is global, it affects all data
    • But if, for example, you want to collect the average of Changhong TV over the last month; Average of the last 3 months; Average for the last 6 months
  • Bucket filter: Filters agGs under different buckets