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

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.

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 :

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

Any comments ?

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.

1 Like

Thanks for your response. I have updated my code and removed the utils class. Please help me.

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

1 Like

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.

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

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

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

Json

"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.

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.

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