前端之家收集整理的这篇文章主要介绍了
带聚合的es复杂查询,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
/**
* 项目名称: logRetrievebyes
* 类名称: ESUtil
* 描述: es工具类
*
*/
public class T {
private Client client;
{
Settings settings = Settings.settingsBuilder().build();
try {
client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("xxx.xxx.xxx.xxx"),9300));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查询
*/
public long query(String keyWord,String stDate,String enDate,String dataType,int currentPage,int rowNum,List<Map<String,Object>> list) {
long num = 0;
QueryStringQueryBuilder queryBuilders = QueryBuilders.queryStringQuery(keyWord).defaultOperator(Operator.AND).field("full_name");
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(queryBuilders);
boolQueryBuilder.filter(QueryBuilders.boolQuery().must(
QueryBuilders.rangeQuery("logdate").gte(stDate).lte(enDate).format("yyyy-MM-dd HH:mm:ss")));
SearchRequestBuilder searchRequestBuilder = client.prepareSearch("logstash-2").setTypes("biglog")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(boolQueryBuilder)
.addAggregation(AggregationBuilders.terms("terms1_count").field("field1"))
//Terms Aggregation 名称为terms1_count 字段为field1 下面的也类似
.addAggregation(AggregationBuilders.terms("terms2_count").field("field2"));
searchRequestBuilder.addHighlightedField("*");
SearchResponse response = searchRequestBuilder.setHighlighterPreTags("<font color='red'>")
.setHighlighterPostTags("</font>").setHighlighterFragmentSize(10000000).setHighlighterRequireFieldMatch(false)
.setFrom((currentPage - 1) * rowNum).setSize(rowNum).addSort("logdate",SortOrder.DESC)
.setExplain(true).get();
//遍历聚会内容
Terms terms1Count = response.getAggregations().get("terms1_count");
for (Terms.Bucket entry : terms1Count.getBuckets()) {
String key = entry.getKeyAsString();
long docCount = entry.getDocCount();
System.out.println(key);
System.out.println(docCount);
}
Terms terms2Count = response.getAggregations().get("terms2_count");
for (Terms.Bucket entry : terms2Count.getBuckets()) {
String key = entry.getKeyAsString();
long docCount = entry.getDocCount();
System.out.println(key);
System.out.println(docCount);
}
SearchHits hits = response.getHits();
num = hits.getTotalHits();
System.out.println("总量" + hits.getTotalHits());
SearchHit[] hitArray = hits.hits();
for (SearchHit hit : hitArray) {
Map<String,Object> map = hit.getSource();
Map<String,HighlightField> highlightFields = hit.getHighlightFields();
// 打印高亮显示内容
for (Map.Entry<String,HighlightField> entry : highlightFields.entrySet()) {
StringBuilder sb = new StringBuilder();
Text[] text = entry.getValue().fragments();
for (Text str : text) {
sb.append(str.string());
}
map.put(entry.getKey(),sb.toString());
}
list.add(map);
}
return num;
}
/**
* 根据id查询
*/
public Map<String,Object> queryById(String id) {
GetResponse response = client.get(new GetRequest("logstash-2","biglog",id)).actionGet();
if (!response.isSourceEmpty()) {
return response.getSource();
}
return null;
}
public static void main(String[] args) {
ESUtil es = new ESUtil();
/*
* Map<String,Object> map = es.queryById("yu6ZUeu31vUcAW6s6eh"); System.out.println(JSON.toJSONString(map));
*/
es.query("测试 看看","2017-02-17 17:12:07","2017-02-19 17:21:08",null,5,10,new ArrayList<Map<String,Object>>());
}
}
原文链接:https://www.f2er.com/javaschema/283206.html