QueryBuilders rangeQuery具有空日期或指定值

时间:2019-10-01 13:24:18

标签: java elasticsearch search

试图使用QueryBuilders.rangeQuery找到范围查询的解决方案

问题:DateFrom可以为null或大于今天DateTill可以为null或小于今天剩余数量可以为null或值可以大于0

只有文档必须出现在我试图解决此问题的搜索结果中

// at least one of the OR conditions must match
    publishedFilter.minimumShouldMatch(1);

    // OR condition for Quantity
    publishedFilter.should(QueryBuilders.boolQuery()
        .should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(SearchAble.QUANTITY_REMAINING)))
        .should(QueryBuilders.rangeQuery(SearchAble.QUANTITY_REMAINING).gte(1))
    );

    // OR condition for Date From
    publishedFilter.should(QueryBuilders.boolQuery()
        .should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(SearchAble.DATE_TIME_FROM)))
        .should(QueryBuilders.rangeQuery(SearchAble.DATE_TIME_FROM).lte(time))
    );

    // OR condition for Date Till
    publishedFilter.should(QueryBuilders.boolQuery()
        .should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(SearchAble. DATE_TIME_TILL)))
        .should(QueryBuilders.rangeQuery(SearchAble. DATE_TIME_TILL).gte(time))
    );

这是一个主意,但是这种情况下它并没有被要求接受,因为如果我有一个对象叫他searchDto必须在场景中可以搜索数据

//searchable data 
 searchDto.setQuantityRemaining(0);

 searchDto.setQuantityRemaining(1);
        searchDto.setDateTimeRange(DateTimeRangeDto.builder()
            .dateTimeFrom(null)
            .dateTimeTill(null)
            .build());

        searchDto.setQuantityRemaining(null);
        searchDto.setDateTimeRange(DateTimeRangeDto.builder()
            .dateTimeFrom(null)
            .dateTimeTill(null)
            .build());

        searchDto.setQuantityRemaining(null);
        searchDto.setDateTimeRange(DateTimeRangeDto.builder()
            .dateTimeFrom(OffsetDateTime.now())
            .dateTimeTill(null)
            .build());

        searchDto.setQuantityRemaining(2);
        searchDto.setDateTimeRange(DateTimeRangeDto.builder()
            .dateTimeFrom(OffsetDateTime.now())
            .dateTimeTill(OffsetDateTime.now().plusHours(1))
            .build());



        // and not searchable

        searchDto.setQuantityRemaining(0);
        searchDto.setDateTimeRange(DateTimeRangeDto.builder()
            .dateTimeFrom(OffsetDateTime.now().plusHours(1))
            .dateTimeTill(OffsetDateTime.now())
            .build());

0 个答案:

没有答案