1
我目前正在做一個elasticsearch請求來檢索一些數據。我已經成功地以Json格式編寫正確的請求。之後,我試圖將其轉換爲Java。但是,當我打印Java發送給ES的請求時,兩個請求都不一樣,我也沒有做到這一點。爲什麼我的java elasticsearch請求翻譯無效?
這裏是返回好數據的JSON請求:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{ "terms": { "accountId": ["107276450147"] } },
{"range" : {
"date" : {
"lt" : "1480612801000",
"gte" : "1478020801000"
} }
}]
}
}
}
},
"size" : 0,
"aggregations" : {
"field-aggregation" : {
"terms" : {
"field" : "publicationId",
"size" : 2147483647
},
"aggregations" : {
"top-aggregation" : {
"top_hits" : {
"size" : 1,
"_source" : {
"includes" : [ ],
"excludes" : [ ]
}
}
}
}
}
}
}
和Java生成的請求......這不返回數據好..
{
"from" : 0,
"size" : 10,
"aggregations" : {
"field-aggregation" : {
"terms" : {
"field" : "publicationId",
"size" : 2147483647
},
"aggregations" : {
"top-aggregation" : {
"top_hits" : {
"size" : 1,
"_source" : {
"includes" : [ ],
"excludes" : [ ]
}
}
}
}
}
}
}
最後的Java代碼生成錯誤的json請求:
TopHitsBuilder top = AggregationBuilders.topHits("top-aggregation")
.setFetchSource(true)
.setSize(1);
TermsBuilder field = AggregationBuilders.terms("field-aggregation")
.field(aggFieldName)
.size(Integer.MAX_VALUE)
.subAggregation(top);
BoolFilterBuilder filterBuilder = FilterBuilders.boolFilter()
.must(FilterBuilders.termsFilter("accountId", Collections.singletonList("107276450147")))
.must(FilterBuilders.rangeFilter("date").gte(1478020801000L).lte(1480612801000L));
NativeSearchQueryBuilder query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filterBuilder))
.withIndices("metric")
.withTypes(type)
.addAggregation(field);
return template.query(query.build());
首先,我必須刪除「大小」:10和Java產生的「from」...以及必須添加過濾器之後。我這樣做,但它永遠不會被添加.. 你能告訴我的java代碼有什麼問題,爲什麼過濾器不會出現在最終的Json中?
謝謝你們。
只是快速的事情,你爲什麼要刪除'size'和'from'? ES會自動使用默認值,然後等於'「from」:0,「size」:10' – ruhungry
您正在使用哪個版本的ES? – Val