我需要执行两个查询,获取这两个查询的并集,然后对组合结果进行排序和限制。
查询只是术语查询:
{
"query": {
"term": { "a.field": "A" }
},
"sort": [
{ "another.field": {"order": "desc"}}
],
"size": 25
}
我查看了dis_max查询,但查询无法单独排序和限制。
等效的 SQL 将是:
select * from (
(select * from my_table where a_field="A" order by another_field desc limit 25)
union
(select * from my_table where a_field="B" order by another_field desc limit 25)
) t order by yet_another_field limit 25;
这个查询还有另一部分在 elasticsearch 中比在关系数据库中快得多。这就是为什么我希望看到它在 elasticsearch 中工作。可能吗?
编辑
不能同时使用值为"A"和"B"的术语查询。这是因为当按"other.field"排序时,没有"B"文档可以进入结果集,以便稍后按"yet_another_field"排序。
您只需要使用术语查询
{
"query": {
"terms": { "a.field": ["A","B"] }
},
"sort": [
{ "another.field": {"order": "desc"}}
],
"size": 25
}
查询为:25 results
来自文档,其中 a.field is either A or B
的值按another.field
降序排序