I have the next curl json and I want to do the same thing in Java - but the result is not the same.
The scope is to retrieve not only the last values but also some other fields, and these fields are missing from java result.
What I do wrong ?
JSON - with CURL
{
"size": 0,
"query": {
"bool": {
"must": [
{"prefix": {"filename.keyword": "/001"}}
,
{"term": {"ftype": "1"}}
]
}
},
"aggs": {
"sources": {
"terms": {
"field": "filename.keyword"
},
"aggs": {
"latest": {
"top_hits": {
"size": 1,
"_source": [
"inode",
"filename.keyword",
"snapshot_id",
"snapshot_uuid",
"snapshot_time_start"
],
"sort": {
"snapshot_time_start": "desc"
}
}
}
}
}
}
}
and JAVA code.
Client client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(address);
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
QueryBuilder qfilename = QueryBuilders.prefixQuery("filename.keyword", "/001");
QueryBuilder qftype = QueryBuilders.matchQuery("ftype", "1");
queryBuilder.must(qfilename);
queryBuilder.must(qftype);
String[] sourceInclude = { "inode", "filename.keyword", "snapshot_id", "snapshot_uuid", "snapshot_time_start" };
AggregationBuilder aggregation = AggregationBuilders.filter("agg", qftype).subAggregation(AggregationBuilders.terms("field").field("filename.keyword"));
TopHitsAggregationBuilder topHits = AggregationBuilders.topHits("xxx").size(1).explain(true).fetchSource(sourceInclude, null).sort("snapshot_time_start", SortOrder.DESC);
aggregation = aggregation.subAggregation(topHits);
SearchRequestBuilder searchRequest = client.prepareSearch("rm").setTypes("snapdiff").setQuery(queryBuilder).setFrom(0).setSize(1000).setExplain(true).addAggregation(aggregation);
SearchResponse searchResponse = searchRequest.execute().actionGet();
System.out.println(searchResponse);