elasticsearch 弹性搜索中的过滤分组
我有一个查询,它根据下面提到的给定过滤器聚合数据
elasticsearch 弹性搜索中的过滤分组,
elasticsearch,filter,aggregation,
elasticsearch,Filter,Aggregation,我有一个查询,它根据下面提到的给定过滤器聚合数据 { "size": 0, "aggs": { "country": { "terms": { "field": "country", "size": 10, "order": { "messages": "desc" } }, "aggs": { "messages": {
{
"size": 0,
"aggs": {
"country": {
"terms": {
"field": "country",
"size": 10,
"order": {
"messages": "desc"
}
},
"aggs": {
"messages": {
"sum": {
"field": "count"
}
}
}
}
}
},
"query": {
"bool": {
"must": [],
"filter": [
{
"terms": {
"country": [
"US", "FR"
]
}
},
{
"terms": {
"locale": [
"en_US", "en_FR"
]
}
}
]
}
}
}
这个查询返回了两个拥有地区(我在过滤器中提到)数据的国家的所有数据
比如:US->en_US,en_FR和FR->en_US,en_FR
现在,我想获取US->en_-US和FR->en_-FR的数据(我需要在查询的过滤器逻辑中对其进行框显)。此外,还需要进行相应的汇总(基于国家和过滤器)
为此,我努力在弹性搜索中查找或构建查询。
任何建议都会有帮助您可以这样做:
{
"size": 0,
"aggs": {
"country": {
"terms": {
"field": "country",
"size": 10,
"order": {
"messages": "desc"
}
},
"aggs": {
"messages": {
"sum": {
"field": "count"
}
}
}
}
},
"query": {
"bool": {
"must": [],
"minimum_should_match": 1,
"should": [
{
"bool": {
"filter": [
{
"term": {
"country": "US"
}
},
{
"term": {
"locale": "en_US"
}
}
]
}
},
{
"bool": {
"filter": [
{
"term": {
"country": "FR"
}
},
{
"term": {
"locale": "en_FR"
}
}
]
}
}
]
}
}
}
在没有任何必须字段的情况下工作正常,但是当我在必须字段中指定时间范围时,它忽略了应该中提到的约束。是否有任何东西可以获取给定范围内的数据,并且满足sholud文件中提到的约束条件?如果您添加了必须条件,您还需要添加
最小值\u应匹配:1
。我已经更新了我的答案,请看一看。