# Implement filter aggregation API via elastic .net client 8.1.1

**URL:** <https://discuss.elastic.co/t/implement-filter-aggregation-api-via-elastic-net-client-8-1-1/334940>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [June 1, 2023, 9:35am UTC](https://discuss.elastic.co/t/implement-filter-aggregation-api-via-elastic-net-client-8-1-1/334940 "2023-06-01T09:35:57Z")\
**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:** [June 1, 2023, 9:35am UTC](https://discuss.elastic.co/t/implement-filter-aggregation-api-via-elastic-net-client-8-1-1/334940/1 "2023-06-01T09:35:57Z")

</div>

Hello

I created this json query for searching & aggregation. In aggregation with did filtered specification attributes. How can i implement this query using elastic .net client 8.1.1 sdk through in my .net project?

Please guide to me, because i am newly using elastic .net client.

```auto
GET product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "published": {
              "value": true
            }
          }
        },
        {
          "multi_match": {
            "query": "shoes",
            "fields": ["name", "sku", "fullDescription", "shortDescription", "categories.name",
                    "manufacturers.name", "specificationAttributes.name", "specificationAttributes.specificationAttributeOptions.name"]
          }
        }
      ]
    }
  },
  "aggs": {
    "price": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 0,
            "to": 1000
          },
          {
            "from": 1001,
            "to": 2000
          },
          {
            "from": 2001,
            "to": 3000
          }
        ]
      }
    },
    "Category":{
      "terms": {
        "field": "categories.name.keyword",
        "size": 10
      }
    },
    "cat":{
      "filter": {
        "bool": {
          "must":[{
            "term":{
              "specificationAttributes.name.keyword":{
               "value":"Color" 
              }
            }
          },{
            "term":{
              "specificationAttributes.specificationAttributeOptions.name.keyword":{
               "value":"Red" 
              }
            }
          }]
        }
      },
      "aggs": {
        "Facets": {
          "multi_terms":{
            "terms":[
              {
                "field" :"categories.id"
              },{
                "field" :"categories.name.keyword"
              }]
          }
        }
      }
    },
    "Manufacturer":{
      "filter": {
        "bool": {
          "must":[{
            "term":{
              "specificationAttributes.name.keyword":{
               "value":"Color" 
              }
            }
          },{
            "term":{
              "specificationAttributes.specificationAttributeOptions.name.keyword":{
               "value":"Red" 
              }
            }
          }]
        }
      },
      "aggs": {
        "Facets": {
          "terms": {
            "field": "manufacturers.name.keyword",
            "size": 10
          }
        }
      }
    },
    "SpecAgg":{
      "filter": {
        "bool": {
          "must":[{
            "term":{
              "specificationAttributes.name.keyword":{
               "value":"Color" 
              }
            }
          },{
            "term":{
              "specificationAttributes.specificationAttributeOptions.name.keyword":{
               "value":"Red" 
              }
            }
          }]
        }
      },
      "aggs": {
        "facets": {
      "multi_terms":{
        "terms":[{"field":"specificationAttributes.name.keyword"},
          {"field":"specificationAttributes.specificationAttributeOptions.name.keyword"}]
      }
        }
      }
    }
  }
}

```

I need below query to implement with elastic .net client

```auto
"SpecAgg":{
      "filter": {
        "bool": {
          "must":[{
            "term":{
              "specificationAttributes.name.keyword":{
               "value":"Color" 
              }
            }
          },{
            "term":{
              "specificationAttributes.specificationAttributeOptions.name.keyword":{
               "value":"Red" 
              }
            }
          }]
        }
      },
      "aggs": {
        "facets": {
      "multi_terms":{
        "terms":[{"field":"specificationAttributes.name.keyword"},
          {"field":"specificationAttributes.specificationAttributeOptions.name.keyword"}]
      }
        }
      }
    }

```

Below is the my Elasticsearch .net client code for create aggregation but need to implement filter query in aggregation.

```auto
// aggregation dictionary
                var aggregations = new AggregationDictionary();

                //specification aggregation
                var specificationAttMultiTermLookUp = new MultiTermLookup
                {
                    Field = "specificationAttributes.name.keyword"
                };
                var specificationAttOptMultiTermLookUp = new MultiTermLookup
                {
                    Field = "specificationAttributes.specificationAttributeOptions.name.keyword"
                };
                var specificationAttOptMultiTermAgg = new MultiTermsAggregation("specAttrAgg")
                {
                    Terms = new MultiTermLookup[] { specificationAttMultiTermLookUp, specificationAttOptMultiTermLookUp },
                    Size = 100,
                };
                aggregations.Add(specificationAttOptMultiTermAgg);
                
                //var sortOrder = 
                
                // search request
                var request = new SearchRequest(nameof(Product).ToLowerInvariant())
                {
                    SearchType = SearchType.QueryThenFetch,
                    From = 0,
                    Size = 100,
                    Query = boolQuery,
                    Aggregations = aggregations,
                    //Sort = 
                };

```

---

<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 29, 2023, 9:36am UTC](https://discuss.elastic.co/t/implement-filter-aggregation-api-via-elastic-net-client-8-1-1/334940/2 "2023-06-29T09:36:31Z")

</div>

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