【问题标题】:ElasticSearch + Rails multiple `must` not working correctlyElasticSearch + Rails 多个“必须”无法正常工作
【发布时间】:2017-10-18 13:22:49
【问题描述】:

根据文档,我使用这种语法:

self.search(
  query: {
    bool: {
      must: { match: { type: 'user' } },
      must: { match: { status: status } },
      must: { range: { created_at: { from: date_from, to: date_to } } }
    }
  },
  size: 1000
)

我认为它应该寻找所有 3 个条件,但查询忽略了所有 must 但最后一个:

<Elasticsearch::Model::Searching::SearchRequest:0x000000039b2c08 @definition= {:index=>"uusers", :type=>"user", :body=>{:query=>{:bool=>{:must=>{:range=>{:created_at=>{:from=>Mon, 25 Aug 2014, :to=>Thu, 18 May 2017}}}}}, :size=>1000}}

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby elasticsearch elasticsearch-model


    【解决方案1】:

    当您有多个语句时,您必须将其添加到数组中,如下所示:

    self.search(
      query: {
        bool: {
          must: [
            { match: { type: 'user' } },
            { match: { status: status } },
            { range: { created_at: { from: date_from, to: date_to } } }
          ]
        }
      },
      size: 1000
    )
    

    【讨论】:

    • 我确定我已经尝试过了,但我没有。谢谢,效果很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多