ElasticSearch Range query with text type

Hi there,

I have created the index by using the following mapping:

put test1
{
	"mappings": {
		"type1": {
			"properties": {
				"age": {
					"type": "text",
					"fields": {
						"raw": {
							"type": "keyword",
							"ignore_above": 32766
						}
					}
				}
			}
		}
	}
}

Added following documents into index:

PUT test1/type1/1/_create
{
  "age":50
}
PUT test1/type1/2/_create
{
  "age":100
}
PUT test1/type1/3/_create
{
  "age":150
}
PUT test1/type1/4/_create
{
  "age":200
}

I have used the following range query to fetch result:

GET test1/_search
{
    "query": {
        "range" : {
            "age" : {
                "lte" : 150
            }
        }
    }
}

It is giving me the following response :

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "test1",
        "_type": "type1",
        "_id": "2",
        "_score": 1,
        "_source": {
          "age": 100
        }
      },
      {
        "_index": "test1",
        "_type": "type1",
        "_id": "3",
        "_score": 1,
        "_source": {
          "age": 150
        }
      }
    ]
  }
}

the above response not showing document having age is 50 it is showing only age is 100 and 150. As 50 also less than 200. What is wrong here?
Can anyone help me to get a valid result?
In my schema age field type text, I don't want to change it.

How can I get a valid result?

You're comparing character strings sorted alphanumerically. 50 is greater than 150 for the same reason cat is greater than aardvaark.
You'll need to index as type integer for this to work properly.

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