/ / Ruby on Rails - Query di ricerca per le parole all'interno del titolo: ruby, ruby-on-rails-4, search, scope, filterrific

Ruby on Rails - Query di ricerca per le parole all'interno del titolo: ruby, ruby-on-rails-4, search, scope, filterrific

Nella mia domanda sto facendo un scope/search sopra :title per un Ricerca / Filtro dei miei record. La ricerca funziona bene, l'unica cosa è che l'utente deve scrivere esattamente il title e non possono cercare la parola all'interno di :title.

Ad esempio se il title è: Questa ricerca è interessante, l'utente deve iniziare la ricerca e avere la frase completa: Questa ricerca per cercare e non possono scrivere è cool e ottenere record che hanno è cool nel titolo.

Mio scope sembra:

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
)
}

Come posso fare il mio scope/query così l'utente può cercare le parole all'interno di title e ottenere i record che contengono le parole che hanno cercato?

Ci ho provato ILIKE, ma poi la ricerca smette di funzionare in sviluppo, Penso che sia per sqlite non può ILIKE, ma in productionla ricerca ha funzionato ma non è ancora possibile cercare le parole all'interno dei titoli.

Quando lo uso LIKE, il sql la query era:

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

Mentre quando ho usato ILIKE, la query era:

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: sto usando Filterrific gem

Io uso pg gem per Production ENV & sqlite3 per Development ENV

risposte:

1 per risposta № 1

Come è descritto in questo articolo di w3schools, LIKE lavora come:

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"

Dovevo cambiare (e.gsub("*", "%") + "%").gsub(/%+/, "%"), a: ("%" + e.gsub("*", "%") + "%").gsub(/%+/, "%") .

Durante la ricerca con (e.gsub("*", "%") + "%").gsub(/%+/, "%"), il risultato sarebbe (LOWER(posts.title) ILIKE "keyword%"), mentre ("%" + e.gsub("*", "%") + "%").gsub(/%+/, "%"), darebbe (LOWER(posts.title) ILIKE "%keyword%")