【问题标题】:Elastic search(2.4) - search and aggregate over arrays弹性搜索(2.4) - 搜索和聚合数组
【发布时间】:2017-06-19 16:16:25
【问题描述】:

我有一个映射为 -

的索引
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"]
}

我想用这些规则查询这些数据:

  1. 查找所有给出标签列表的用户。
  2. 对于这些用户,按计数对标签进行分组。

例如,我想查询同时拥有 tag2 和 tag3 的用户,并将标签按其计数按排序顺序分组。在给定的三个用户的示例数据中,我的结果是label2 = 2; label1 = 1, label4 = 1

【问题讨论】:

    标签: elasticsearch aggregate elasticsearch-aggregation elasticsearch-query nosql


    【解决方案1】:

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

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

    希望这会有所帮助 谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 2021-02-19
      • 2023-03-18
      相关资源
      最近更新 更多