# Using Terms filter query API via elastic .net client

**URL:** <https://discuss.elastic.co/t/using-terms-filter-query-api-via-elastic-net-client/334439>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [May 26, 2023, 1:50pm UTC](https://discuss.elastic.co/t/using-terms-filter-query-api-via-elastic-net-client/334439 "2023-05-26T13:50:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sagar\_Kayasth2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sagar_kayasth2/32/103180_2.png) [@Sagar\_Kayasth2](https://discuss.elastic.co/u/Sagar_Kayasth2)\
**Post date:** [May 26, 2023, 1:50pm UTC](https://discuss.elastic.co/t/using-terms-filter-query-api-via-elastic-net-client/334439/1 "2023-05-26T13:50:38Z")

</div>

Hi

I am currently trying to write a Terms Query via the Elastic.Clients.Elasticsearch 8.1.1 .NET client.

I have to apply dynamic terms query filter for filtered categories Ids. If any category id not selected then not applied this filter on the terms query.

```auto
 var response = await client.SearchAsync<EsProduct>(x => x.Index(nameof(Product).ToLowerInvariant())
                .Query(q => q
                    .Bool(b => b
                        .Must(m => m
                            .MultiMatch(mm => mm
                            .Query(keywords).Fields(fields)))
                        .Filter(f => f
                            .Term(publishedQuery) // check published                            
                            .Terms(ts => ts
                                .Field("categories.id")
                                .Terms(new TermsQueryField(categoriesIds.Select(id => FieldValue.Long(id)).ToArray())))
                            )))
                .Aggregations(a => a
                    .Range("price", r => r.Field(f => f.Price).Ranges(new AggregationRange[] { priceRange1, priceRange2, priceRange3 })))
                .From(0).Size(100)
                );

```

With this i can't able to get proper filtered products.  
Also, i can't able to create aggregation with field type keyword. Any suggestion for this.

---

<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:** [June 23, 2023, 1:51pm UTC](https://discuss.elastic.co/t/using-terms-filter-query-api-via-elastic-net-client/334439/2 "2023-06-23T13:51:36Z")

</div>

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