如何在ElasticSearch上进行聚合时从正确的数组索引中提取记录?

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

How do I fetch the record from the correct index of array while aggregation on ElasticSearch?

问题

I can help you with the translation. Here's the translated content:

问题描述

我有一个ElasticSearch索引,其中存储了城市的名称和它们的ID。它的结构如下:

{
  "usr" : "plore113",
  "cityName": "New York",
  "cityId": "150p7"
},
{
  "usr" : "plore114",
  "cityName": "Delhi",
  "cityId": "171x9"
}

我正在对这个索引运行聚合操作,其中我获取所有城市名称并将它们分桶,同时也获取它们的ID。我是通过以下方式实现的:

{
  "size": 0,
  "aggs": {
    "cName": {
      "terms": {
        "field": "cityName.keyword",
        "size": 1000
      },
      "aggs": {
        "cId": {
          "terms": {
            "field": "cityId.keyword",
            "size": 1
          }
        }
      }
    }
  }
}

这个操作按预期工作,但现在我有不同的要求,我现在将会有cityName和cityId作为数组(名称和ID将与数组索引进行映射)。

示例:

cityName: ["New York", "Delhi", "Mumbai"],
cityId: ["150p7", "171x9", "183l0"]

请问如何运行相同的聚合操作,以便通过子聚合获取城市名称和它们的ID?

提前感谢。

英文:

Problem Statement

I have an ElasticSearch index that stores the Name of cities and their Ids. It looks like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

{
  &quot;usr&quot; : &quot;plore113&quot;
  &quot;cityName&quot;: &quot;New York&quot;,
  &quot;cityId&quot;: &quot;150p7&quot;
},
{
  &quot;usr&quot; : &quot;plore114&quot;
  &quot;cityName&quot;: &quot;Delhi&quot;,
  &quot;cityId&quot;: &quot;171x9&quot;
}

<!-- end snippet -->

I'm running an aggregation on this, where I get all the city names bucketed and I also get their Ids. I do it through this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

{
  &quot;size&quot;: 0, 
  &quot;aggs&quot;: {
    &quot;cName&quot;: {
      &quot;terms&quot;: {
        &quot;field&quot;: &quot;cityName.keyword&quot;,
        &quot;size&quot;: 1000
      },
      &quot;aggs&quot;: {
        &quot;cId&quot;: {
          &quot;terms&quot;: {
            &quot;field&quot;: &quot;cityId.keyword&quot;,
            &quot;size&quot;: 1
          }
        }
      }
    }
  }
}

<!-- end snippet -->

This is working as expected, however now I have different requirement, where I'll now have the cityName and cityId as arrays. (the name and id will be mapped with the array index).

Example:

cityName: [&quot;New York&quot;, &quot;Delhi&quot;, &quot;Mumbai&quot;],
cityId: [&quot;150p7&quot;, &quot;171x9&quot;, &quot;183l0&quot;]

How can I now run the same aggregation where I receive the cityNames, and also their Ids using the sub-aggregation?

Thanks in advance.

答案1

得分: 1

以下是翻译好的内容:

尝试使用以下代码,它创建一个运行时字段以同时过滤 cityName 和 cityId。

{
    "size": 0,
    "runtime_mappings": {
        "city_and_id": {
            "type": "keyword",
            "script": {
                "source": "emit(doc['cityName.keyword'].value + '-' +  doc['cityId.keyword'].value)"
            }
        }
    },
    "query": {
        "bool": {
            "filter": [
                {
                    "terms": {
                        "city_and_id": ["Delhi-171x9"]
                    }
                }
            ]
        }
    },
    "aggs": {
        "cName": {
            "terms": {
                "field": "cityName.keyword",
                "size": 1000
            },
            "aggs": {
                "cId": {
                    "terms": {
                        "field": "cityId.keyword",
                        "size": 1
                    }
                }
            }
        }
    }
}
英文:

Try this, it builds a runtime_field to filter cityName and cityId at the same time.

    {
        &quot;size&quot;: 0,
        &quot;runtime_mappings&quot;: {
            &quot;city_and_id&quot;: {
                &quot;type&quot;: &quot;keyword&quot;,
                &quot;script&quot;: {
                    &quot;source&quot;: &quot;emit(doc[&#39;cityName.keyword&#39;].value + &#39;-&#39; +  doc[&#39;cityId.keyword&#39;].value)&quot;
                }
            }
        },
        &quot;query&quot;: {
            &quot;bool&quot;: {
                &quot;filter&quot;: [
                    {
                        &quot;terms&quot;: {
                            &quot;city_and_id&quot;: [&quot;Delhi-171x9&quot;]
                        }
                    }
                ]
            }
        },
        &quot;aggs&quot;: {
            &quot;cName&quot;: {
                &quot;terms&quot;: {
                    &quot;field&quot;: &quot;cityName.keyword&quot;,
                    &quot;size&quot;: 1000
                },
                &quot;aggs&quot;: {
                    &quot;cId&quot;: {
                        &quot;terms&quot;: {
                            &quot;field&quot;: &quot;cityId.keyword&quot;,
                            &quot;size&quot;: 1
                        }
                    }
                }
            }
        }
    }

huangapple
  • 本文由 发表于 2023年4月13日 17:50:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004039.html
匿名

发表评论

匿名网友

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

确定