这是可能的,使用过滤查询,嵌套在布尔查询。
这个例子说明了基本设置(注意不同的过滤器是如何被使用):
@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
显然可以是其他类型的查询。
谢谢!它帮助! –