试图找到范围查询的解决方案 使用QueryBuilders.rangeQuery
问题: DateFrom可以为null或大于今天 DateTill可以为null或小于今天 剩余的数量可以为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
有什么想法吗?
答案 0 :(得分: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))
);
答案 1 :(得分:0)
是的,这是个主意,但这种情况并未被要求接受,因为
如果我有一个叫他的对象,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());