2

Assuming my 2 values are "Red Square" and "Green circle", when i run the aggregation using Elastic search i get 4 values instead of 2, space separated? They are Red, Square, Green, circle. Is there a way to get the 2 original values.

The code is below:

 var result = this.client.Search<MyClass>(s => s
            .Size(int.MaxValue)
            .Aggregations(a => a
            .Terms("field1", t => t.Field(k => k.MyField))
            )
            );


        var agBucket = (Bucket)result.Aggregations["field1"];

        var myAgg = result.Aggs.Terms("field1");
        IList<KeyItem> list = myAgg.Items;

        foreach (KeyItem i in list)
        {
            string data = i.Key;
        }

1 Answers1

2

In your mapping, you need to set the field1 string as not_analyzed, like this:

{
    "your_type": {
        "properties": {
            "field1": {
                 "type": "string",
                 "index": "not_analyzed"
            }
        }
    }
}

You can also make field1 a multi-field and make it both analyzed and not_analyzed to get the best of both worlds (i.e. text matching on the analyzed field + aggregation on the exact value of the not_analyzed raw sub-field).

{
    "your_type": {
        "properties": {
            "field1": {
                 "type": "string",
                 "fields": {
                     "raw": {
                         "type": "string",
                         "index": "not_analyzed"
                     }
                 }
            }
        }
    }
}

If you choose this second option, you'll need to run your aggregation on field1.raw instead of field1.

Val
  • 207,596
  • 13
  • 358
  • 360
  • .NET code ..var result = this.client.Map(m => m .Properties(props => props .MultiField(s => s .Name(p => p.Plan) .Fields(pprops => pprops .String(ps => ps.Name(p => p.Plan).Index(FieldIndexOption.NotAnalyzed)) .String(ps => ps.Name("original").Index(FieldIndexOption.Analyzed)) ) ) ) ); – Mohd Saleem Navalur Jun 05 '15 at 13:00
  • Is there a way to get the counts of the items aggregated? like 3 "Red Square" 4 "Green circle" – Mohd Saleem Navalur Jun 10 '15 at 07:35