# Unable to get gte and lte from elastic Java API in range query

**URL:** <https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781>\
**Category:** Elasticsearch\
**Created:** [December 6, 2020, 3:53pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781 "2020-12-06T15:53:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 6, 2020, 3:53pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/1 "2020-12-06T15:53:20Z")

</div>

```
Hi ,
I am using RestHighLevelclient to get data from elastic.I am using below query
BoolQueryBuilder boolQuery=QueryBuilders.boolQuery();
		boolQuery.must(QueryBuilders.rangeQuery("ipFromLong").gte(ipLong));
		boolQuery.must(QueryBuilders.rangeQuery("ipToLong").lte(ipLong));

when it is converted it is executed as
{
  "size": 10000,
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "ipFromLong": {
              "from": 2888966725,
              "to": null,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "range": {
            "ipToLong": {
              "from": null,
              "to": 2888966725,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}
so why no gte and lte in query.I tried multiple ways.still no solution.`Preformatted text`
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 6, 2020, 4:07pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/2 "2020-12-06T16:07:49Z")

</div>

Welcome!

Please format your code, logs or configuration files using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and not the citation button. It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

This is the icon to use if you are not using markdown format:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7e6e239431ec2d71cbf1beef741f2e93e7cc762c.jpg)

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.  
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.

The generated query looks good to me. Why do you think it is not correct?

BTW why 2 must clauses? I think that you can have only one, no?

---

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 6, 2020, 4:23pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/3 "2020-12-06T16:23:41Z")

</div>

I did that .please reply if you can help me.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 6, 2020, 9:27pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/4 "2020-12-06T21:27:42Z")

</div>

I already answered. Please read it again.

---

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 7, 2020, 10:48am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/5 "2020-12-07T10:48:51Z")

</div>

the query is not working and if you see there are two fields IpToLong and IPFromLong.  
and I what to apply range query on two.if given long is gte than IPFromlong and lte than IptoTolong it's not working if I use them together.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 7, 2020, 11:36am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/6 "2020-12-07T11:36:42Z")

</div>

> [@AKASH\_DUBEY1](#):
>
> the query is not working and if you see there are two fields IpToLong and IPFromLong.

Ha right. I misread the query.

> [@AKASH\_DUBEY1](#):
>
> if given long is gte than IPFromlong and lte than IptoTolong it's not working if I use them together.

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 8, 2020, 5:57am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/7 "2020-12-08T05:57:11Z")

</div>

````
Sample record
POST http://localhost:9205/iptoproxy/1
```

````

{  
"ipToLong": 28147177550,  
"ipFromLong": 28147177543,  
"ipTo": "0:0:0:0:0:ffff:4131:6d83",  
"ipFrom": "0:0:0:0:0:ffff:4131:6d83",  
"proxyType": "DCH",  
"countryCode": "JP",  
"countryName": "Japan",  
"region": "Tokyo",  
"city": "Tokyo",  
"isp": "Hurricane Electric LLC",  
"domain": "[he.net](http://he.net)",  
"proxyCatagory": "DCH",  
"catagoryDesc": "Data Center/Web Hosting/Transit",  
"ipType": null,  
"ipToProxyIndex": "iptoproxy"  
}  
```

````
POST http://localhost:9205/iptoproxy/_search
```
{
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "ipFromLong": {
              "from": 2888966725,
              "to": null,
              "include_lower": true,
              "include_upper": false,
              "boost": 1
            }
          }
        },
        {
          "range": {
            "ipToLong": {
              "from": null,
              "to": 2888966725,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}
```
My Use case is to get a record of my long value is greater than or equal to ipFromLong and less than or equal to prolong. If I create a query using Java API I get from and to instead of gte and lte which doesn't work as expected even If try using gte and lte together still it didn't work but If I try Get and let in two different queries they works without any issue. my first concern is why I don't get LTE and GTE even if I use there methods from range queries like gte(ipLong) and lte(iplong) .second why I can't use them in two caluses in same query .
````

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 8, 2020, 6:43am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/8 "2020-12-08T06:43:15Z")

</div>

You didn't define a mapping?

---

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 8, 2020, 8:22am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/9 "2020-12-08T08:22:23Z")

</div>

````
Hi ,

This is the mapping 
```
{
"iptoproxy": {
"mappings": {
"dynamic_templates": [
{
"free_as_keywords": {
"match": "free_text_*",
"match_mapping_type": "long",
"mapping": {
"copy_to": "free_text_search",
"type": "text"
}
}
}
,
{
"strings_as_keywords": {
"match": "*",
"unmatch": "free_text_*",
"match_mapping_type": "string",
"mapping": {
"copy_to": "free_text_search",
"type": "keyword"
}
}
}
,
{
"integer_as_keywords": {
"match": "*",
"match_mapping_type": "long",
"mapping": {
"copy_to": "free_text_search",
"type": "keyword"
}
}
}
,
{
"long_as_keywords": {
"match": "*",
"match_mapping_type": "long",
"mapping": {
"copy_to": "free_text_search",
"type": "keyword"
}
}
}
],
"properties": {
"catagoryDesc": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"city": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"countryCode": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"countryName": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"domain": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"free_text_search": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ipFrom": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"ipFromLong": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"ipTo": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"ipToLong": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"ipToProxyIndex": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"ipType": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"isp": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"proxyCatagory": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"proxyType": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"region": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
}
}
}
}
}
```
````

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 8, 2020, 8:35am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/10 "2020-12-08T08:35:17Z")

</div>

Your IP addresses don't have the ip datatype. You need to fix this before trying running ranges.

---

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 8, 2020, 9:04am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/11 "2020-12-08T09:04:33Z")

</div>

If you see my query. I am querying the long field instead of IP field.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 8, 2020, 9:05am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/12 "2020-12-08T09:05:47Z")

</div>

Why have you mapped this as keyword?

---

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 8, 2020, 9:17am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/13 "2020-12-08T09:17:46Z")

</div>

mapping updated with long but still not working.I tried long, keyword, double no luck.

---

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 8, 2020, 9:18am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/14 "2020-12-08T09:18:36Z")

</div>

keyword support range query.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 8, 2020, 9:48am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/15 "2020-12-08T09:48:56Z")

</div>

Yes, but it may not give the expected results as it does not compare it as a number but rather character by character. An example is that `234` \< `24` \< `256`.

---

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 8, 2020, 12:06pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/16 "2020-12-08T12:06:07Z")

</div>

`If I execute the query with one range clause I have no issue adding second returns no result But as I already mentioned I tried with long and double also for given columns still no luck.`

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 8, 2020, 12:16pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/17 "2020-12-08T12:16:29Z")

</div>

Please provide a full reproduction including mappings, data as well as the queries you have tried and expected/unexpected results.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 8, 2020, 12:31pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/18 "2020-12-08T12:31:21Z")

</div>

There is no chance that `2888966725` matches something like `0:0:0:0:0:ffff:4131:6d83`.

That's why it's failing.

Again, as Christian said and as I asked previously,

> Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

> **A full reproduction script is something anyone can copy and paste in Kibana dev console** , click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. **It will also most likely help to get a faster answer**.

It should include mappings, etc...

And please format the code, and only the code. Not your questions, your text.

---

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 8, 2020, 1:11pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/19 "2020-12-08T13:11:24Z")

</div>

> [@AKASH\_DUBEY1](#):
>
> "ipToLong": 28147177550,

please read shared data first  
POST [http://localhost:9205/iptoproxy/1](http://localhost:9205/iptoproxy/1)  
'''  
{  
"ipToLong": 28147177550,  
"ipFromLong": 28147177543,  
"ipTo": "0:0:0:0:0:ffff:4131:6d83",  
"ipFrom": "0:0:0:0:0:ffff:4131:6d83",  
"proxyType": "DCH",  
"countryCode": "JP",  
"countryName": "Japan",  
"region": "Tokyo",  
"city": "Tokyo",  
"isp": "Hurricane Electric LLC",  
"domain": "[he.net](http://he.net/)",  
"proxyCatagory": "DCH",  
"catagoryDesc": "Data Center/Web Hosting/Transit",  
"ipType": null,  
"ipToProxyIndex": "iptoproxy"  
}  
'''  
BoolQueryBuilder boolQuery=QueryBuilders.boolQuery();  
boolQuery.must(QueryBuilders.rangeQuery("ipFromLong").gte(ipLong));  
boolQuery.must(QueryBuilders.rangeQuery("ipToLong").lte(ipLong));

above fields are used  
please check the data added before replying.I have added mapping 1 record from sample ,use case,search query all in comments.  
my simplest use case is "If my long value falls between ipFromLong and ipToLong .I should get the record but its not working with two range clauses in must condition.

---

<div class="post-metadata">

**Author:** ![AKASH\_DUBEY1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akash_dubey1/32/80336_2.png) [@AKASH\_DUBEY1](https://discuss.elastic.co/u/AKASH_DUBEY1)\
**Post date:** [December 8, 2020, 1:15pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/20 "2020-12-08T13:15:36Z")

</div>

PUT [http://localhost:9205/iptoproxy](http://localhost:9205/iptoproxy)

```auto
{
"iptoproxy": {
"mappings": {
"dynamic_templates": [
{
"free_as_keywords": {
"match": "free_text_*",
"match_mapping_type": "long",
"mapping": {
"copy_to": "free_text_search",
"type": "text"
}
}
}
,
{
"strings_as_keywords": {
"match": "*",
"unmatch": "free_text_*",
"match_mapping_type": "string",
"mapping": {
"copy_to": "free_text_search",
"type": "keyword"
}
}
}
,
{
"integer_as_keywords": {
"match": "*",
"match_mapping_type": "long",
"mapping": {
"copy_to": "free_text_search",
"type": "keyword"
}
}
}
,
{
"long_as_keywords": {
"match": "*",
"match_mapping_type": "long",
"mapping": {
"copy_to": "free_text_search",
"type": "keyword"
}
}
}
],
"properties": {
"catagoryDesc": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"city": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"countryCode": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"countryName": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"domain": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"free_text_search": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ipFrom": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"ipFromLong": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"ipTo": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"ipToLong": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"ipToProxyIndex": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"ipType": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"isp": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"proxyCatagory": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"proxyType": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
},
"region": {
"type": "keyword",
"copy_to": [
"free_text_search"
]
}
}
}
}
}

```

POST [http://localhost:9205/iptoproxy/1](http://localhost:9205/iptoproxy/1)

```auto
{
"ipToLong": 28147177550,
"ipFromLong": 28147177543,
"ipTo": "0:0:0:0:0:ffff:4131:6d83",
"ipFrom": "0:0:0:0:0:ffff:4131:6d83",
"proxyType": "DCH",
"countryCode": "JP",
"countryName": "Japan",
"region": "Tokyo",
"city": "Tokyo",
"isp": "Hurricane Electric LLC",
"domain": "he.net",
"proxyCatagory": "DCH",
"catagoryDesc": "Data Center/Web Hosting/Transit",
"ipType": null,
"ipToProxyIndex": "iptoproxy"
}

```

POST [http://localhost:9205/iptoproxy/\_search](http://localhost:9205/iptoproxy/_search)

```auto
{
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "ipFromLong": {
              "from": 28147177551,
              "to": null,
              "include_lower": true,
              "include_upper": false,
              "boost": 1
            }
          }
        },
        {
          "range": {
            "ipToLong": {
              "from": null,
              "to": 28147177551,
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

```

[Next page](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781.md?page=2)
