问题描述
Elastic Search中有多个索引,想在所有索引中搜索数据,但是我们想对不同的索引应用不同的过滤器。
We have got multiple indices in Elastic Search and would like to search the data across all indices, but we want to apply different filters on different indices.
例如:
- 很少有索引取决于
client_id
,因此需要client_id
过滤器 - 我们在少数索引中具有
is_deleted
标志,因此is_deleted
过滤器是必需的
- few indices depends on
client_id
, hence aclient_id
filter is required - we have
is_deleted
flag in few indexes, henceis_deleted
filter is required
在Elastic Search中应该如何处理?
How should one approach this in Elastic Search?
此外,我们正在使用突出显示功能,应该向用户提供建议。但我们想忽略突出显示结果中的某些字段。可以在全局级别排除某些字段吗?
Also, we are using highlight feature, which is supposed to give suggestions to the users. But we would like to ignore certain fields in the highlighted results. Is it possible to exclude certain fields at global level?
推荐答案
可以使用嵌套在布尔查询。
此示例说明了基本设置(注意如何使用不同的过滤器):
This example illustrates the basic setup (notice how different filters are used):
@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客户端一起使用,但应该可以很好地理解这个概念。 / em>
This particular code may or may not work with your ES-client, but it should give a fairly good idea of the concept.
请注意,查询喵出现了两次,您可能想使用一个变量来在两者中搜索相同的事物索引。另外, multi_match
显然可以是其他类型的查询。
Note that the query "meow" occurs twice, and you may want to use a variable instead, to search for the same thing in the two indices. Also, multi_match
could be some other type of query obviously.
这篇关于Elasticsearch全局搜索多个索引上的不同过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!