gpt4 book ai didi

elasticsearch - ElasticSearch-聚合+范围+字词查询

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

使用ElasticSearch,我想通过过滤另一个字段的值来获得一个字段在一段时间内的总和。

我定义了这个映射:

PUT match-orders
{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
},
"mappings": {
"order": {
"_all": {"enabled": false},
"properties": {
"matchTime": {"type": "date", "index": "true"},
"product_id": {"type": "keyword", "index": "true"},
"size": {"type": "float", "index": "true"},
"price": {"type": "float", "index": "true"},
"side": {"type": "keyword", "index": "true"}
}
}
}
}

我可以得到一个范围的总和:
POST /match-orders/_search?pretty
{
"aggs" : {
"price_ranges" : {
"range" : {
"field" : "matchTime",
"ranges" : [
{ "from" : "2017-09-10T18:00:00Z", "to" : "2017-09-10T18:15:00Z" }
]
},
"aggs" : {
"result" : { "sum" : { "field" : "size" } }
}
}
}
}

我可以获得包含特定术语的文档:
POST /match-orders/_search?pretty
{
"query": {
"term" : { "side" : "sell" }
}
}

但是,如何合并这两个查询?

谢谢 :)

最佳答案

您只需要像这样将查询与聚合合并即可:

POST /match-orders/_search?pretty
{
"query": {
"term" : { "side" : "sell" }
},
"aggs" : {
"price_ranges" : {
"range" : {
"field" : "matchTime",
"ranges" : [
{ "from" : "2017-09-10T18:00:00Z", "to" : "2017-09-10T18:15:00Z" }
]
},
"aggs" : {
"result" : { "sum" : { "field" : "size" } }
}
}
}
}

关于elasticsearch - ElasticSearch-聚合+范围+字词查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46325024/

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