gpt4 book ai didi

Elasticsearch 的等效 SQL Where 子句

转载 作者:行者123 更新时间:2023-11-29 02:52:13 25 4
gpt4 key购买 nike

我正在尝试在 Elasticsearch 中创建聚合结果,但过滤选项对我不起作用。

我可以在没有过滤器的情况下聚合数据,例如

select  name , material ,sum(price)
from products group by name , material

curl -XGET 'http://localhost:9200/products/_search?pretty=true' -d'
{
"aggs" : {
"product" : {
"terms" : {
"field" : "name"
},
"aggs" : {
"material" : {
"terms" : {
"field" : "material"
},
"aggs" : {
"sum_price" : {
"sum" : {
"field" : "price"
}
}
}
}
}
}
},
"size" : 0
}'

但是我在编写等效的 DSL 查询时遇到了问题:

select  name , material ,sum(price)
from products
where material = "wood"
group by name , material

最佳答案

应该是这样的:

{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"term": {
"material": "wood"
}
}
}
},
"aggs" : {
"product" : {
"terms" : {
"field" : "name"
},
"aggs" : {
"material" : {
"terms" : {
"field" : "material"
},
"aggs" : {
"sum_price" : {
"sum" : {
"field" : "price"
}
}
}
}
}
}
},
"size" : 0
}

如果您知道确切的值并且不需要匹配,请使用过滤器,否则使用匹配查询而不是过滤查询。

关于 Elasticsearch 的等效 SQL Where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25042470/

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