Search code examples
elasticsearchelasticsearch-aggregationelasticsearch-dslelasticsearch-queryelasticsearch-mapping

Retrieve unique documents from elasticsearch index


How to get all the unique documents from a index.Do i need to write the aggregation queries for all the fields of document. Can i get unique documents without writting the aggregation for each n every field.


Solution

  • you can do that with script :

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

    From discuss elastic