Class: SearchRedux::Postgres
- Inherits:
-
Object
- Object
- SearchRedux::Postgres
- Defined in:
- lib/search_redux/rdbms/postgres.rb
Class Attribute Summary collapse
-
.sanitized_query ⇒ Object
Returns the value of attribute sanitized_query.
Class Method Summary collapse
- .compatible_search(options) ⇒ Object
- .rank_sql(rank_column, query) ⇒ Object
- .search_sql(columns) ⇒ Object
- .to_ts_query(query) ⇒ Object
Class Attribute Details
.sanitized_query ⇒ Object
Returns the value of attribute sanitized_query.
4 5 6 |
# File 'lib/search_redux/rdbms/postgres.rb', line 4 def sanitized_query @sanitized_query end |
Class Method Details
.compatible_search(options) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/search_redux/rdbms/postgres.rb', line 6 def compatible_search() query = to_ts_query([:query]) rank_query = rank_sql([:rank], query) search_query = search_sql([:columns]) ->(obj) { obj.where(Arel.sql(search_query), q: Arel.sql(query)).order(Arel.sql(rank_query)) } end |
.rank_sql(rank_column, query) ⇒ Object
19 20 21 |
# File 'lib/search_redux/rdbms/postgres.rb', line 19 def rank_sql(rank_column, query) "ts_rank(to_tsvector(#{rank_column}), to_tsquery('#{query}')) desc" end |
.search_sql(columns) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/search_redux/rdbms/postgres.rb', line 23 def search_sql(columns) template = ->(column_name) { return "to_tsvector('english', #{column_name}) @@ to_tsquery(:q)" } columns.map { |c| template.call(c) }.join(' OR ') end |
.to_ts_query(query) ⇒ Object
14 15 16 17 |
# File 'lib/search_redux/rdbms/postgres.rb', line 14 def to_ts_query(query) words = query.scan(/\s*("([^"]+)"|\w+)\s*/)&.flatten&.compact words.map { |ts| "#{ts}:*" }.join(" & ") end |