# High level Java rest client date range greater than showing from and to in query formed

**URL:** <https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649>\
**Category:** Elasticsearch\
**Created:** [April 26, 2018, 11:19am UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649 "2018-04-26T11:19:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Omprakash\_Verma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/omprakash_verma/32/42589_2.png) [@Omprakash\_Verma](https://discuss.elastic.co/u/Omprakash_Verma)\
**Post date:** [April 26, 2018, 11:19am UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649/1 "2018-04-26T11:19:53Z")

</div>

Hi All,  
I am stucked in 1 elastic search query. I need to execute following query.

select \* from history where (start\_date is null OR start\_date \< SOME\_DATE) AND (end\_date is null OR end\_date \< SOME\_DATE).

I have added following java code to create similar query.

```auto
BoolQueryBuilder startDateBoolQuery = new BoolQueryBuilder();
BoolQueryBuilder startDateNullBoolQuery = new BoolQueryBuilder();
startDateNullBoolQuery.mustNot(QueryBuilders.existsQuery("start_datetime"));
startDateBoolQuery.should(QueryBuilders.rangeQuery("start_datetime").lte(now());
startDateBoolQuery.should(startDateNullBoolQuery);
finalBoolQuery.must(startDateBoolQuery);

BoolQueryBuilder endDateBoolQuery = new BoolQueryBuilder();
BoolQueryBuilder endDateNullBoolQuery = new BoolQueryBuilder();
endDateNullBoolQuery.mustNot(QueryBuilders.existsQuery("end_datetime"));
endDateBoolQuery.should(QueryBuilders.rangeQuery("end_datetime").gte(now());
endDateBoolQuery.should(endDateNullBoolQuery);

finalBoolQuery.must(endDateBoolQuery);

```

When I use range query it generates json like this :

```auto
"range": {
	"start_datetime": {
		"to":"now",
		"from":"now",
		"boost": 1.0
	}
}

```

---

<div class="post-metadata">

**Author:** ![Omprakash\_Verma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/omprakash_verma/32/42589_2.png) [@Omprakash\_Verma](https://discuss.elastic.co/u/Omprakash_Verma)\
**Post date:** [April 30, 2018, 7:17am UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649/2 "2018-04-30T07:17:55Z")

</div>

Any comments ?

---

<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:** [April 30, 2018, 8:11am UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649/3 "2018-04-30T08:11:53Z")

</div>

I don't understand why `end_datetime` is becoming `product.start_datetime`, why `SwishUtils.now().withZoneSameInstant(zone)` is becoming `now`.

That said I have no idea of what `SwishUtils` is.

---

<div class="post-metadata">

**Author:** ![Omprakash\_Verma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/omprakash_verma/32/42589_2.png) [@Omprakash\_Verma](https://discuss.elastic.co/u/Omprakash_Verma)\
**Post date:** [April 30, 2018, 12:31pm UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649/4 "2018-04-30T12:31:15Z")

</div>

Thanks for your response. I have updated my code and removed the utils class. Please 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:** [April 30, 2018, 2:05pm UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649/5 "2018-04-30T14:05:07Z")

</div>

What is wrong?  
And what is `now()` doing?

---

<div class="post-metadata">

**Author:** ![Omprakash\_Verma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/omprakash_verma/32/42589_2.png) [@Omprakash\_Verma](https://discuss.elastic.co/u/Omprakash_Verma)\
**Post date:** [April 30, 2018, 4:39pm UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649/6 "2018-04-30T16:39:41Z")

</div>

I am not getting records with following logic :  
startDate = null OR startDate \< now()  
AND  
endDate = null OR endDate \> now()

now() -\> Current time.

How to create this logic using java code.

---

<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:** [April 30, 2018, 6:18pm UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649/7 "2018-04-30T18:18:24Z")

</div>

Share what the json query look like, your mapping and a document which should have matched.

---

<div class="post-metadata">

**Author:** ![Omprakash\_Verma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/omprakash_verma/32/42589_2.png) [@Omprakash\_Verma](https://discuss.elastic.co/u/Omprakash_Verma)\
**Post date:** [May 1, 2018, 6:50am UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649/8 "2018-05-01T06:50:00Z")

</div>

Here is my code and json created by High level rest api 6.2.

```auto
BoolQueryBuilder startDateBoolQuery = new BoolQueryBuilder();
BoolQueryBuilder startDateNullBoolQuery = new BoolQueryBuilder();
startDateNullBoolQuery.mustNot(QueryBuilders.existsQuery("start_datetime"));
startDateBoolQuery.should(QueryBuilders.rangeQuery("start_datetime").lte(now());

```

Json

```auto
"range": {
	"start_datetime": {
		"to":"now",
		"from":"now",
		"boost": 1.0
	}
}

```

How I can get the less than or greater than query. Even specified the same into code.

---

<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:** [May 1, 2018, 7:08am UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649/9 "2018-05-01T07:08:06Z")

</div>

This is not the same query. One is a Boolean, the other one is a range.

I can't help if you don't share **all** details I asked for.

---

<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:** [May 29, 2018, 7:08am UTC](https://discuss.elastic.co/t/high-level-java-rest-client-date-range-greater-than-showing-from-and-to-in-query-formed/129649/10 "2018-05-29T07:08:13Z")

</div>

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