【发布时间】:2020-06-19 08:13:14
【问题描述】:
我正在尝试计算具有唯一嵌套字段值的文档(接下来也是文档本身)。看起来获得独特的文件是可行的。
但是当我尝试执行count 的请求时,我收到如下错误:
抑制:org.elasticsearch.client.ResponseException:方法 [POST],主机 [http://localhost:9200],URI [/package/_count?ignore_throttled=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true],状态行 [HTTP/1.1 400 Bad要求] {"error":{"root_cause":[{"type":"parsing_exception","reason":"request 不支持 [collapse]","line":1,"col":216}],"type ":"parsing_exception","reason":"request 不支持 [collapse]","line":1,"col":216},"status":400}
代码:
BoolQueryBuilder innerTemplNestedBuilder = QueryBuilders.boolQuery();
NestedQueryBuilder templatesNestedQuery = QueryBuilders.nestedQuery("attachment", innerTemplNestedBuilder, ScoreMode.None);
BoolQueryBuilder mainQueryBuilder = QueryBuilders.boolQuery().must(templatesNestedQuery);
if (!isEmpty(templateName)) {
innerTemplNestedBuilder.filter(QueryBuilders.termQuery("attachment.name", templateName));
}
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource()
.collapse(new CollapseBuilder("attachment.uuid"))
.query(mainQueryBuilder);
// NEXT LINE CAUSE ERROR
long count = client.count(new CountRequest("package").source(searchSourceBuilder), RequestOptions.DEFAULT).getCount(); <<<<<<<<<< ERROR HERE
// THIS WORKS
SearchResponse searchResponse = client.search(
new SearchRequest(
new String[] {"package"},
searchSourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS)).from(offset).size(limit)
).indices("package").searchType(SearchType.DFS_QUERY_THEN_FETCH),
RequestOptions.DEFAULT
);
return ....;
该方法的总体意图是获取部分文档和所有此类文档的数量。可能已经存在另一种方法来满足这种需求。如果我尝试使用aggregations 和cardinality 获取count - 我得到的结果为零,并且它看起来不适用于嵌套字段。
计数请求:
{
"query": {
"bool": {
"must": [
{
"nested": {
"query": {
"bool": {
"adjust_pure_negative": true,
"boost": 1.0
}
},
"path": "attachment",
"ignore_unmapped": false,
"score_mode": "none",
"boost": 1.0
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
},
"collapse": {
"field": "attachment.uuid"
}
}
映射是如何创建的:
curl -X DELETE "localhost:9200/package?pretty"
curl -X PUT "localhost:9200/package?include_type_name=true&pretty" -H 'Content-Type: application/json' -d '{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 1
}}'
curl -X PUT "localhost:9200/package/_mappings?pretty" -H 'Content-Type: application/json' -d'
{
"dynamic": false,
"properties" : {
"attachment": {
"type": "nested",
"properties": {
"uuid" : { "type" : "keyword" },
"name" : { "type" : "text" }
}
},
"uuid" : {
"type" : "keyword"
}
}
}
'
代码生成的查询结果应该是这样的:
curl -X POST "localhost:9200/package/_count?&pretty" -H 'Content-Type: application/json' -d' { "query" :
{
"bool": {
"must": [
{
"nested": {
"query": {
"bool": {
"adjust_pure_negative": true,
"boost": 1.0
}
},
"path": "attachment",
"ignore_unmapped": false,
"score_mode": "none",
"boost": 1.0
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
},
"collapse": {
"field": "attachment.uuid"
}
}'
【问题讨论】:
-
您能否转储您的 java(?) 查询生成器实际生成的查询?您的文档样本加上您的映射也会很有用。您将无法将它们粘贴到 cmets 中,因此请编辑您的问题。
-
@jzzfs 已编辑 - 添加计数请求和索引映射
-
@jzzfs 也更新了更精确的错误消息
-
如果我尝试使用“_search”获取总数 - 仍将未折叠的值设为“total”。
标签: java elasticsearch elasticsearch-aggregation elasticsearch-query cardinality