gpt4 book ai didi

ruby-on-rails - Elasticsearch 不应用 NOT 过滤器

转载 作者:太空宇宙 更新时间:2023-11-03 16:52:20 25 4
gpt4 key购买 nike

今天我一直在用 Elasticsearch 碰壁,试图修复失败的测试用例。

我正在使用 Rails 3.2.14、Ruby 1.9.3、Tire gem 和 ElasticSearch 0.90.2

目标是让查询返回匹配结果 EXCLUDINGvid == "ABC123xyz"

Video 模型中的 Ruby 代码如下所示:

def related_videos(count)
Video.search load: true do
size(count)
filter :term, :category_id => self.category_id
filter :term, :live => true
filter :term, :public => true
filter :not, {:term => {:vid => self.vid}}
query do
boolean do
should { text(:_all, self.title, boost: 2) }
should { text(:_all, self.description) }
should { terms(:tags, self.tag_list, minimum_match: 1) }
end
end
end
end

Tire 生成的结果搜索查询如下所示:

{
"query":{
"bool":{
"should":[
{
"text":{
"_all":{
"query":"Top Gun","boost":2
}
}
},
{
"text":{
"_all":{
"query":"The macho students of an elite US Flying school for advanced fighter pilots compete to be best in the class, and one romances the teacher."
}
}
},
{
"terms":{
"tags":["top-gun","80s"],
"minimum_match":1
}
}
]
}
},
"filter":{
"and":[
{
"term":{
"category_id":1
}
},
{
"term":{
"live":true
}
},
{
"term":{
"public":true
}
},
{
"not":{
"term":{
"vid":"ABC123xyz"
}
}
}
]
},
"size":10
}

来自 ElasticSearch 的结果 JSON:

{
"took": 7,
"timed_out": false,
"_shards":{
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total":1,
"max_score":0.2667512,
"hits":[
{
"_index":"test_videos",
"_type":"video",
"_id":"8",
"_score":0.2667512,
"_source":{
"vid":"ABC123xyz",
"title":"Top Gun",
"description":"The macho students of an elite US Flying school for advanced fighter pilots compete to be best in the class, and one romances the teacher.",
"tags":["top-gun","80s"],
"category_id":1,
"live":true,
"public":true,
"featured":false,
"last_video_view_count":0,
"boost_factor":0.583013698630137,
"created_at":"2013-08-28T14:24:47Z"
}
}
]
}
}

谁能帮忙! Elasticsearch 的文档围绕这个主题很少,我的想法已经用完了。

谢谢

最佳答案

按照您现在的方式使用顶级过滤器不会过滤查询结果 - 它只会过滤掉诸如方面计数之类的结果。 elasticsearch documentation for filter 中有更完整的描述.

你需要做一个 filtered query这略有不同并过滤查询子句的结果:

Video.search load: true do
query do
filtered do
boolean do
should { text(:_all, self.title, boost: 2) }
should { text(:_all, self.description) }
should { terms(:tags, self.tag_list, minimum_match: 1) }
end
filter :term, :category_id => self.category_id
filter :term, :live => true
filter :term, :public => true
filter :not, {:term => {:vid => self.vid}}
end
end
end

关于ruby-on-rails - Elasticsearch 不应用 NOT 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18493005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com