可以
使用Elasticsearch的聚合(Aggregation)功能来实现
查询某一个字段的出现次数。以下是
使用JestClient进行
查询的
Java代码示例:
```
java
im
port io.searchbox.client.
JestClient;
im
port io.searchbox.core.Search;
im
port io.searchbox.core.SearchR
esult;
im
port io.searchbox.params.Parameters;
im
port org.elasticsearch.index.query.QueryBuilders;
im
port org.elasticsearch.search.aggregations.AggregationBuilders;
im
port org.elasticsearch.search.aggregations.bucket.terms.Terms;
im
port org.elasticsearch.search.aggregations.metrics.sum.Sum;
im
port
java.io.IOException;
public class ElasticsearchQuery {
public static void main(String[] args) throws IOException {
// 创建
JestClient实例
JestClient
jestClient =
JestClientFactoryUtil.get
JestClient();
// 构建
查询语句
Search search = new Search.Builder("{
" +
" "query": {
" +
" "match_all": {}
" +
" },
" +
" "aggs": {
" +
" "count_by_field": {
" +
" "terms": {
" +
" "field": "field_name.keyword"
" + // 要
查询的字段名
" }
" +
" }
" +
" }
" +
"}