gpt4 book ai didi

ruby-on-rails - 将前缀匹配添加到 pg_search

转载 作者:行者123 更新时间:2023-11-29 11:38:27 25 4
gpt4 key购买 nike

我正在关注 this Railscasts 插曲。

如果我搜索“Kerber”,它会返回正确的文章。但是,如果我搜索“Ke”,它不会返回同一篇文章。

有办法解决这个问题吗?

class Item < ActiveRecord::Base
include PgSearch
pg_search_scope :search, against: [:description, :about, :link, :twitterhandle, :builtby],
using: {tsearch: {dictionary: "english"}}

def self.text_search(query)
if query.present?
search(query)
else
scoped
end
end

最佳答案

我是 pg_search 的作者和维护者。

您可以将 prefix: true 添加到 :tsearch 搜索功能的配置中,让 pg_search 自动将 :* 添加到您的查询。

https://github.com/Casecommons/pg_search#prefix-postgresql-84-and-newer-only

class Item < ActiveRecord::Base
include PgSearch
pg_search_scope :search, against: [:description, :about, :link, :twitterhandle, :builtby],
using: {tsearch: {prefix: true, dictionary: "english"}}

def self.text_search(query)
if query.present?
search(query)
else
scoped
end
end
end

关于ruby-on-rails - 将前缀匹配添加到 pg_search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16129946/

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