这是可能的,使用过滤查询,嵌套在布尔查询中。
此示例说明了基本设置(注意如何使用不同的过滤器):
@results = elastic_client.search([:dogs, :cats], {
:bool => {
:should => [
# cats
{
:filtered => {
:query => {
:multi_match => {
:query => 'meow', # repeated, replace with a variable
:type => 'phrase_prefix',
:fields => ['name', 'age']
}
},
:filter => {
:and => [
{ :term => { :owner_id => '123' } },
{ :type => { :value => 'cat' } }
]
}
}
},
# dogs
{
:filtered => {
:query => {
:multi_match => {
:query => 'meow', # repeated, replace with a variable
:type => 'phrase_prefix',
:fields => ['name', 'color']
}
},
:filter => {
:and => [
{ :term => { :kennel_id => '456' } },
{ :type => { :value => 'dog' } }
]
}
}
}
]
}
})
此特定代码可能适用于您的 ES 客户端,也可能不适用,但它应该可以很好地了解这个概念。
请注意,查询“meow”出现了两次,您可能想改用变量来在两个索引中搜索相同的内容。此外,multi_match 显然可能是其他类型的查询。