gpt4 book ai didi

elasticsearch - 聚合中发现的Elasticsearch返回命中数

转载 作者:行者123 更新时间:2023-12-02 23:43:25 25 4
gpt4 key购买 nike

我正在尝试从数据库中获取具有唯一“sku”字段的行。
我有一个可以正常计算此数字的查询,我的查询是:

GET _search
{
"size": 0,
"aggs": {
"unique_products":{
"cardinality":{
"field":"sku.keyword"
}
}
},
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "(merch1: 'Dog') AND ((store_name: 'walmart')) AND product_gap: 'yes'"
}
},
{
"range": {
"capture_date": {
"format": "date",
"gte": "2020-05-13",
"lte": "2020-08-03"
}
}
}
]
}
}

}
返回此结果:
{
"took" : 129,
"timed_out" : false,
"_shards" : {
"total" : 514,
"successful" : 514,
"skipped" : 98,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 150,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"unique_products" : {
"value" : 38
}
}
}
正确地将unique_products的数量报告为38。
我正在尝试编辑此查询,以便它实际上将返回所有38个独特的产品,但不确定如何,我首先尝试从agg结果返回最佳匹配:
GET _search
{
"size": 0,
"aggs": {
"unique_products":{
"cardinality":{
"field":"sku.keyword"
}
},
"top_hits": {
"size": 1,
"_source": {
"include": [
"sku", "source_store"
]
}
}
},
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "(merch1: 'Dog') AND ((store_name: 'walmart')) AND product_gap: 'yes'"
}
},
{
"range": {
"capture_date": {
"format": "date",
"gte": "2020-05-13",
"lte": "2020-08-03"
}
}
}
]
}
}

}
但是我的结果有一个错误说:
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "Expected [START_OBJECT] under [size], but got a [VALUE_NUMBER] in [top_hits]",
"line": 10,
"col": 13
}
],
"type": "parsing_exception",
"reason": "Expected [START_OBJECT] under [size], but got a [VALUE_NUMBER] in [top_hits]",
"line": 10,
"col": 13
},
"status": 400
}
基数汇总仍然是我退回所有38种独特产品的最佳选择吗?谢谢

最佳答案

虽然基数聚合提供唯一计数,但它不能接受子聚合。换句话说,top_hits不能在这里直接使用。
该方法是正确的,但是您可能首先想对sku进行存储,然后使用top_hits检索基础文档:

{
"size": 0,
"aggs": {
"unique_products": {
"cardinality": {
"field": "sku.keyword"
}
},
"terms_agg": {
"terms": {
"field": "sku.keyword",
"size": 100
},
"aggs": {
"top_hits_agg": {
"top_hits": {
"size": 1,
"_source": {
"include": [
"sku",
"source_store"
]
}
}
}
}
}
},
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "(merch1: 'Dog') AND ((store_name: 'walmart')) AND product_gap: 'yes'"
}
},
{
"range": {
"capture_date": {
"format": "date",
"gte": "2020-05-13",
"lte": "2020-08-03"
}
}
}
]
}
}
}
仅供引用,您的查询引发异常的原因是 top_hits是agg 类型的agg ,就像 unique_products一样,它缺少自己的名称。

关于elasticsearch - 聚合中发现的Elasticsearch返回命中数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63238821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com