2

I need to query certain fields, after aggregating.

Document structure is:

{
  id: 1,
  type: AA,
  hashValue: "qweqeqwdwwew"
    ...and many more fields
}

I want to aggregate by 'hashValue', so that i get only unique hashValues and the return results should also have the type. I need help with NEST query.

The current query to aggregate is:

var result = esClient.Search < EType > (q => q
.Index(esClient.Index)
.Routing(id.ToString(CultureInfo.InvariantCulture))
.Aggregations(ag => ag
  .Terms("Hash", ee => ee
    .Field(f => f.hashValue)))));

How can i extend it return type field along with hashValue?

Thanks.

bittusarkar
  • 6,247
  • 3
  • 30
  • 50
user2439903
  • 1,277
  • 2
  • 34
  • 68
  • What do you mean by "the return results should also have the type"? Can you provide a small example explaining the expected output? – bittusarkar Dec 14 '15 at 07:27
  • @bittusarkar: like key value pair, where key would be the "hashValue" and value will contain "type" field. – user2439903 Dec 14 '15 at 07:35
  • Still not clear. Do you want the output to be the count of documents per `Hash` field value per `Type` field value or you do you want two separate list of values one for each field? – bittusarkar Dec 14 '15 at 08:21
  • I want the output to contain both "hash" value and the "type" for that hash. for example: {"hashkey1": ["type1"], "haskey2": ["type1", "type2"], "haskey3": ["type4"], "hashkey4": ["type6"]} – user2439903 Dec 14 '15 at 08:39

1 Answers1

1

From your comments, it seems like you want to aggregate documents per type per hash value. For that the Nest query you need is as under:

var result = esClient.Search<EType>(q => q
    .Index(esClient.Index)
    .Routing(id.ToString(CultureInfo.InvariantCulture)
    .Aggregations(agHash => agHash
        .Terms("Hash", eeHash => eeHash
            .Field(fHash => fHash.hashValue)
        .Aggregations(agType => agType
            .Terms("Types", eeType => eeType
                .Field(fType => fType.typeValue))))));
bittusarkar
  • 6,247
  • 3
  • 30
  • 50