如何在 elasticsearch 中组合 3 个术语?
How to combine 3 terms in elasticsearch?
我的搜索需要结合我的用户的三个不同术语:
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
知道如何解决这个问题或者更好的查询吗?
这样试试:
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
该查询现在 return 文档 user_locked: false, user_expired: false
符合预期
我将 chewy
gem 与 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
]
}
}
}
)
我的搜索需要结合我的用户的三个不同术语:
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
知道如何解决这个问题或者更好的查询吗?
这样试试:
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
该查询现在 return 文档 user_locked: false, user_expired: false
符合预期
我将 chewy
gem 与 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
]
}
}
}
)