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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

.NET code ..var result = this.client.Map<ClaimForm>(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)) ) ) ) );
Is there a way to get the counts of the items aggregated? like 3 "Red Square" 4 "Green circle"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.