# 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:** 4\
**Page:** 2

<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, 1:30pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/21 "2020-12-08T13:30:27Z")

</div>

As far as I can see there are a couple of issues here:

- The value in the query is not in the interval specified for the example document.
- If you are looking for a range that covers the IP in the query I think you may have mixed up the `lte` and `gte` clauses and should flip them.

---

<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:35pm UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/22 "2020-12-08T20:35:31Z")

</div>

````
[quote="AKASH_DUBEY1, post:20, topic:257781"]
```
{
  "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": false,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}
```
[/quote]

````

In given query I tried all possible combination already nathing worked and ipToLong and ipForLong are just long variable then what do u mean by intervals here and again I tried all combination include\_lower and include\_upper .one more thing ,putting my point again If you run this clauses of range in two different bool queries this works and I menton again both are different fields if you run in single then doesn't so fliping should have no mean with it.

---

<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 9, 2020, 8:47am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/23 "2020-12-09T08:47:57Z")

</div>

I tried the following example based on what you wrote. That's the typical script which could help. So please, next time, provide such a script. It's easy to read and understand what you are doing.

```auto
DELETE test 
PUT test
{
  "mappings": {
    "properties": {
      "ipFromLong": {
        "type": "long"
      },
      "ipToLong": {
        "type": "long"
      }
    }
  }
}
POST test/_doc
{
  "ipToLong": 28147177550,
  "ipFromLong": 28147177543
}

```

From that example, we can test your subqueries:

```auto
GET test/_search
{
  "query": {
    "range": {
      "ipToLong": {
        "to": 28147177551
      }
    }
  }
}

```

This one gives the hit. Why this? Because for `ipToLong`: 28147177550 \< 28147177551

But:

```auto
GET test/_search
{
  "query": {
    "range": {
      "ipFromLong": {
        "from": 28147177551
      }
    }
  }
}

```

This one gives no hit. Why this? Because for `ipFromLong` 28147177543 \< 28147177551. So it can't match.

That's why your query can not match anything.

I'm now trying to understand what exactly you want to do actually.  
And I wonder if you are trying to find values in intervals. In which case, I'd highly recommend using:

> **[Range field types | Elasticsearch Reference \[7.10\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/range.html)**

---

<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:** [January 6, 2021, 8:48am UTC](https://discuss.elastic.co/t/unable-to-get-gte-and-lte-from-elastic-java-api-in-range-query/257781/24 "2021-01-06T08:48:00Z")

</div>

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

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