0

我的搜索需要结合我的用户的三个不同术语:

user_locked? || (user_expired? && !user_granted?)

这是我现在得到的,但它会引发错误消息:

query do
  bool do
    must { match_all {} }
    filter do
      bool do
        should { term(user_locked: true) }
        minimum_should_match { 1 }
      end
      bool do
        filter do
          term(user_expired: true)
          term(user_granted: false)
        end
      end
    end
  end
end

错误信息:

NoMethodError: undefined method 'filter' for #<Elasticsearch::DSL::Search::Filters::Bool

知道如何解决这个问题甚至更好的查询吗?

4

2 回答 2

1

试试这样:

query do
  bool do
    minimum_should_match 1
    should do
      term(user_locked: true)
    end
    should do
      bool do
        must do
          term(user_expired: true)
        end
        must do
          term(user_granted: false)
        end
      end
    end
  end
end

更新

根据您的第二条评论,我们需要否定您的条件,即

!(user_locked? || (user_expired? && !user_granted?))

现在相当于

!user_locked? && (!user_expired? || user_granted?)

这转化为

query do
  bool do
    must do
      term(user_locked: false)
    end
    must do
      bool do
        minimum_should_match 1
        should do
          term(user_expired: false)
        end
        should do
          term(user_granted: true)
        end
      end
    end
  end
end

该查询现在user_locked: false, user_expired: false将按预期返回文档

于 2017-03-16T11:49:53.040 回答
0

我将chewygem 与 elasticsearch 一起使用,但您可以将其用于 DSL。我没有测试过它,但我认为它应该可以工作:

   index.query(
     bool: {
       must: { match_all: {} },
         filter: {
           bool: {
             should: [
               {term: { user_locked: false}},
               bool: {
                 must: [
                   { term: { user_expired: false} },
                   { term: { user_granted: true } }
                 ]
                },
              minimum_should_match: 1
              ]
            }
          }
       }
     )
于 2017-03-16T17:17:41.810 回答