2

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!

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Henry C
  • 4,781
  • 4
  • 43
  • 83
  • I believe it's "Text" now for string analysis searches. – ProgrammingLlama Aug 07 '17 at 10:57
  • Text? What do you mean - are you referring to string vs keyword / text properties? – Henry C Aug 07 '17 at 10:57
  • As in, you used to have `.String( ... )` and if you're doing something like an ngram, keyword search, etc. with it, it has becmome `.Text(...)` – ProgrammingLlama Aug 07 '17 at 10:58
  • See [here](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/fluent-mapping.html#_manual_mapping) – ProgrammingLlama Aug 07 '17 at 10:59
  • 1
    I was looking at https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html so I assumed that I should be using Keyword for Postcodes, but I see the documentation itself does show that you can't set an analyzer. So I'll have to use Text (apparently String is deprecated for 5.x also) - cheers! – Henry C Aug 07 '17 at 11:02
  • 1
    `keyword` data types are not analyzed and cannot have analysis applied; in 5.0 the `string` data type was split into `text` (analyzed) and `keyword` (not analyzed): https://www.elastic.co/blog/strings-are-dead-long-live-strings. `keyword` data types can have a normalizer applied in 5.2+ as it is often useful to normalize data in some way e.g. lowercase it, whilst still leveraging the `doc_values` underlying columnar data structure. The normalizer is restricted to producing a single token however (think of it as a restricted _analyzer_ if you will). – Russ Cam Aug 07 '17 at 12:08
  • Take a look at https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/writing-analyzers.html for writing analyzers in NEST – Russ Cam Aug 07 '17 at 12:09

1 Answers1

2

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")
                                                   )
                                       )
                                )
)
Henry C
  • 4,781
  • 4
  • 43
  • 83