Implement filter aggregation API via elastic .net client 8.1.1

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.

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

"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.

// 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 = 
                };

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