Why isn't search_analyzer supported for numeric field mapping?

I understand that numeric fields are not analyzed at index time. What I was hoping to do was to use a custom analyzer at query time, always, on a numeric phone number field to strip out all non-digit characters using a char_filter.

I can, in fact, do this by specifying the analyzer in my query, and it works as expected: "123-456-7890" matches 1234567890. However, I don't want to have to ensure that's done everywhere anyone queries the field. Additionally, that doesn't help me when I do a multi_match query, for example.

Hi Justin,
Numeric fields are typically reserved for representing quantities that are optimised for range queries (money, byte sizes etc).
A telephone number is a number but as a form of identifier is perhaps best indexed as a keyword field - I doubt you'll ever need to do range queries on these numbers. Keyword fields can have a normalizer to treat them.

Thanks, you're probably right about the type. I actually did play around with making it a keyword, and it did work as desired on ingest. But, it's the query behavior that's actually my primary concern here, and keywords don't allow you to specify a search_analyzer, either.

Don't you just let the index and search time use the same normalization approach? i.e.

DELETE test
PUT test
{
  "settings": {
	"analysis": {
	  "char_filter": {
		"strip-hyphens": {
		  "type": "mapping",
		  "mappings": [
			"- => "
		  ]
		}
	  },
	  "normalizer": {
		"my_normalizer": {
		  "type": "custom",
		  "char_filter": ["strip-hyphens"]
		}
	  }
	}
  },
  "mappings": {
	"properties": {
	  "foo": {
		"type": "keyword",
		"normalizer": "my_normalizer"
	  }
	}
  }
}
POST test/_doc/1
{
  "foo": "123-456"
}
GET test/_search
{
  "size":0,
  "aggs":{
	"terms":{
	  "terms":{
		"field":"foo"
	  }
	}
  }
}

GET test/_search?q=123-456
GET test/_search?q=123-654

I thought so, but I will go back and set it up again using your example for guidance. Thanks.

I must have done something incorrectly before. This setup is working as I want now. Thanks for the help.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.