为了账号安全,请及时绑定邮箱和手机立即绑定

从 SQL 创建 ElasticSearch 6.5 嵌套布尔查询

从 SQL 创建 ElasticSearch 6.5 嵌套布尔查询

HUH函数 2022-05-25 16:05:24
我有这个非常简单的 SQL 查询:SELECT * FROM tableWHERE ((sphereMin>=-17 AND sphereMax<=5) OR sphereMin<=-17)AND((sphereMax<=5 AND sphereMax>=-17) OR sphereMax>=5)我必须使用高级 API 将该查询转换为 ES。这是相关代码: SearchSourceBuilder searchBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = boolQuery(); BoolQueryBuilder booleanQuery1 = boolQuery(); BoolQueryBuilder subQuery1a = boolQuery(); booleanQuery1.must(subQuery1a); RangeQueryBuilder subQuery1b = null;subQuery1a.must(rangeQuery("sphereMin").gte(-17));subQuery1a.must(rangeQuery("sphereMax").lte(5));subQuery1b = rangeQuery("sphereMin").lte(-17);booleanQuery1.should(subQuery1b);BoolQueryBuilder booleanQuery2 = boolQuery();BoolQueryBuilder subQuery2a = boolQuery();booleanQuery2.must(subQuery2a);RangeQueryBuilder subQuery2b = null;subQuery2a.must(rangeQuery("sphereMax").gte(-17));subQuery2a.must(rangeQuery("sphereMax").lte(5));subQuery2b = rangeQuery("sphereMax").gte(5);booleanQuery2.should(subQuery2b);boolQueryBuilder.filter(booleanQuery1);boolQueryBuilder.filter(booleanQuery2);searchBuilder.query(boolQueryBuilder);SearchRequest searchRequest = Requests.searchRequest(indexName).allowPartialSearchResults(true)                .source(searchBuilder).routing(routing);它看起来对我来说是正确的,但该查询不会返回与 SQL 查询相同的数据。我究竟做错了什么?
查看完整描述

2 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

在你的情况下


booleanQuery1.must(subQuery1a);

应该是


booleanQuery1.should(subQuery1a);


booleanQuery2.must(subQuery2a);

也应该是


booleanQuery2.should(subQuery2a);

例子:


qb.should(a);

qb.should(b);

qb.should(c);

默认情况下,必须至少满足以下条件之一:a OR b OR cwhile


qb.must(a);

qb.should(b);

表示 a必须匹配,而 b应该。


查看完整回答
反对 回复 2022-05-25
?
浮云间

TA贡献1829条经验 获得超4个赞

((sphereMax<=5 AND sphereMax>=-17) OR sphereMax>=-17)


ubQuery2a.must(rangeQuery("sphereMax").gte(-17));

subQuery2a.must(rangeQuery("sphereMax").lte(5));

subQuery2b = rangeQuery("sphereMax").gte(5);

5 而不是 -17


;)


查看完整回答
反对 回复 2022-05-25
  • 2 回答
  • 0 关注
  • 202 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信