我正在使用pg_search
并尝试实施否定选项。
我的搜索表单包含一系列选项,其中包括选择关键字并选择不得在关键字中的字词。我获取所有搜索关键字和所有否定关键字,并为ps_search方法格式化它们。这是如何:
def self.find_words(words, not_words)
if words.present? || not_words.present?
a_not_words = not_words.gsub(/,/,"").split(" ").each{|l|l.prepend("!")}
a_words = words.gsub(/,/,"").split(" ")
a_all = a_not_words + a_words
my_words = a_all.join(" ")
pg_search_keywords(my_words)
else
order("id DESC")
end
end
这适用于简单的单词。例如,如果某人想要包含“kitchen”和“drawer”但不包含“spoon”的所有结果,则会将其发送到pg_search方法:"kitchen drawer !spoon"
但是,我的许多关键字都包含空格和停用词(法语中的“de”和“en”)。
因此,如果一个人寻找“rouleau de cuisine”(擀面杖)但不想要“assiette de table”(餐桌菜肴),这会被发送到pg_search方法:“assiette de table!rouleau!de!菜肴”。
这会产生3个问题:
1 /“de”一词在搜索词内,在否定词内 2 /这会查找关键字“table”,它不存在 - 只存在“assiette de table” 2 /我无法删除停用词“de”,因为它将开始寻找和否定不存在的术语。
更新
这就是我实现pg_search_keywords的方式:
pg_search_scope :pg_search_keywords,
:against => :summery,
:associated_against => {
:keywords => [:keyword]
},
:ignoring => :accents,
:using => {
:tsearch => {
:negation => true,
:dictionary => 'french'
}
}
谢谢!