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