user2439903
user2439903

Reputation: 1307

NEST elastic search: how to return certain fields along with aggregated results?

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.

Upvotes: 2

Views: 839

Answers (1)

bittusarkar
bittusarkar

Reputation: 6357

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))))));

Upvotes: 1

Related Questions