我需要执行两个查询,得到这两个查询的联合,然后对合并的结果进行排序和限制。
这些查询只是术语查询:
{
"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”的术语查询不是一个选项。这是因为当按"another.field“排序时,没有任何"B”文档可以通过"yet_another_field“对结果集进行排序。
发布于 2016-03-16 23:11:25
你只需要使用一个术语查询
{
"query": {
"terms": { "a.field": ["A","B"] }
},
"sort": [
{ "another.field": {"order": "desc"}}
],
"size": 25
}
查询是:25 results
,来自按another.field
降序排序的a.field is either A or B
值的文档
https://stackoverflow.com/questions/36053480
复制