RubyonRails-搜索标题中的单词
在我的应用程序中,我正在RubyonRails-搜索标题中的单词,ruby,ruby-on-rails-4,search,scope,filterrific,Ruby,Ruby On Rails 4,Search,Scope,Filterrific,在我的应用程序中,我正在:title上执行范围/搜索,以搜索/筛选我的记录。搜索本身工作正常,唯一的问题是用户需要准确地编写标题&他们不能在:标题中搜索单词 例如,如果标题是:此搜索很酷,用户需要开始搜索并拥有完整的句子:此搜索要搜索,他们不能写入是酷并获取标题中包含是酷的记录 我的范围看起来像: 当我使用ILIKE时,查询是: SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "posts" WHERE ((LOWE
:title
上执行范围/搜索
,以搜索/筛选我的记录。搜索本身工作正常,唯一的问题是用户需要准确地编写标题
&他们不能在:标题
中搜索单词
例如,如果标题是:此搜索很酷,用户需要开始搜索并拥有完整的句子:此搜索要搜索,他们不能写入是酷并获取标题中包含是酷的记录
我的范围
看起来像:
当我使用ILIKE时,查询是:
SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "posts" WHERE ((LOWER(posts.title) LIKE 'rails%')) LIMIT 50 OFFSET 0) subquery_for_count
SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "posts" WHERE ((LOWER(posts.title) ILIKE 'rails%')) LIMIT 50 OFFSET 0) subquery_for_count
SQLite3::SQLException: near "ILIKE": syntax error: SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "posts" WHERE ((LOWER(posts.title) ILIKE 'rails%')) LIMIT 50 OFFSET 0) subquery_for_count
ps:Im使用Filterrific gem
我将pg gem
用于Production ENV
,sqlite3
用于Development ENV
,正如本文所述,类似于的功能如下:
WHERE CustomerName LIKE 'a%' => Finds any values that starts with "a"
WHERE CustomerName LIKE '%a' => Finds any values that ends with "a"
WHERE CustomerName LIKE '%or%' => Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' => Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%_%' => Finds any values that starts with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' => Finds any values that starts with "a" and ends with "o"
我需要将(e.gsub('*','%')+'%').gsub(/%+/,'%')
,更改为:('%'+e.gsub('*','%')+'')).gsub(/%+/,'%'))
当使用(e.gsub('*','%')+'%').gsub(/%+/,'%')
搜索时,结果将是(较低的(posts.title)ILIKE“keyword%”)
,其中as(''%'+e.gsub('*','%')+'%')).gsub(/%+/+/+,'%')
,将提供(较低的(posts.title)ILIKE“keyword%%”)
,快速查看我说,你应该在pg和sqlite3Thanks@marmeladze中使用ILIKE,我尝试了ILIKE
,但是整个搜索都不起作用。即使是以前有用的。可能是useful@user6589814“那么整个搜索都不起作用了。即使是以前起作用的东西”——嗯??您能找出前后生成了什么SQL吗?我不明白这怎么可能会破坏您的代码。@TomLord我用生成的SQL查询更新了问题。看起来,ILIKE
不仅在Development ENV
中有效,但在Production ENV
中没有问题,但即使在Production ENV
中使用ILIKE
,我仍然无法在标题中搜索单词
WHERE CustomerName LIKE 'a%' => Finds any values that starts with "a"
WHERE CustomerName LIKE '%a' => Finds any values that ends with "a"
WHERE CustomerName LIKE '%or%' => Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' => Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%_%' => Finds any values that starts with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' => Finds any values that starts with "a" and ends with "o"