I have this query I made using sense, I'm breaking my head how to transform it into Java. I can manage the aggs part, the real pain is the "constant_score"
GET /xxxx/yyyy/_search
{
"size": 0,
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"userId" : 275}},
{ "range" :{"logDate" : { "gte" : "2016-04-30", "lte" : "now/d" }}}
]
}
}
}
},
"aggs" : {
"datebucket" : {
"date_histogram" : {
"field" : "logDate",
"interval": "day",
"format": "yyyy-MM-dd",
"min_doc_count": 0
},
"aggs": {
"info": {
"filters": {
"filters" : [
{"term": { "logAction": "sleep" }},
{"term": { "logAction": "stop" }}
]
}
}
}
}
}
}
I had a more simple query I managed to do it like this
SearchResponse res = client.prepareSearch("xxxx").setTypes("yyyy")
.setSize(0)
.setQuery(QueryBuilders.termQuery("userId", 95))
.addAggregation(
AggregationBuilders.dateHistogram("date_histogram")
.field("logDate")
.interval(DateHistogramInterval.DAY)
.format("yyyy-MM-dd")
.minDocCount(0)
).execute().get();
Well, I found the solution. I hope this will help someone
String query = "{\"constant_score\" : "
+ "{ \"filter\" : "
+ "{\"bool\" : "
+ "{\"must\" : "
+ "[{ \"term\" : {\"userId\" : " + userID + "}}, "
+ "{ \"range\" :{\"logDate\" : { \"gte\" : \"" + startdate + "\", \"lte\" : \"" + enddate + "\" }}}]"
+ "}"
+ "}"
+ "}"
+ "}";
SearchResponse res = client.prepareSearch(xxxx).setTypes(yyyy)
.setQuery(query).addAggregation(
AggregationBuilders.dateHistogram("date_histogram")
.field("logDate")
.interval(DateHistogramInterval.DAY)
.format("dd-MM-yyyy")
.minDocCount(0)
.subAggregation(AggregationBuilders.filters("info")
.filter(QueryBuilders.termQuery("logAction", "click"))
.filter(QueryBuilders.termQuery("logAction", "view")))
).setSize(0).execute().get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.