带有空日期或值的 QueryBuilders rangeQuery

QueryBuilders rangeQuery with null date or values

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

问题: DateFrom 可以为空或高于今天 DateTill 可以为空或小于今天 quantyti remaining 可以为 null 或值可以大于 0

只有这样文档才能出现在搜索结果中我正试图用

解决这个问题
         publishedFilter.should(QueryBuilders.boolQuery()
                .mustNot(QueryBuilders.existsQuery(SearchAble.QUANTITY_REMAINING)))
            .should(QueryBuilders.rangeQuery(SearchAble.QUANTITY_REMAINING).gte(1))
            .minimumShouldMatch(1);

        publishedFilter.should(QueryBuilders.boolQuery()
            .mustNot(QueryBuilders.existsQuery(SearchAble.DATE_TIME_FROM))
            .should(QueryBuilders.rangeQuery(SearchAble.DATE_TIME_FROM).lte(time))
        );

        publishedFilter.should(QueryBuilders.boolQuery()
            .mustNot(QueryBuilders.existsQuery(SearchAble.DATE_TIME_TILL))
            .should(QueryBuilders.rangeQuery(SearchAble.DATE_TIME_TILL).gte(time))
        );

但此解决方案仅适用于剩余数量而不是 dateFrom 和 dateTill

有什么想法吗?

我会这样做:

    // 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 必须是在场景

中可搜索的数据
 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());