从elasticsearch索引中检索唯一文档

时间:2018-02-11 17:27:11

标签: elasticsearch elasticsearch-aggregation elasticsearch-dsl elasticsearch-query elasticsearch-mapping

如何从索引中获取所有唯一文档。我需要为文档的所有字段编写聚合查询。我是否可以获得独特的文档而无需为每个字段写入聚合。

1 个答案:

答案 0 :(得分:0)

你可以用脚本来做到这一点:

    PUT test/doc/1 
{ "field1": "value1", "field2": "value2" }

 PUT test/doc/2 { "field1": "value2", "field2": "value3" } 

PUT test/doc/3 { "field1": "value1", "field2": "value3" } PUT test/doc/4 { "field1": "value3", "field2": "value4" }

 GET test/_search { "size": 0, "aggs": { "cardinality": { "cardinality": { "script": { "inline": "[ doc['field1'].value, doc['field2'].value ]" } } } } }

来自discuss elastic