Alex Gurskiy Alex Gurskiy - 4 months ago 48
C# Question

Aggregation by phrase in ElasticSearch using C# Nest

This is my aggregation:

return a =>
a.Terms("Group1", t => t.Field(ff => ff.ItemAttributeDesc1).
Aggregations(aa => aa.Terms("Value1", tt => tt.Field(fff => fff.SearchedFilterValue1))))
.Terms("Group2", t => t.Field(ff => ff.ItemAttributeDesc2).
Aggregations(aa => aa.Terms("Value2", tt => tt.Field(fff => fff.SearchedFilterValue2))))
.Terms("Group3", t => t.Field(ff => ff.ItemAttributeDesc3).
Aggregations(aa => aa.Terms("Value3", tt => tt.Field(fff => fff.SearchedFilterValue3))))
.Terms("Group4", t => t.Field(ff => ff.ItemAttributeDesc4).
Aggregations(aa => aa.Terms("Value4", tt => tt.Field(fff => fff.SearchedFilterValue4))))
.Terms("Group5", t => t.Field(ff => ff.ItemAttributeDesc5).
Aggregations(aa => aa.Terms("Value5", tt => tt.Field(fff => fff.SearchedFilterValue5))))
.Terms("Group6", t => t.Field(ff => ff.ItemAttributeDesc6).
Aggregations(aa => aa.Terms("Value6", tt => tt.Field(fff => fff.SearchedFilterValue6))))
.Terms("Group7", t => t.Field(ff => ff.ItemAttributeDesc7).
Aggregations(aa => aa.Terms("Value7", tt => tt.Field(fff => fff.SearchedFilterValue7))))
.Terms("Group8", t => t.Field(ff => ff.ItemAttributeDesc8).
Aggregations(aa => aa.Terms("Value8", tt => tt.Field(fff => fff.SearchedFilterValue8))))
.Terms("Brands", t => t.Field(ff => ff.VendorSearch).Size(60).Order(TermsOrder.TermAscending))
.Terms("Category", t => t.Field(ff => ff.MainSearch))
.Range("Price", ra => ra.Field(ff => ff.SalePrice)
.Ranges(
pr => pr.From(50).To(100),
pr => pr.From(100).To(250),
pr => pr.From(250).To(500),
pr => pr.From(500).To(750),
pr => pr.From(750).To(1000),
pr => pr.From(1000).To(1500),
pr => pr.From(1500).To(2000),
pr => pr.From(2000).To(2500),
pr => pr.From(2500).To(3000),
pr => pr.From(3000).To(3500),
pr => pr.From(3500)));


It's working as expected except one thing. I have to aggregate by full match. For example, if I have term field "Field" (one word) it's working fine. But when if the term contains of several words (or have specific symbols) it's not working. Not working, because elastic split phrase by spaces and symbols. I need 100% match here. Could anyone help me to resolve the issue?

Answer

This happens because the field you are aggregating on is mapped as an analyzed string which is the default mapping for string types, to resolve this, map the relevant fields you are aggregating on as type: string and index: not_analyzed

See more information regarding analyzed strings

Comments