我使用gem elasticsearch-rails和elasticsearch-model,我很难用elasticsearch-rails编写这个查询。
SELECT "news".* FROM "news"
WHERE "news"."is_active" = 'true' AND
((priority is not null AND created_at > '2014-07-08 08:55:52.888587') OR
(created_at > '2014-07-08 08:55:52.888820' AND is_persisted = 't') )
ORDER BY "news"."priority" ASC, "news"."created_at" DESC
LIMIT 10 OFFSET 0在我之前的项目中,我使用了“轮胎模型”,我使用了类似这样的东西:filter :bool, must: {and: [{term: {field with options}}, {term: {field with options}}]},它适用于轮胎模型
但是如果我在elasticsearch-rails中使用这样的东西,它会抛出丢失的过滤错误。我写了这样的代码来过滤活动记录:
def self.news_index(page = 1)
query =:
response=self.elasticsearch.search query: { match: { is_active: true }}
response=response.page(page)
end在上面的方法中,我想添加带有bool选项的组合过滤器。有人能给我指路吗?
发布于 2014-08-12 16:57:01
当涉及到查询时,Elasticsearch-ruby更接近于elasticsearch DSL。大多数情况下,您会将散列(或类似散列的对象)传递给query方法。
像这样的东西应该会让你更接近:
self.search query: {
filtered: {
query: { match: { is_active: true }},
filter: {
bool: {
must: {
and: [{term: {field with options}}, {term: {field with options}}]
}
}
}
}
}不同之处在于,filter不是轮胎查询中的方法调用,它接受参数:bool和过滤器。现在,您需要指定一个具有散列值的:filter密钥,然后该散列值包含一个使用现有过滤器作为值的:bool密钥。
https://stackoverflow.com/questions/25258835
复制相似问题