# Facet\`s feature with NEST and aggregation api

**URL:** <https://discuss.elastic.co/t/facet-s-feature-with-nest-and-aggregation-api/126228>\
**Category:** Elasticsearch\
**Created:** [March 30, 2018, 12:38pm UTC](https://discuss.elastic.co/t/facet-s-feature-with-nest-and-aggregation-api/126228 "2018-03-30T12:38:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdcm](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rdcm](https://discuss.elastic.co/u/rdcm)\
**Post date:** [March 30, 2018, 12:38pm UTC](https://discuss.elastic.co/t/facet-s-feature-with-nest-and-aggregation-api/126228/1 "2018-03-30T12:38:00Z")

</div>

At this moment my UI has 4 groups with searching checkboxes:

```auto
1) text input (full text search and exact matches)
2) grades (list of checkboxes)
3) subjects (list of checkboxes)
4) types (list of checkboxes)

```

Between groups require `must` condition, between checkboxes in each group `should` condition.

C# code:

```auto
            return new SearchDescriptor<AssignmentDocument>().Index(AssignmentIndex)
                .Query(q => q.Bool(
                            b => b.Filter(
                                f => f.Bool(
                                    b2 => b2.Must(
                                        new BoolQuery { Should = gradesFilters },
                                        new BoolQuery { Should = subjectsFilters },
                                        new BoolQuery { Should = typesFilters }
                                    )
                                )
                            ).Must(SetText(query))
                        )
                    )
                    .From(query.From)
                    .Size(query.Size)
                    .Highlight(SetHighlights(query))
                    .Sort(sort => sort.Descending(doc => doc.CreationDate));

```

```auto
        public async Task<SearchResult> SearchAsync(SearchQuery query)
        {
            var sd = _searchDescriptorProvider.CreateSearchDescriptor(query);
            var response = await _elasticClient.SearchAsync(sd);

            return _mapper.Map<SearchResult>(response);
        }

```

How can i count finded documents for each checkbox without breaking `should/must` logic?

Besides following code snippet i have no idea how implement this functionality.

```auto
            return new MultiSearchDescriptor().Index(AssignmentIndex)
                    .Search<AssignmentDocument>(
                        "checkbox1_search",

                    //search query for checkbox1 with ValueCount aggregation
                    )
                    .Search<AssignmentDocument>(
                        "checkbox2_search",

                    //search query for checkbox2 with ValueCount aggregation
                    );

```

Can any suggest more efficient way for write this?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [March 31, 2018, 11:48am UTC](https://discuss.elastic.co/t/facet-s-feature-with-nest-and-aggregation-api/126228/2 "2018-03-31T11:48:21Z")

</div>

By using [named queries](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-named-queries-and-filters.html), you can see which queries matched for a given document in the response with the `matched_queries` array for each hit.

---

<div class="post-metadata">

**Author:** ![rdcm](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rdcm](https://discuss.elastic.co/u/rdcm)\
**Post date:** [April 2, 2018, 9:05am UTC](https://discuss.elastic.co/t/facet-s-feature-with-nest-and-aggregation-api/126228/3 "2018-04-02T09:05:37Z")

</div>

@forloop

Thank you! Very simple solution. But for counting filters hits, needed following code:

```auto
void Main()
{
   var docs = enumerateDocuments()
   	.SelectMany(x => x.MatchedQueries)
   	.GroupBy(x => x)
   	.Select(group => 
   		new 
   		{
   			Metric = group.Key,
   			Count = group.Count() 
   		});
   		
   		
   docs.Dump();
}

public class Document
{
   public int Id { get; set; }
   
   public string[] MatchedQueries { get; set; }
}

private static IEnumerable<Document> enumerateDocuments()
{
   yield return new Document { Id = 1, MatchedQueries = new [] { "foo", "bar" } };
   yield return new Document { Id = 2, MatchedQueries = new [] { "foo", "baz" } };
   yield return new Document { Id = 3, MatchedQueries = new [] { "foo" } };
   yield return new Document { Id = 4, MatchedQueries = new [] { "bar" } };
   yield return new Document { Id = 5, MatchedQueries = new [] { "baz" } };
   yield return new Document { Id = 6, MatchedQueries = new [] { "bar", "baz" } };
   yield return new Document { Id = 7, MatchedQueries = new [] { "foo", "baz" } };
}

```

Can i avoid linq usage on server side and aggregate needed metrics on elastic side with aggregation api?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 30, 2018, 9:05am UTC](https://discuss.elastic.co/t/facet-s-feature-with-nest-and-aggregation-api/126228/4 "2018-04-30T09:05:59Z")

</div>

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