Search code examples
c#elasticsearchnestelasticsearch-7

Elasticsearch 7.x case insensitive sorting using normalizer


I am using elasticsearch 7.5 and NEST client.

I want to sort a query, and as you know, by default does A..Za..z. I want it to be case insensitive.

I am trying to use a normalizer as is explained here

PUT /testindex
{
  "settings": {
     "analysis": {
       "normalizer": {
         "case_insensitive": {
            "filter": "lowercase"
         }
        }
       }
     }
}

And then I could use it in the mapping:

PUT /testindex/_mapping/testmapping
{
  "properties": {
    "Id": {
    "type": "keyword"
    },
    "Name": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "normalizer": "case_insensitive"
        }
      }
     }
   }
}

The problem it comes when I try to do it on the C# NEST client:

client.Indices.Create("testindex", e => e
                .Settings(s => s
                    .Analysis(a => a
                        .Normalizers(n => n.Custom("case_insensitive",c => c.Filters("lowercase")))))
                .Map(m => m
                    .Properties(p => p
                       .Text(st => st.Name("Name")
                       **.NORMALIZER**)))
            );

There is no way to add the normalizer the Name property field.

Any ideas? Another way to do it efficiently?

Thanks a million.


Solution

  • This property is part of keyword type properties as docs say.

    The normalizer property of keyword fields is similar to analyzer except that it guarantees that the analysis chain produces a single token.

    Simply changing your prop. to keyword field will allow you to place normalizer

    await client.Indices.CreateAsync("testindex", e => e
        .Settings(s => s
            .Analysis(a => a
                .Normalizers(n => n.Custom("case_insensitive", c => c.Filters("lowercase")))))
        .Map(m => m
            .Properties(p => p
                .Keyword(st => st.Normalizer("case_insensitive").Name("Name"))))
    );
    

    Hope that helps.