• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Full text search on Heroku -- for FREE!
 

Full text search on Heroku -- for FREE!

on

  • 1,367 views

How to do full text search in a Ruby on Rails app on Heroku, without having to pay for their full text search add-ons, using some workarounds for problems in the pg_search gem.

How to do full text search in a Ruby on Rails app on Heroku, without having to pay for their full text search add-ons, using some workarounds for problems in the pg_search gem.

Statistics

Views

Total Views
1,367
Views on SlideShare
1,345
Embed Views
22

Actions

Likes
0
Downloads
8
Comments
0

3 Embeds 22

http://www.linkedin.com 19
http://www.hanrss.com 2
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Full text search on Heroku -- for FREE! Full text search on Heroku -- for FREE! Presentation Transcript

    • Full-Text Searchon Heroku . . .FOR FREE!(by working around bogons in pg_search)by Dave Aronson, T. Rex of Codosaurus, LLC
    • Whats the Problem?:-) Heroku gives lots of stuff for free.:-( But not its full-text search add-ons.:-) But we still have the database.:-( But using LIKE for that, sucks.:-) But PostgreSQL has full text search.:-( But that feature is a royal pain to use.:-) But the pg_search gem makes it easy.:-( But its scopes have problems.:-) But Ive figured out workarounds.
    • Example: Job Modelclass JobsController < ApplicationController def index # also serves as a search screen @jobs = Job.in_category(params[:category]). desc_has(params[:desc]). in_state(params[:state]). title_has(params[:title]) # quick and dirty example, # ignoring order and pagination endend
    • Making pg_search_scopesclass Job < ActiveRecord::Base include PgSearch attr_accessible :category, :desc, :state, :title scope :in_category, lambda { |cat| where(category: cat) if cat.present? } pg_search_scope :desc_has, against: :desc scope :in_state, lambda { |st| where(state: st) if st.present? } pg_search_scope :title_has, against: :titleend
    • Bogon #1: Not Optional- Fails SILENTLY (as far as user knows) -- no results; only clue is in logs/screen:Started GET "/jobs" [...]Processing by JobsController#index as HTMLNOTICE: text-search query doesnt contain lexemes: ""LINE 1: ...glish, coalesce("jobs"."desc"::text, ))) @@()))- "query doesnt contain lexemes" means: "I need something to search for".
    • Fix: Wrap it up, Ill take itclass Job < ActiveRecord::Base include PgSearch attr_accessible :category, :desc, :state, :title scope :in_category, lambda { |cat| where(category: cat) if cat.present? } scope :desc_has, lambda { |desc| _desc_has(desc) if desc.present? } scope :in_state, lambda { |st| where(state: st) if st.present? } scope :title_has, lambda { |ttl| _title_has(ttl) if ttl.present? }private pg_search_scope :_desc_has, against: :desc pg_search_scope :_title_has, against: :titleend
    • Bogon #2: ONE per Query!ActiveRecord::StatementInvalid in Jobs#indexShowing /Users/dave/jobboard/app/views/jobs/index.html.erb where line #49 raised:PG::Error: ERROR: ORDER BY "pg_search_rank" is ambiguousLINE 1: ...nglish, || senator || )))) ORDER BY pg_search_... ^- I didnt ask for pg_search_rank!- Look at the generated SQL: SELECT "jobs".*, ([gobbledygook]) AS pg_search_rank, "jobs".*, ([similar gobbledygook]) AS pg_search_rank FROM "jobs" WHERE ([more gobbledygook]) ORDER BY pg_search_rank DESC, "jobs"."id" ASC- It doesnt know which one we want!
    • Wrong Fix: add .orderclass JobsController < ApplicationController def index # also serves as a search screen @jobs = Job.in_category(params[:category]). desc_has(params[:desc]). in_state(params[:state]). title_has(params[:title]). order(:id) endend- Generated SQL now ends in: ORDER BY pg_search_rank DESC, "jobs"."id" ASC, id- Didnt clear; just tacked it on!
    • Right Fix: add .reorderclass JobsController < ApplicationController def index # also serves as a search screen @jobs = Job.in_category(params[:category]). desc_has(params[:desc]). in_state(params[:state]). title_has(params[:title]). reorder(:id) endend- Generated SQL now ends in: ORDER BY id- No longer barfs; query now works right.
    • SummaryTo do full-text search for free on Heroku,you can use pg_search, BUT...To make its scopes optional, wrap them innormal scopes with no-argument detectionTo use two or more in one query, tack.reorder onto the query, using anon-ambiguous column or value
    • Questions/Contact/Etc.Any questions?Contact information: YourNameHere.2.trex [at] codosaur [dot] us (yes, put your name there, without spaces, punctuation, etc.) +1-571-308-6622 http://www.codosaur.us/ (main site) http://blog.codosaur.us/ (code blog) Brought to http://www.dare2XL.com/ (excellence blog) you by Codosaurus, http://linkedin.com/in/davearonson LLC http://facebook.com/dare2xl @davearonson