ES聚合查询实例

查询特定渠道分享数量最大的30个文章的uuid:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "ul_actType": [
              1030
            ],
            "boost": 1
          }
        },
        {
          "terms": {
            "ul_distChannel": [
              2000,
              1000,
              1001,
              1019
            ],
            "boost": 1
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "explain": false,
  "aggregations": {
    "aggs_targetId": {
      "terms": {
        "field": "ul_targetId",
        "size": 30,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_term": "asc"
          }
        ]
      }
    }
  }
}
View Code

 

 

查询特定渠道30篇文章的阅读数,推荐数,点赞数,分享数:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "ul_targetId": [
              "94a803d3e883f96fc236c136de7fca2a",
              "dedff564f1e67d86fda44fb8dce6cbc5",
              "1b5ae5c0cfe26374af883e87d2b2f7c0",
              "3c5c5b3cc698aae419cad8d6eb02cbf2",
              "a7886af3f7d46dce4e5396dce3ed2b97",
              "35140f8d3f96bd48b3f6724c712150b0",
              "84801260669eec369d18225c6e63b01b",
              "10dc5b9bb733049a028b4ada6e7a4b84",
              "3587229434114e2fc3b3380643239f21",
              "93956578579740bc8b812eec57631f71",
              "9b9e248c892e3127ddcf3d5a440ab7a6",
              "4a9eb484d0595f210fbc49fdb1fb1eb5",
              "42f47d23cc154a4a6baddd5192f74f9c",
              "9b640b026fd85fe3eaa738dca483ff73",
              "2085b778ae6627cb236ae3cd6ba08acf",
              "3a0e3bab19b8d1902e1359a7d330f675",
              "4a10a6f8ddd78ae47bf3ff79bbc4a929",
              "83f179c72918e0ac424c6f08cb628922",
              "c642a8b80a5f72b426c1c30547e34ae3",
              "7d2994d2b17c331a4d14eb16362b41b7",
              "eaff7777a93fe1cfd05ce83dac3a07c4",
              "f239bd18ddb31a9b38305a3fa8135583",
              "ba3f535eb515c2f5948c902aa78e6733",
              "3fd4a262c96d17c2df22ede68c013fc8",
              "7bc7e72fdfb2f89200a975d8bec3323e",
              "a56fd1fc1cde98355cafb321bf6b7fa5",
              "1a21061052d3c72c5bff7c658fc9e656",
              "2cd32bc12d56168dc7143a756697a831",
              "70881bbbed12c5930af37de3d4c8cb6f",
              "8c66080ac50992e25a0a3b9f06eba89e"
            ],
            "boost": 1
          }
        },
        {
          "terms": {
            "ul_actType": [
              2000,
              1000,
              1010,
              1030
            ],
            "boost": 1
          }
        },
        {
          "terms": {
            "ul_distChannel": [
              2000,
              1000,
              1001,
              1019
            ],
            "boost": 1
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "explain": false,
  "aggregations": {
    "aggs_targetId": {
      "terms": {
        "field": "ul_targetId",
        "size": 30,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_term": "asc"
          }
        ]
      },
      "aggregations": {
        "aggs_actType": {
          "terms": {
            "field": "ul_actType",
            "size": 4,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_term": "asc"
              }
            ]
          }
        }
      }
    }
  }
}
View Code

 

 

 

聚合例子:

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "ul_actType": [
              1001
            ]
          }
        },
        {
          "terms": {
            "ul_actFrom": [
              1
            ]
          }
        },
        {
          "wildcard": {
            "ul_langMode": "*1*"
          }
        },
        {
          "range": {
            "ul_addTime": {
              "lt": 1494432000000,
              "gte": 1494345600000
            }
          }
        }
      ],
      "must_not": [
        {
          "wildcard": {
            "ul_userId": "temp*"
          }
        },
        {
          "term": {
            "ul_version.keyword": -1
          }
        }
      ]
    }
  },
  "size": 0,
  "aggregations": {
    "aggs_targetId": {
      "terms": {
        "field": "ul_targetId",
        "size": 50,
        "min_doc_count": 1000,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_term": "asc"
          }
        ]
      },
      "aggs": {
        "rare_targetId": {
          "bucket_selector": {
            "buckets_path": {
              "doc_count": "_count"
            },
            "script": {
              "inline": "params.doc_count < 3000"
            }
          }
        }
      }
    }
  }
}
View Code

 

 

 多字段聚合例子:(2字段)

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "image_count": {
              "from": "0",
              "to": null,
              "include_lower": false,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "term": {
            "atype": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_pool": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_ql": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_off": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "terms": {
            "domain": [
              "goldenmob.com"
            ],
            "boost": 1
          }
        },
        {
          "range": {
            "add_time": {
              "from": "0",
              "to": "2017-06-15",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "size": 0,
  "aggregations": {
    "agg_author_count": {
      "terms": {
        "field": "author"
      },
      "aggregations": {
        "agg_cate": {
          "terms": {
            "field": "app_category"
          }
        }
      }
    }
  }
}
View Code

 

 多字段聚合例子:(3字段)

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "image_count": {
              "from": "0",
              "to": null,
              "include_lower": false,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "term": {
            "atype": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_pool": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_ql": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_off": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "terms": {
            "domain": [
              "goldenmob.com"
            ],
            "boost": 1
          }
        },
        {
          "range": {
            "add_time": {
              "from": "0",
              "to": "2017-06-15",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "size": 0,
  "aggs": {
    "agg_author_count": {
      "terms": {
        "field": "author"
      },
      "aggs": {
        "agg_cate": {
          "terms": {
            "field": "app_category"
          },
          "aggs": {
            "agg_lang": {
              "terms": {
                "field": "lang"
              }
            }
          }
        }
      }
    }
  }
}
View Code

注意:这里如果要进行多个字段的聚合,需要注意第二个aggs的位置,是在第一个的terms之后,而不是在第一个agg_author_count之后. aggregations = aggs

 

 

JSON转CSV:  https://json-csv.com/

转载于:https://www.cnblogs.com/0xcafedaddy/p/6501814.html

好的,如果您使用Java语言,可以使用Elasticsearch的Java客户端RestHighLevelClient进行聚合查询。以下是一个示例代码,按天分组: ``` import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; import java.util.concurrent.TimeUnit; public class AggregationExample { public void aggregationByDate(RestHighLevelClient client, String indexName, String dateField) throws IOException { SearchRequest searchRequest = new SearchRequest(indexName); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchAllQuery()); Histogram histogramAggregation = AggregationBuilders.dateHistogram("group_by_day") .field(dateField) .calendarInterval(DateHistogramInterval.DAY) .format("yyyy-MM-dd"); searchSourceBuilder.aggregation(histogramAggregation); searchRequest.source(searchSourceBuilder); searchRequest.scroll(new TimeValue(60000L)); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); histogramAggregation = searchResponse.getAggregations().get("group_by_day"); for (Histogram.Bucket bucket : histogramAggregation.getBuckets()) { String keyAsString = bucket.getKeyAsString(); long docCount = bucket.getDocCount(); System.out.println("日期:" + keyAsString + ",文档数量:" + docCount); } } } ``` 其中,`client`参数为您创建的RestHighLevelClient实例,`indexName`为您要查询的索引名,`dateField`为您要聚合的日期字段名。 您可以根据需要修改`calendarInterval`和`format`参数来实现不同的聚合需求。例如,如果您要按月份聚合,可以将`calendarInterval`设置为`DateHistogramInterval.MONTH`,并将`format`设置为`yyyy-MM`。 希望这能帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值