Elasticsearch NEST facet is tokenizing field even if attribute of NotAnalyzed is defined in the mapping

321 views Asked by At

I am searching on Event objects in my elasticsearch index using NEST. At its basic level, Event looks like this:

public class Event
{
    public int Id {get; set;}
    [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
    public string EventType {get; set;}
}

My search looks like this:

SearchDescriptor<Event> search = new SearchDescriptor<Event>()
    .From(0)
    .Take(10000)
    .QueryRaw(@"{""match_all"": {} }")
    .FacetTerm("my_facet", f => f.OnField("eventType"));

var esResults = esClient.Search<Event>(search);

Currently, all of my documents in ES have an eventType of Test type, but the facet is returning results for Test and type, rather than returning them together.

My understanding is that the Index = FieldIndexOption.NotAnalyzed is supposed to alleviate this issue, but I am still seeing it. Why is this happening?

1

There are 1 answers

7
Rob On

Are you sure your index was created with proper mapping?

This code

var indicesOperationResponse = client.CreateIndex(descriptor => descriptor
    .Index(indexName)
    .AddMapping<Event>(m => m
        .MapFromAttributes()));

creates index with following mapping

{
    "my_index" : {
        "mappings" : {
            "event" : {
                "properties" : {
                    "eventType" : {
                        "type" : "string",
                        "index" : "not_analyzed"
                    },
                    "id" : {
                        "type" : "integer"
                    }
                }
            }
        }
    }
}

Your query works fine and returns correct facets:

{
   "took": 1,
   "timed_out": false,
   "_shards": {..},
   "hits": {..},
   "facets": {
      "my_facet": {
         "_type": "terms",
         "missing": 0,
         "total": 3,
         "other": 0,
         "terms": [
            {
               "term": "Test type",
               "count": 3
            }
         ]
      }
   }
}

I have tested it against elasticsearch and NEST 1.5.0.