【问题标题】:Ruby on Rails - Search Query for words within the titleRuby on Rails - 搜索查询标题中的单词
【发布时间】:2017-05-18 10:26:08
【问题描述】:

在我的应用程序中,我正在对:title 执行scope/search搜索/过滤我的记录。搜索本身工作正常,唯一的问题是用户需要准确地编写 title 并且他们无法在 :title 中搜索单词。

例如如果title是: 这个搜索很酷,用户需要开始搜索并有完整的句子:这个搜索 strong> 进行搜索,他们不能写 iscool 并获取标题中有 iscool 的记录。

我的scope 看起来像:

class Post < ActiveRecord::Base

  scope :search_query, lambda { |query|
    return nil  if query.blank?
    # condition query, parse into individual keywords
    terms = query.downcase.split(/\s+/)
    # replace "*" with "%" for wildcard searches,
    # append '%', remove duplicate '%'s
    terms = terms.map { |e|
      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }
    # configure number of OR conditions for provision
    # of interpolation arguments. Adjust this if you
    # change the number of OR conditions.
    num_or_conditions = 1
    where(
        terms.map {
          or_clauses = [
              "LOWER(posts.title) LIKE ?"
          ].join(' OR ')
          "(#{ or_clauses })"
        }.join(' AND '),
        *terms.map { |e| [e] * num_or_conditions }.flatten
    )
  }

如何创建我的scope/query,以便用户可以在title 中搜索单词并获取包含他们搜索过的单词的记录?

我尝试使用ILIKE,但随后搜索停止工作正在开发中,我认为这是因为sqlite 不能有ILIKE,但在production搜索工作但仍然无法搜索标题中的单词。

当我使用LIKE 时,sql 查询是:

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

当我使用ILIKE 时,查询是:

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:我使用的是Filterrific gem

我将pg gem 用于Production ENVsqlite3 用于Development ENV

【问题讨论】:

  • 看一下我说,你应该对 pg 和 sqlite3 使用 ILIKE
  • 谢谢@marmeladze,我试过ILIKE ,但是整个搜索都不起作用。甚至以前有效的方法。
  • @user6589814 “那么整个搜索都不起作用。即使以前起作用” -- 嗯??你能找出之前和之后生成了什么SQL吗?我不明白这怎么可能破坏你的代码。
  • @TomLord 我用生成的 SQL 查询更新了问题。看起来ILIKE 不仅在Development ENV 中有效,但在Production ENV 中没有问题,但即使在ILIKEProduction ENV 中我仍然无法在标题中搜索单词

标签: ruby ruby-on-rails-4 search scope filterrific


【解决方案1】:

正如w3schools article 中所述,LIKE 的作用是:

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(/%+/, '%') 搜索时,结果为(LOWER(posts.title) ILIKE 'keyword%'),而('%' + e.gsub('*', '%') + '%').gsub(/%+/, '%') 则为(LOWER(posts.title) ILIKE '%keyword%')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2010-12-18
    • 1970-01-01
    • 2015-07-19
    • 2010-11-05
    • 2014-06-06
    • 2023-03-12
    相关资源
    最近更新 更多