Sphinx on Rails

100 %
0 %
Information about Sphinx on Rails
Technology

Published on December 7, 2008

Author: freelancing_god

Source: slideshare.net

Description

Presentation from OSDC 2008 in Sydney, on using Sphinx with Rails. The sldes don't work that well without the talk though, but it may be useful as a reference, I guess.

Sphinx on Rails

Fast and Painless Full-Text Searching

Pat Allan http://freelancing-gods.com http://twitter.com/pat

Searching can be ugly...

SELECT * FROM users WHERE first_name LIKE '%query%' OR last_name LIKE '%query%' OR email LIKE '%query%' OR location LIKE '%query%';

SELECT * FROM users WHERE (first_name LIKE '%one%' OR first_name LIKE '%two%') OR (last_name LIKE '%one%' OR last_name LIKE '%two%') ...

... very ugly

Sphinx to the rescue!

Indexes Documents

Queries Document Indexes

Similar to: • Lucene • Xapian •Ferret •Solr

MySQL

PostgreSQL

XML

• C • Perl •C++ •PHP •C# •Python •Java •Ruby •Haskell

Ruby

Ruby on Rails

How?

acts as sphinx

Sphincter

Ultrasphinx

Thinking Sphinx

script/plugin install git://github.com/freelancing-god/ thinking-sphinx.git

class User < ActiveRecord::Base # end

class User < ActiveRecord::Base define_index do indexes first_name indexes last_name indexes email indexes location end end

rake thinking_sphinx:index rake thinking_sphinx:start

SELECT * FROM users WHERE first_name LIKE '%query%' OR last_name LIKE '%query%' OR email LIKE '%query%' OR location LIKE '%query%';

User.search quot;queryquot;

SELECT * FROM users WHERE (first_name LIKE '%one%' OR first_name LIKE '%two%') OR (last_name LIKE '%one%' OR last_name LIKE '%two%') ...

User.search quot;one twoquot;, :match_mode => :any

Wait! You forgot something...

Pagination

@users = User.search quot;queryquot;

<%= will_paginate @users %>

@users = User.search quot;queryquot;, :page => (params[:page] || 0)

Sorting

Attributes != Fields

Fields are for Searching

Attributes are for Filtering and Sorting

class User < ActiveRecord::Base define_index do # ... has created_at end end

User.search quot;Melbournequot;, :order => :created_at

class User < ActiveRecord::Base define_index do # ... indexes last_name, :sortable => true # ... end end

User.search quot;Melbournequot;, :order => :last_name

Filtering

class User < ActiveRecord::Base define_index do # ... has active end end

User.search quot;Melbournequot;, :with => {:active => 1}

User.search quot;Melbournequot;, :with => { :created_at => ( 1.year.ago..Time.now ) }

User.search :conditions => { :first_name => quot;Patquot; }

Weighting

User.search quot;Melbournequot;, :field_weights => { quot;first_namequot; => 10, quot;last_namequot; => 10, quot;locationquot; => 5 }

class User < ActiveRecord::Base define_index do # ... set_property :field_weights => { quot;first_namequot; => 10, quot;last_namequot; => 10, quot;locationquot; => 5 } end end

Indexing Everything

Sphinx lets you dig your own grave

class User < ActiveRecord::Base has_many :articles define_index do indexes articles.subject, :as => :subjects # ... end end

Complex SQL = Slower Indexing

Searching Everything

ThinkingSphinx::Search.search( quot;queryquot; )

Geo-location Searching

class User < ActiveRecord::Base define_index do # ... has lat, lng end end

rake thinking_sphinx:stop rake thinking_sphinx:index rake thinking_sphinx:start

User.search :geo => [ -0.591376, 2.638356 ]

What’s the catch?

Sphinx is a bit all-or-nothing

User.create( :first_name => quot;Patquot;, :last_name => quot;Allanquot; )

User.search quot;Patquot; #=> []

rake thinking_sphinx:index

rake thinking_sphinx:index

δ to the rescue

δelta to the rescue

class User < ActiveRecord::Base define_index do # ... set_property :delta => true end end

rake thinking_sphinx:stop rake thinking_sphinx:index rake thinking_sphinx:start

User.create( :first_name => quot;Patquot;, :last_name => quot;Allanquot; )

User.search quot;Patquot; # => [#<User ...>]

So err, what just happened?

core

δelta

core δelta

rake thinking_sphinx:index

core

Merb?

ActiveRecord

DataMapper?

Soon!

Well Soon-ish

Okay, I’ll stop talking now...

Questions?

Add a comment

Related presentations

Related pages

Thinking Sphinx on Rails 3 - Stack Overflow

Installed Thinking Sphinx via Gemfile: gem 'thinking-sphinx',: ... But when I fire up the Rails console (or my app, for that matter), I get this, ...
Read more

Thinking Sphinx on Ruby on Rails: Part 1 « full-text diary ...

Ruby on Rails (or, just Rails) is an open source web application framework. You’ve probably heard of it. In this post, we’re going to explore the ...
Read more

Home § Thinking Sphinx - Pat Allan

Thinking Sphinx What? A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration and searching.
Read more

全文搜尋 Sphinx on Rails | ihower { blogging }

Update(2009/3/31): 除了 ultrasphinx 之外,還有一套 Ruby library 是 thinking sphinx 目前已經是最被推薦的 Rails 套件 ...
Read more

Thinking Sphinx on Ruby on Rails: Part 2 « full-text diary ...

Let’s continue our Thinking Sphinx series. In the first part of this series, we got started with the rails-bootstrap example app, we generated a scaffold ...
Read more

Using Sphinx search engine in Ruby on Rails | Dmytro ...

Using Sphinx search engine in Ruby on Rails . Posted by Dmytro Shteflyuk on November 26, 2006 under Ruby & Rails. Almost all Web-applications ...
Read more

Ruby on Rails – Wikipedia

Ruby on Rails, kurz Rails, früher auch oft kurz RoR, ist ein ursprünglich von David Heinemeier Hansson in der Programmiersprache Ruby geschriebenes und ...
Read more

Sphinx Client API for Ruby | Dmytro Shteflyuk's Home

Sphinx Client API for Ruby . Posted by Dmytro Shteflyuk on April 5, 2007. The Sphinx Client API is used to communicate with searchd daemon and ...
Read more

Ruby on Rails 3: DataMapper / HAML und SASS / RubyGems ...

Michael Voigt, Stefan Tennigkeit - Ruby on Rails 3: DataMapper / HAML und SASS / RubyGems & Bundler / jetzt kaufen. 4 Kundrezensionen und 4.5 Sterne.
Read more