Why Elastic Search allow to put number in text field?

Hi,

I have mapping like this:

     "mappings": {
                            "dynamic": "strict",
                            "properties": {
                              "name": {
                                "type": "text"
                              }
                            }}

when I add new document:

POST /my_index/_bulk
{ "index":{} }
{"name":123}

everything is fine and result of search is:

   "hits": [
      {
        "_index": "my_index",
        "_id": "gUuDNooBpc7sew2t4t-R",
        "_score": 1,
        "_source": {
          "name": 123
        }
      },
      {
        "_index": "my_index",
        "_id": "gkuGNooBpc7sew2tud_d",
        "_score": 1,
        "_source": {
          "name": 123
        }
      },
      {
        "_index": "my_index",
        "_id": "g0uHNooBpc7sew2tL9_4",
        "_score": 1,
        "_source": {
          "name": 123
        }
      }

Question

How to make ES to not accept incorrect type data?
So if field is text, numbers should be rejected

I don't think you can because the data type is not incorrect for elasticsearch.

Mapping a field as text or keyword for example doesn't mean that it will only accept text characters, it means that the value of the field will be treated as a string even if it is a number.

In your example, the value 123 is not a numeric value for elasticsearch, you can't do numeric operations with the field name.

So you can't make elasticsearch reject numeric values for text/keyword fields, if this is something that youe use case needs, then you will need to validate it before sending the data to Elasticsearch.

1 Like

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