Aggregation with distinct null elements

Hello everybody !
I have PersonType with groupId field, which can be null, or even doesn't exist

{
Id: 1
Name : ToTo,
GroupId: 1
}
{
Id: 2
Name : TaTo,
GroupId: 2
}
{
Id: 3
Name : ToTa,
GroupId: null
}
{
Id: 4
Name : ToTo
}
{
Id: 5
Name : TaTa,
GroupId: 2
}

I need to sort them by Name and aggregate by GroupId, where if GroupId is null or doesn't exist it should be see as different group for each null
so for my example i will have :
2, 3, 1, 4

How can i do this, is it possible ?

Hey

Would the missing value features of the terms agg help here?

--Alex

Hi ! Thanks for your reply =)
No it's not good solution for me, because i need distinct persons when they have null group, but in your solution i will aggregate 3 persons with null groupId into one bucket .

I found solution. i use the script in aggregation, which choose GroupId or -Id, like that i can cover all documents with or without GroupId

private readonly string _customGroupIdScript = "if (doc['groupId'].empty || doc['groupId'].value == null || doc['groupId'].value == 0)"
                                                       + "{ return -(Long.parseLong(doc['id'].value)); } "
                                                       + "else { return doc['groupId'].value; }";
body.Aggregations(
                            aggs => aggs
                                    .Terms(
                                        "groupId",
                                        gr => gr
                                              .Script(_customGroupIdScript)
                                              .Order(
                                                  new TermsOrder
                                                  {
                                                      Key = "current_score",
                                                      Order = parameters.Sort.Order == Order.ASC
                                                                  ? appliedSort ? SortOrder.Ascending : SortOrder.Descending
                                                                  : SortOrder.Descending
                                                  })
                                              .Size(parameters.Start + parameters.Length)
                                              .Aggregations(
                                                  aggss => aggss
                                                           .Max("current_score", tt => tt.Script("_score"))
                                                           .TopHits(
                                                               "first_elementid", tt => tt
                                                                                        .Source(s => s.Includes(f => f.Field("id")))
                                                                                        .Size(1))))
                                    .Cardinality("totalDistinct", c => c.Script(_customGroupIdScript))
                        );
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.