gpt4 book ai didi

elasticsearch - Elasticsearch (2.4)-搜索和聚合数组

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

我有一个索引,映射为-

GET /sampledesc/desc/_mapping
{
"sampledesc": {
"mappings": {
"desc": {
"properties": {
"labels": {
"type": "string",
"index": "not_analyzed"
},
"tags": {
"type": "string",
"index": "not_analyzed"
},
"user_id": {
"type": "long"
}
}
}
}
}
}

我在其中添加了一些数据-
PUT /sampledesc/desc/1
{
"user_id": 1,
"tags": ["tag1", "tag2", "tag3"],
"labels": ["label1","label2"]
}

PUT /sampledesc/desc/2
{
"user_id": 2,
"tags": ["tag2","tag3",],
"labels": ["label2","label4"]
}

PUT /sampledesc/desc/3
{
"user_id": 3,
"tags": ["tag7"],
"labels": ["label1","label4"]
}

我想使用以下规则查询此数据:
  • 查找已给定标签列表的所有用户。
  • 对于这些用户,请按计数对标签进行分组。

  • 例如,我想查询同时具有tag2和tag3的用户,并按照标签的计数将标签分组。在给定的三个用户样本数据中,我的结果将是 label2 = 2; label1 = 1, label4 = 1

    最佳答案

    您好,您可以使用以下查询

    {
    "aggs": {
    "label_group": {
    "terms": {
    "field": "labels",
    "size": 10
    }
    }
    },
    "query": {
    "bool": {
    "must": [{
    "terms": {
    "tags": [
    "tag2"
    ]
    }
    }, {
    "terms": {
    "tags": [
    "tag3"
    ]
    }
    }]
    }
    }
    }

    希望这可以帮助
    谢谢

    关于elasticsearch - Elasticsearch (2.4)-搜索和聚合数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42004819/

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