一尘不染

ElasticSearch(2.2)startDate和endDate之间的Java过滤器(如果存在)

elasticsearch

我想在Elasticsearch(Java客户端)中使用以下过滤器:

如果存在startDate并降低到现在

如果endDate存在且现在更大

我已经有以下内容,但没有显示没有startDate或endDate的内容:

       //Start date end date

        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        queryBuilder.filter(QueryBuilders.rangeQuery("startDate").lte("now"));
        queryBuilder.should(QueryBuilders.existsQuery("startDate"));
        queryBuilder.filter(QueryBuilders.rangeQuery("endDate").gte("now"));
        queryBuilder.should(QueryBuilders.existsQuery("endDate"));

        boolQueryBuilder.filter(queryBuilder);

mysql中的查询如下所示:

SELECT * FROM customer_job WHERE (start_date <= CURDATE() OR start_date IS NULL) AND (end_date>=CURDATE() OR end_date IS NULL);

那么该怎么做呢?


我尝试从答案中追随,但仍然没有显示没有startDate或endDate的人:

BoolQueryBuilder startDateQuery = new BoolQueryBuilder()
                .should(QueryBuilders.existsQuery("startDate"))
                .should(QueryBuilders.rangeQuery("startDate").lte("now"))
                .minimumNumberShouldMatch(1);
        BoolQueryBuilder endDateQuery = new BoolQueryBuilder()
                .should(QueryBuilders.existsQuery("endDate"))
                .should(QueryBuilders.rangeQuery("endDate").gte("now"))
                .minimumNumberShouldMatch(1);

        boolQueryBuilder.filter(startDateQuery).filter(endDateQuery);

阅读 286

收藏
2020-06-22

共1个答案

一尘不染

您需要这样的查询:

BoolQueryBuilder startDateQuery = new BoolQueryBuilder()
   .should(QueryBuilders.missingQuery("startDate"))
   .should(QueryBuilders.rangeQuery("startDate").lte("now"))
   .minimumShouldMatch(1);
BoolQueryBuilder endDateQuery = new BoolQueryBuilder()
   .should(QueryBuilders.missingQuery("endDate"))
   .should(QueryBuilders.rangeQuery("endDate").gte("now"))
   .minimumShouldMatch(1);
BoolQueryBuilder queryBuilder = new BoolQueryBuilder()
   .filter(startDateQuery)
   .filter(endDateQuery);
2020-06-22