Search code examples
c#.netelasticsearchnestelasticsearch-analyzers

How can I specify index / field analyzers using NEST fluent mapping for ElasticSearch 5.x?


Reasonably new to ElasticSearch / NEST - I have a property on a mapping that holds UK postcodes (eg DT5 2HW, BB1 9DR). At the moment, I have the following code:-

if (!client.IndexExists("user").Exists)
{
    client.CreateIndex("user", c => c.Mappings(
                                        m => m.Map<User>(
                                              mp => mp.AutoMap()
                                        )
                                    )                                                                    
                      );
}

I'm trying to find the correct place to specify an analyzer when creating a fluent mapping (so I can implement what's being done here), but:-

  • calling mp.AutoMap().Analyzer() is marked as deprecated / to be removed in 6.0 with a warning that default analyzers are to be removed at the type level, and need to be specified at the index or field level (sidenote: by field, do they mean property?)
  • Analyzer() is not available in Intellisense after either Keyword() or Name()

Is it just not possible to do so via a fluent mapping? Does this mean that I have to specify the available analyzers via CreateIndex -> Settings -> Analysis, then specify the Analyzers to use on a property level with attributes on the POCO?

I feel like I've gone fundamentally wrong somewhere - any pointers would be greatly appreciated!


Solution

  • It turns out the answer isn't about it being fluent or not, but you cannot specify analyzers for Keyword fields, so the data is to be used as-is.

    You can see the difference between the documentation for keyword with the documentation for text fields. I was a little misled by the text at the top of the reference for the keyword datatype that said "A field to index structured content such as email addresses, hostnames, status codes, zip codes or tags".

    I suspect what I'm trying to do is what Normalizers is being developed for, but it's still marked as experimental, but at least I can use Text for now.

    c => c.Mappings(m => m.Map<User>(
                        mp => mp.AutoMap()
                                .Properties(p => p.Text(
                                         t => t.Name(n => n.Postcode)
                                               .Analyzer("my_analyzer")
                                                       )
                                           )
                                    )
    )