gpt4 book ai didi

elasticsearch - elasticsearch-数组匹配中的聚合计数

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

这是我的结构:

文件1有

{
"people": [
{
"name": "Darrell",
"age": "10",
},
{
"name": "Karen",
"age": "20",
},
{
"name": "Gary",
"age": "30",
}
]
}

文件2有
{
"people": [
{
"name": "Karen",
"age": "25",
},
{
"name": "Gary",
"age": "30",
}
]
}

现在,如何对与查询匹配的单个数组元素执行计数汇总?
  • 如果我查询name:Karen,我需要将计数为2。
  • 如果我查询姓名:Karen && age:20,我需要将计数为1。
  • 如果我查询姓名:Gary && age:30,则我的计数为2。
  • 最佳答案

    您需要将people对象映射为 nested objects,否则应映射it won't work as you expect

    curl -XPUT localhost:9200/your_index -d '{
    "mappings": {
    "your_type": {
    "properties": {
    "people": {
    "type": "nested",
    "properties": {
    "name": {"type": "string"},
    "age": {"type": "integer"}
    }
    }
    }
    }
    }
    }'

    一旦创建了索引和映射类型,并且在为数据重新建立索引之后,就可以像下面这样运行查询:
    curl -XPOST localhost:9200/your_index/_search -d '{
    "query": {
    "bool": {
    "must": [
    {
    "nested": {
    "path": "people",
    "query": {
    "term": {
    "people.name": "karen"
    }
    }
    }
    },
    {
    "nested": {
    "path": "people",
    "query": {
    "term": {
    "people.age": "20"
    }
    }
    }
    }
    ]
    }
    },
    "aggs": {
    "people": {
    "nested": {
    "path": "people"
    },
    "aggs": {
    "names": {
    "terms": {
    "field": "people.name"
    }
    }
    }
    }
    }
    }'

    关于elasticsearch - elasticsearch-数组匹配中的聚合计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35574512/

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