试图找到范围查询的解决方案
使用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
有任何想法吗 ?
最佳答案
我会这样做:
// 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))
);