question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Normalizer is not applied on fields

See original GitHub issue

NEST/Elasticsearch.Net version: 7.9.0 Elasticsearch version: 7.6.1 (Docker) Description of the problem including expected versus actual behavior: Hi, I basically need to query on text fields using a case insensitive Prefix to get all records having their productSearchName field “starting with” something.

Example of data :

productSearchName= "12 tons of ..."
productSearchName= "12 Tons of ..."
productSearchName= "12 TONS of ..."

I would like to get all these three records using a Prefix query, like

GET catalog/_search
{
  "_source": ["id", "productSearchName"], 
  "query": 
  {
    "prefix": {
      "productSearchName.Keyword": {
        "value": "12 tons"
      }
    }
  }
}

When defining a Normalizer (as I understood it’s the correct approach, is it ?) and telling my field should use it, the field is not mapped as expected, it’s a “basic” text fields on Keyword

        "productSearchName": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }

My query against the field indeed does not retrieve all records as expected. I indeed reindexed my whole documents on a dedicated index with the provided mapping.

Steps to reproduce: Here is my IndexDescriptor

            c => c
            .Settings(s => s.Analysis(a => a
			.Normalizers(n => n.Custom("case_insensitive", c => c.Filters("lowercase")))
            ))
            .Map<Catalog>(m => m
                .AutoMap()
                .Properties(p => p
                    .Keyword(st => st
                        .Name(n => n.ProductSearchName)
                            .Normalizer("case_insensitive")
                            )))

Expected behavior I expect the ProductSearchName field to use the provided normalizer, and then being able to query in a case insensitive way using Prefix instruction, and then get all my 3 example records.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
russcamcommented, Oct 16, 2020

No worries, @NicolasReyDotNet. You can also check to see if the index exists before attempting to create, with client.Indices.Exist()

1reaction
russcamcommented, Oct 16, 2020

Hi @NicolasReyDotNet,

Creating an index with a normalizer specified in analysis and applying to a type field works as expected. Here’s an example that’ll also log out the request and response

private static void Main()
{
	var defaultIndex = "documents";
	var pool = new SingleNodeConnectionPool(new Uri($"https://localhost:9200"));
	
	var settings = new ConnectionSettings(pool)
	    .DefaultIndex(defaultIndex)
	    .DisableDirectStreaming()
		.PrettyJson()
		.OnRequestCompleted(callDetails =>
	    {
	        if (callDetails.RequestBodyInBytes != null)
	        {
	            var json = JObject.Parse(Encoding.UTF8.GetString(callDetails.RequestBodyInBytes));
	            
	            Console.WriteLine(
	                $"{callDetails.HttpMethod} {callDetails.Uri} \n" +
	                $"{json.ToString(Newtonsoft.Json.Formatting.Indented)}");
	        }
	        else
	        {
	            Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
	        }
	
	        Console.WriteLine();
	
	        if (callDetails.ResponseBodyInBytes != null)
	        {
	            Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
	                     $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n" +
	                     $"{new string('-', 30)}\n");
	        }
	        else
	        {
	            Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
	                     $"{new string('-', 30)}\n");
	        }
	    });
	    
	var client = new ElasticClient(settings);
	
	var createIndexResponse = client.Indices.Create("foo", c => c
		.Settings(s => s.Analysis(a => a
				.Normalizers(n => n.Custom("case_insensitive", c => c.Filters("lowercase")))
				))
		.Map<Catalog>(m => m
			.AutoMap()
			.Properties(p => p
				.Keyword(st => st
					.Name(n => n.ProductSearchName)
						.Normalizer("case_insensitive")
		)))
	);

}

public class Catalog
{
	public string ProductSearchName {get;set;}
}

which yields

PUT https://localhost:9200/foo?pretty=true 
{
  "mappings": {
    "properties": {
      "productSearchName": {
        "normalizer": "case_insensitive",
        "type": "keyword"
      }
    }
  },
  "settings": {
    "analysis": {
      "normalizer": {
        "case_insensitive": {
          "filter": [
            "lowercase"
          ],
          "type": "custom"
        }
      }
    }
  }
}

It looks like a document may have been indexed into the index before the explicit index creation and mapping was applied; The productSearchName is mapped with the default inference mapping for a string that would be generated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Normalizer ” + normalizerName + ” not found for field ” + ...
A normalizer needs to be defined under the analysis settings section when creating an index. The normalizer is applied before indexing the field...
Read more >
elasticsearch - Keyword normalizer not applied on document
1 Answer. You do nothing wrong, but normalizers (and analyzer alike) will never modify your source document, only whatever is indexed from it. ......
Read more >
Unable to setup a normalizer on a keyword using dynamic ...
It works fine when I apply it to a field in the mapping definition. But as I would like this to be the...
Read more >
Can I add normalizer in runtime? - Elasticsearch
Hey guys! I want to add a new field into my existing mapping (using dynamic templates as well). As far as I understand...
Read more >
Normalizer Exclude does not apply
Hello KNIME Dev Team, I've found that the function of Exclude in Normalizer doesn't apply at all. The following are the my environment...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found