嵌套字段类型在 Elasticsearch 中只会作为一个文档计数进行搜索。

huangapple go评论67阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月24日 15:45:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64041747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定