我正在嘗試從 ElasticSearch 索引中獲取聚合結果
例如,我的索引中的值
"_source": {
"ctry": "abc",
"totalentry": 1,
"entrydate": "2022-01-06"
},
"_source": {
"ctry": "abc",
"totalentry": 3,
"entrydate": "2022-01-07"
},
"_source": {
"ctry": "xyz",
"totalentry": 1,
"entrydate": "2022-01-08"
}
預期結果應根據國家/地區獲得總條目
ctry : abc
totalentry : 4
ctry : xyz
totalentry : 1
我的聚合查詢
QueryBuilder querybuilder = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("entrydate")
.gte("2022-01-01").lte ("2022-01-31"));
TermsAggregationBuilder groupBy = AggregationBuilders.terms("ctry").field("ctry");
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(querybuilder).addAggregation(groupBy)
.build();
List<Sample> records = elasticsearchRestTemplate.queryForList(searchQuery, Sample.class);
上面的聚合查詢回傳 3 條記錄而不是 2 條聚合結果。我的索引屬性
"ctry": {
"type": "keyword"
如何將其更改為以下,以便我希望我能得到正確的聚合結果
ctry": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
我的java代碼
@Document(indexName="sample", createIndex=true, shards = 4)
public class Sample {
@Field(type = FieldType.Keyword)
private String ctry;
uj5u.com熱心網友回復:
您正在使用 Spring Data Elasticsearch 的過時版本。這些queryForList變體在 4.0 中已棄用,并已在 4.2 中洗掉。
您需要使用其中一種search...()回傳SearchHits<Sample>>物件的方法。這將包含您的查詢和聚合的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/416986.html
標籤:
