Search code examples
c#elasticsearchnest

Elasticsearch filter group by using nest c#


I am using elastic search to get the products grouped by category and perform aggregations on result.... If I use categoryid(numeric) as a field its giving result but when i try to give category name its giving Unsuccessful(400)

Please see the blow code snippet

I am getting document count. Can i get document data from same request?

ISearchResponse<Products> results;

        results = _client.Search<Products>(s => s
                    //.Size(int.MaxValue)
                    .Query(q => q
                        .Bool(b => b
                            .Should(
                                bs => bs.Prefix(p => p.cat_name, "heli"),
                                bs => bs.Prefix(p => p.pr_name, "heli")
                                    )
                            )
                        )
                    .Aggregations(a => a
                        .Terms("catname", t => t
                            .Field(f => f.categoryid)
                            .Size(int.MaxValue)
                            .Aggregations(agg => agg
                               .Max("maxprice", av => av.Field(f2 => f2.price))
                               .Average("avgprice", av => av.Field(f3 => f3.price))
                               .Max("maxweidht", av => av.Field(f2 => f2.weight))
                               .Average("avgweight", av => av.Field(f3 => f3.weight))
                               )
                            )
                        )
                    );

mapping model:

{
  "product_catalog": {
     "mappings": {
  "properties": {
    "@timestamp": {
      "type": "date"
    },
    "@version": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "cat_name": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "categoryid": {
      "type": "long"
    },
    "createdon": {
      "type": "date"
    },
    "fulldescription": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "height": {
      "type": "float"
    },
    "length": {
      "type": "float"
    },
    "pr_name": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "price": {
      "type": "long"
    },
    "productid": {
      "type": "long"
    },
    "shortdescription": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "sku": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "updatedon": {
      "type": "date"
    },
    "weight": {
      "type": "float"
    },
    "width": {
      "type": "float"
        }
      }
    }
  }
}

Can anyone guide how to use category name for grouping.


Solution

  • catname field is of type text and thus you can't use it by default in aggregations because fielddata is disabled for performance reasons.

    Based on your mapping I see you are also indexing keyword as well for catname so you can use this field. Just change this part of your term aggregation .Field(f => f.categoryid) to .Field(f => f.cat_name.Suffix("keyword")) and you should be good.