当我已将nums
(浮点数列表)编入索引时,我希望聚合可以访问该确切数据。相反,我所看到的是doc['nums']
表现得更像一套。也就是说,它从我的列表中重复删除元素并按升序对它们进行排序。任何解决方法?
#! /bin/sh
# Delete the index
curl -XDELETE "http://localhost:9200/test"
# Insert a document with one field -- a list of floats
curl -XPUT "http://localhost:9200/test/doc/1?refresh=true" -d '{
"nums": [5.0, 3.5, 3.5, 1.0]
}'
# Expected:
# aggregations.foobar.value == [5.0, 3.5, 3.5, 1.0]
# Actual:
# aggregations.foobar.value == [1.0, 3.5, 5.0]
curl -XGET "http://localhost:9200/test/_search" -d '{
"query": {
"match_all": {}
},
"filter": {},
"aggregations": {
"foobar": {
"scripted_metric": {
"init_script": "_agg[\"numList\"] = []",
"map_script": "if (doc != null) { _agg.numList.add(doc[\"nums\"].getValues()) }",
"combine_script": "return _agg.numList",
"reduce_script": "return _aggs.flatten()"
}
}
}
}'
答案 0 :(得分:1)
重复数据删除'发生的不是因为聚合,而是更早 - 在索引阶段。只要该字段被映射为" float",它将表现得像一组数字并删除重复项。
在聚合中保存重复项的唯一方法是引用文档的_source
,因为它已存储
{
"query": {
"match_all": {}
},
"aggs": {
"foobar": {
"terms": {
"script": "groovy.json.JsonOutput.toJson(_source.nums)"
}
}
}
}