我和ElasticSearch都是新手,我想知道是否有一种方法可以构造条件查询/过滤器。我正在使用Rails,所以我认为它必须在特定的级别上,因为我找不到任何指向ES级别的条件查询的东西,而且我非常肯定,假设这样做是愚蠢的!
下面是我的(工作)查询:
search_definition = {
query: {
bool: {
must: [
{
more_like_this: {
fields: tag_types,
docs: [
{
_index: self.class.index_name,
_type: self.class.document_type,
_id: id
}
],
min_term_freq: 1
}
}
],
should: [
range: {
age: {
gte: min_age,
lte: max_age,
boost: 4.0
}
}
],
filter: {
bool: {
must: [
term: {
active: true
}
],
must: [
geo_distance: {
distance: xdistance,
unit: "km",
location: {
lat: xlat,
lon: xlng
},
boost: 5.0
}
]
}
}
}
},
size: how_many
}而且效果很好。现在让我们假设我想要应用额外的过滤器,在这个特殊的例子中,我需要验证当正在搜索的用户时,另一端的用户实际上是在为搜索的人寻找一个性别的人。这被保存在数据库中的两个单独的布尔属性中(男性/女性)。我认为准备两个类似的过滤器非常简单--但是,还有一些条件过滤器会运行在查询中,最终我会得到超过10个预先准备好的过滤器。一定有一种更优雅的方式!谢谢!
发布于 2017-05-14 06:05:48
你熟悉elasticsearch search templates
使用搜索模板,您可以使用conditional和dynamic查询。例如,您可以有一个字段和值列表来进行术语筛选,并将其作为参数传递给搜索模板。
发布于 2017-05-14 14:16:07
https://stackoverflow.com/questions/43955921
复制相似问题