# ElasticSearch Range query with text type

**URL:** <https://discuss.elastic.co/t/elasticsearch-range-query-with-text-type/149789>\
**Category:** Elasticsearch\
**Created:** [September 25, 2018, 9:31am UTC](https://discuss.elastic.co/t/elasticsearch-range-query-with-text-type/149789 "2018-09-25T09:31:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![surajdalvi](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@surajdalvi](https://discuss.elastic.co/u/surajdalvi)\
**Post date:** [September 25, 2018, 9:31am UTC](https://discuss.elastic.co/t/elasticsearch-range-query-with-text-type/149789/1 "2018-09-25T09:31:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 27, 2018, 9:58am UTC](https://discuss.elastic.co/t/elasticsearch-range-query-with-text-type/149789/2 "2018-09-27T09:58:55Z")

</div>

> [@surajdalvi](#):
>
> What is wrong here?

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 25, 2018, 9:58am UTC](https://discuss.elastic.co/t/elasticsearch-range-query-with-text-type/149789/3 "2018-10-25T09:58:57Z")

</div>

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