首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何在Elasticsearch-rails和Elasticsearch-model中表达这个SQL?

我如何在Elasticsearch-rails和Elasticsearch-model中表达这个SQL?
EN

Stack Overflow用户
提问于 2014-08-12 15:47:10
回答 1查看 504关注 0票数 0

我使用gem elasticsearch-rails和elasticsearch-model,我很难用elasticsearch-rails编写这个查询。

代码语言:javascript
复制
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中使用这样的东西,它会抛出丢失的过滤错误。我写了这样的代码来过滤活动记录:

代码语言:javascript
复制
def self.news_index(page = 1)
  query =:
  response=self.elasticsearch.search query: { match: { is_active: true }}
  response=response.page(page)
end

在上面的方法中,我想添加带有bool选项的组合过滤器。有人能给我指路吗?

EN

回答 1

Stack Overflow用户

发布于 2014-08-12 16:57:01

当涉及到查询时,Elasticsearch-ruby更接近于elasticsearch DSL。大多数情况下,您会将散列(或类似散列的对象)传递给query方法。

像这样的东西应该会让你更接近:

代码语言:javascript
复制
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密钥。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25258835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档