英文:
Nested field type will only search as one doc count in elastic search
问题
我正在尝试使用AggregationBuilders
按其客户名称聚合我的文档计数,因为我们有多个与单个文档关联的客户。我们使用嵌套字段client_name
来索引和搜索文档。
以下是我的数据格式:
{
"id": "5f68c6c80f52a45a0db6d470",
"name": "Mi Note 10 Lite",
"brand": {
"name": "xiaomi"
},
"client_name": [
{
"name": "client a"
},
{
"name": "client b"
}
]
}
当我尝试使用聚合过滤器搜索客户a和b时,它只会返回一个文档计数:
"aggregations": {
"client a": {
"doc_count": 1
},
"client b": {
"doc_count": 0
}
}
我正在使用带有Elasticsearch 6.3的Java API。
以下是我的查询代码:
boolQuery.should(new NestedQueryBuilder(CLIENT_NAME,
QueryBuilders.termQuery(CLIENT_NAME + "." + NAME, token), ScoreMode.None));
这是我的AggregationBuilders.filter(k, v)
:
AggregationBuilders.filter(k, v);
难道不应该在两个存储桶上都返回文档计数1吗?还是我漏掉了什么?
英文:
I am trying to use AggregationBuilders
to aggregate my document count by its client name since we have multiple clients associated with a single doc. we are using the nested field client_name
to index and search for a document.
Here is my data format
{
"id": "5f68c6c80f52a45a0db6d470",
"name": "Mi Note 10 Lite",
"brand": {
"name": "xiaomi"
},
"client_name": [
{
"name": "client a"
},
{
"name": "client b"
}
]
}
when I'm trying to search with aggregation filter for a client a and b, it will be only return one document count
"aggregations": {
"client a": {
"doc_count": 1
},
"client b": {
"doc_count": 0
},
}
I am using java API with elastic search 6.3.
here is my query code
boolQuery.should(new NestedQueryBuilder(CLIENT_NAME,
QueryBuilders
.termQuery(CLIENT_NAME + "." + NAME, token),ScoreMode.None));
and here is my AggregationBuilders.filter(k,v);
should not aggregation bucket return doc count 1 on both bucket or am I missing something?
答案1
得分: 1
添加一个包含索引数据、映射、搜索结果和搜索查询的实际示例
索引映射:
{
"mappings": {
"properties": {
"client_name": {
"type": "nested",
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
}
索引数据:
{
"id": "5f68c6c80f52a45a0db6d470",
"name": "Mi Note 10 Lite",
"brand": {
"name": "xiaomi"
},
"client_name": [
{
"name": "client a"
},
{
"name": "client b"
}
]
}
搜索查询:
{
"size": 0,
"aggs": {
"client": {
"nested": {
"path": "client_name"
},
"aggs": {
"client_count": {
"terms": {
"field": "client_name.name"
}
}
}
}
}
}
搜索结果:
"aggregations": {
"client": {
"doc_count": 2,
"client_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "client a",
"doc_count": 1
},
{
"key": "client b",
"doc_count": 1
}
]
}
}
}
您可以参考此stackoverflow回答,将上述Elasticsearch查询转换为JAVA代码。
英文:
Adding a working example with index data, mapping, search result, and search query
Index Mapping:
{
"mappings": {
"properties": {
"client_name": {
"type": "nested",
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
}
Index Data:
{
"id": "5f68c6c80f52a45a0db6d470",
"name": "Mi Note 10 Lite",
"brand": {
"name": "xiaomi"
},
"client_name": [
{
"name": "client a"
},
{
"name": "client b"
}
]
}
Search Query:
{
"size" : 0,
"aggs": {
"client": {
"nested": {
"path": "client_name"
},
"aggs": {
"client_count": {
"terms": {
"field": "client_name.name"
}
}
}
}
}
}
Search Result:
"aggregations": {
"client": {
"doc_count": 2,
"client_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "client a",
"doc_count": 1
},
{
"key": "client b",
"doc_count": 1
}
]
}
}
You can refer this SO answer, to convert the above elasticsearch query to JAVA code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论