Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java elasticsearch如何按数组中的重复项进行分组,而无需区分_Java_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Java,elasticsearch" /> elasticsearch,Java,elasticsearch" />

Java elasticsearch如何按数组中的重复项进行分组,而无需区分

Java elasticsearch如何按数组中的重复项进行分组,而无需区分,java,elasticsearch,Java,elasticsearch,我试图通过数组中的重复项获得计数组,但不使用明显的aggs术语,但不起作用 GET /my_index/_search { "size": 0, "aggs": { "keywords": { "terms": { "field": "keywords" } } } } 文件包括: "keywords": [ "value1", "value1",

我试图通过数组中的重复项获得计数组,但不使用明显的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
            }
    ]

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

"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
    }
},
...]

用你的映射,你不能。因为你的钥匙在同一个文件里。我们可以使用脚本进行计数,明天将尽力帮助您。@LeBigCat谢谢我学习了lucene。我知道Elasticsearch使用lucene和反向索引。重复值在倒排表中具有相同的键。恐怕剧本太慢了。我想修改映射,比如:关键字:[{name:value 1,count:8},…]