elasticsearch如何按数组中的重复项进行分组而没有不同

时间:2019-01-28 08:33:21

标签: java elasticsearch

我正在尝试通过数组中的重复项来获取counts组,但不使用唯一性,使用aggs术语,但是不起作用

GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "keywords": {
       "terms": {
          "field": "keywords"
       }
    }
  }
}

文档,例如:

"keywords": [
            "value1",
            "value1",
            "value2"
          ],

但结果是:

"buckets": [
        {
          "key": "value1",
          "doc_count": 1
        },
        {
          "key": "value2",
          "doc_count": 1
        }
]

我如何获得如下结果:

"buckets": [
            {
              "key": "value1",
              "doc_count": 2
            },
            {
              "key": "value2",
              "doc_count": 1
            }
    ]

1 个答案:

答案 0 :(得分:1)

最后,我使用嵌套修改映射:

"keywords": {
    "type": "nested",
    "properties": {
        "count": {
            "type": "integer"
        },
        "keyword": {
            "type": "keyword"
        }
    }
},

并查询:

GET /my_index/_search
{
    "size": 0,
    "aggs": {
        "keywords": {
            "nested": {
                "path": "keywords"
            },
            "aggs": {
                "keyword_name": {
                    "terms": {
                        "field": "keywords.keyword"
                    },
                    "aggs": {
                        "sums": {
                            "sum": {
                                "field": "keywords.count"
                            }
                        }
                    }
                }
            }
        }
    }
}

结果:

"buckets": [{
    "key": "value1",
    "doc_count": 495,
    "sums": {
        "value": 609
    }
},
{
    "key": "value2",
    "doc_count": 440,
    "sums": {
        "value": 615
    }
},
{
    "key": "value3",
    "doc_count": 319,
    "sums": {
        "value": 421
    }
},
...]