0

I am using High level java rest client(6.5) and I need to filter datas according to some date range - let, I have these 4 documents (added a postman image) in a same index and in 2 documents I don't have publish_date and publish_end_date field so when I will put a range I need the documents which will come in between those range and the documents which don't have those range fields also.

My requirements are matching with this question

ElasticSearch (2.2) Java filter between startDate and endDate if exists

But the QueryBuilders.missingQuery is showing undefined for QueryBuilders in 6.5.

The json documents

Is that possible?

I have modified the query like below-

when I am using only the range query like below its working fine-

    BoolQueryBuilder startDateQuery = new BoolQueryBuilder()
               .must(QueryBuilders.rangeQuery("publish_date").lte("now"));
    BoolQueryBuilder endDateQuery = new BoolQueryBuilder()
              .must(QueryBuilders.rangeQuery("publish_end_date").gte("now"));

When I am using only the mustNot query like below I am getting the desired results-

    BoolQueryBuilder startDateQuery = new BoolQueryBuilder()
               .mustNot(QueryBuilders.existsQuery("publish_date"));
    BoolQueryBuilder endDateQuery = new BoolQueryBuilder()
              .mustNot(QueryBuilders.existsQuery("publish_end_date"));

But the problem is when I mix both the queries like below I am not getting any result(getting empty array)-

    BoolQueryBuilder startDateQuery = new BoolQueryBuilder()
               .must(QueryBuilders.rangeQuery("publish_date").lte("now"))
               .mustNot(QueryBuilders.existsQuery("publish_date"));
    BoolQueryBuilder endDateQuery = new BoolQueryBuilder()
              .must(QueryBuilders.rangeQuery("publish_end_date").gte("now"))
              .mustNot(QueryBuilders.existsQuery("publish_end_date"));

1 Answer 1

0

Missing query is used in Elasticsearch 1 and 2. In Elasticsearch 5 it is deprecated.

Please see deprecated reference here

In QueryBuilders there is also an option called exist query so we can use that to search documents for which specific fields don't exist with must not boolean query. Here we have to add must not with exist query to search those field non-exist docs along with range matched docs

setRangeFieldsQuery
     .should(QueryBuilders.boolQuery()
     .mustNot(QueryBuilders.existsQuery("field_name")));

Please refer eixst query documentation here

Sign up to request clarification or add additional context in comments.

15 Comments

I have tried that one but its not working,I am not getting anything, the results which I was getting at the beginning with only the range query.
@JAVA Coder Can you add the full query you have used. Should is similar to OR condition in SQL have you passed the Range Query and Must Not Exist Query in Should then It should get all the result.
My requirement is-let, I have a content which have a publish_date(start) and publish_end date(end) and some contents doesn't have any date.so when someone search the documents he should see the contents 1> which don't have any publish_date and publish_end date and 2>the documents with current_date>publish_date 3>current_date < publish_end date.I have written the below query -
BoolQueryBuilder startDateQuery = new BoolQueryBuilder() .mustNot(QueryBuilders.existsQuery("publish_date")) .must(QueryBuilders.rangeQuery("publish_date").gte("2019-05-26T05:48:27Z").lte(null)); BoolQueryBuilder endDateQuery = new BoolQueryBuilder() .mustNot(QueryBuilders.existsQuery("publish_end_date")) .must(QueryBuilders.rangeQuery("publish_end_date").gte(null).lte("2019-07-26T05:48:27Z")); and Executed this like-BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().should(startDateQuery).should(endDateQuery);
BoolQueryBuilder wholeBoolQuery = QueryBuilders.boolQuery(); wholeBoolQuery.should(QueryBuilders.rangeQuery("publish_date").gte("2019-05-26T05:48:27Z")); wholeBoolQuery.should(QueryBuilders.rangeQuery("publish_end_date").lte("2019-07-26T05:48:27Z")); BoolQueryBuilder publishAndEndDateMustNot = QueryBuilders.boolQuery(); publishAndEndDateMustNot.must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("publish_date"))); publishAndEndDateMustNot.must(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("publish_end_date"))); wholeBoolQuery.should(publishAndEndDateMustNot);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.