3

How to use High Level Rest Client to find documents by distinct value. Documentation talks about Aggregation framework for finding distinct documents by field. But I could not find any example using High Level Rest Client. How can I get distinct documents by field.

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("Field1", "Value1"))
searchSourceBuilder.query(boolQueryBuilder);

searchSourceBuilder.aggregation(????)

Data

{
    ..
    ..

      "Field1" : "Value1",
      "Field2" : "Value2"
    },
    {
      "Field1" : "Value1",
      "Field2" : "Value21"
    }
}

Expected Output (any document with "Field1" : "Value1")

{
    ..
    ..

      "Field1" : "Value1",
      "Field2" : "Value2"
    }
}

Elastic Search Query for Distinct Nested Values

Elastic Aggregation

Code below gives the same result:

   AggregationBuilder termAggregationBuilder = AggregationBuilders
                .terms("someName")
                .field("Field1");
   AggregationBuilder topHitsAggregationBuilder = AggregationBuilders
                .topHits("someOtherName")
                .fetchSource(includeFields, excludeFields);
   searchSourceBuilder.aggregation(termAggregationBuilder.subAggregation(topHitsAggregationBuilder));
java_dude
  • 4,038
  • 9
  • 36
  • 61

0 Answers0