Top Banner
Solving the Riddle of Search Using Sphinx with Rails
283

Solving the Riddle of Search: Using Sphinx with Rails

May 11, 2015

Download

Technology

freelancing_god

An introduction to using the Sphinx search service and the Thinking Sphinx library for Ruby and Rails.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Solving the Riddle of Search: Using Sphinx with Rails

Solving the Riddle of Search

Using Sphinx with Rails

Page 2: Solving the Riddle of Search: Using Sphinx with Rails

My name is Pat

Page 4: Solving the Riddle of Search: Using Sphinx with Rails

Questions?

Page 5: Solving the Riddle of Search: Using Sphinx with Rails

Within Current Context?

Page 6: Solving the Riddle of Search: Using Sphinx with Rails

Ask as we go

Page 7: Solving the Riddle of Search: Using Sphinx with Rails

Jumping topics?

Page 8: Solving the Riddle of Search: Using Sphinx with Rails

Please Wait

Page 9: Solving the Riddle of Search: Using Sphinx with Rails

A Tute inTwo Parts

Page 10: Solving the Riddle of Search: Using Sphinx with Rails

Part One:Core Concepts

Page 11: Solving the Riddle of Search: Using Sphinx with Rails

(ie: theboring bits)

Page 12: Solving the Riddle of Search: Using Sphinx with Rails

Part Two: Beyond the

Basics

Page 13: Solving the Riddle of Search: Using Sphinx with Rails

(ie: thefun bits)

Page 14: Solving the Riddle of Search: Using Sphinx with Rails

So...

Page 15: Solving the Riddle of Search: Using Sphinx with Rails

Part One:Core Concepts

Page 16: Solving the Riddle of Search: Using Sphinx with Rails

What is Sphinx?

Page 17: Solving the Riddle of Search: Using Sphinx with Rails

Open Source Search Service

Page 18: Solving the Riddle of Search: Using Sphinx with Rails

http://www.sphinxsearch.com/

Page 19: Solving the Riddle of Search: Using Sphinx with Rails

Similar to Ferret, Solr, Xapian, etc

Page 20: Solving the Riddle of Search: Using Sphinx with Rails

Talks to MySQL and PostgreSQL

Page 21: Solving the Riddle of Search: Using Sphinx with Rails

... and Microsoft SQL Server and Oracle in 0.9.9

Page 22: Solving the Riddle of Search: Using Sphinx with Rails

Two Key Tools

Page 23: Solving the Riddle of Search: Using Sphinx with Rails

indexer

Page 24: Solving the Riddle of Search: Using Sphinx with Rails

Guess whatit does

Page 25: Solving the Riddle of Search: Using Sphinx with Rails

Talks to your database

Page 26: Solving the Riddle of Search: Using Sphinx with Rails

Files awayyour data

Page 27: Solving the Riddle of Search: Using Sphinx with Rails

searchd

Page 28: Solving the Riddle of Search: Using Sphinx with Rails

No prizes for guessing this

either.

Page 29: Solving the Riddle of Search: Using Sphinx with Rails

searchd = search daemon

Page 30: Solving the Riddle of Search: Using Sphinx with Rails

Runs in the Background

Page 31: Solving the Riddle of Search: Using Sphinx with Rails

Handles Search Requests

Page 32: Solving the Riddle of Search: Using Sphinx with Rails

Does not talk to your database.

Page 33: Solving the Riddle of Search: Using Sphinx with Rails

What gets indexed?

Page 34: Solving the Riddle of Search: Using Sphinx with Rails

Documents

Page 35: Solving the Riddle of Search: Using Sphinx with Rails

No, not Microsoft Word docs

Page 36: Solving the Riddle of Search: Using Sphinx with Rails

A document is a single database

record

Page 37: Solving the Riddle of Search: Using Sphinx with Rails

(ie: a Rails model instance)

Page 38: Solving the Riddle of Search: Using Sphinx with Rails

Documents have fields and attributes

Page 39: Solving the Riddle of Search: Using Sphinx with Rails

They are notthe same.

Page 40: Solving the Riddle of Search: Using Sphinx with Rails

THEY ARE NOTTHE SAME.

Page 41: Solving the Riddle of Search: Using Sphinx with Rails

Fields are...

Page 42: Solving the Riddle of Search: Using Sphinx with Rails

Textual Data

Page 43: Solving the Riddle of Search: Using Sphinx with Rails

Strings

Page 44: Solving the Riddle of Search: Using Sphinx with Rails

Words tosearch for.

Page 45: Solving the Riddle of Search: Using Sphinx with Rails

Attributes, on the other hand...

Page 46: Solving the Riddle of Search: Using Sphinx with Rails

Integers

Page 47: Solving the Riddle of Search: Using Sphinx with Rails

Floats

Page 48: Solving the Riddle of Search: Using Sphinx with Rails

Timestamps

Page 49: Solving the Riddle of Search: Using Sphinx with Rails

Booleans

Page 50: Solving the Riddle of Search: Using Sphinx with Rails

... and Strings. Kinda.

Page 51: Solving the Riddle of Search: Using Sphinx with Rails

Strings as Ordinals

Page 52: Solving the Riddle of Search: Using Sphinx with Rails

Huh?

Page 53: Solving the Riddle of Search: Using Sphinx with Rails

Integer values indicating

alphabetical order of strings.

Page 54: Solving the Riddle of Search: Using Sphinx with Rails

Useless for Searching?

Page 55: Solving the Riddle of Search: Using Sphinx with Rails

Useful for Sorting though.

Page 56: Solving the Riddle of Search: Using Sphinx with Rails

Search queries don’t look at

attribute values.

Page 57: Solving the Riddle of Search: Using Sphinx with Rails

Sorting

Page 58: Solving the Riddle of Search: Using Sphinx with Rails

Grouping

Page 59: Solving the Riddle of Search: Using Sphinx with Rails

Filtering

Page 60: Solving the Riddle of Search: Using Sphinx with Rails

Exact value matching

Page 61: Solving the Riddle of Search: Using Sphinx with Rails

So, that’s Sphinx...

Page 62: Solving the Riddle of Search: Using Sphinx with Rails

But what’s Thinking Sphinx?

Page 63: Solving the Riddle of Search: Using Sphinx with Rails

Ruby Library/Rails Plugin

Page 64: Solving the Riddle of Search: Using Sphinx with Rails

Hooks into ActiveRecord

Page 65: Solving the Riddle of Search: Using Sphinx with Rails

Configure Sphinx with

Ruby and YAML

Page 66: Solving the Riddle of Search: Using Sphinx with Rails

Search with Sphinx in Rails

Page 67: Solving the Riddle of Search: Using Sphinx with Rails

What’s in it for me though?

Page 68: Solving the Riddle of Search: Using Sphinx with Rails

Why should Iuse Sphinx?

Page 69: Solving the Riddle of Search: Using Sphinx with Rails

SQL does it all, right?

Page 70: Solving the Riddle of Search: Using Sphinx with Rails

Well, maybe

Page 71: Solving the Riddle of Search: Using Sphinx with Rails

But it gets messy

Page 72: Solving the Riddle of Search: Using Sphinx with Rails

• Find all people

• Where any word matches

• Part of any of four fields

Page 73: Solving the Riddle of Search: Using Sphinx with Rails

# given a query of ‘Pat Allan’# assuming MySQL

SELECT * FROM peopleWHERE first_name LIKE '%Pat%' OR first_name LIKE '%Allan%' OR last_name LIKE '%Pat%' OR last_name LIKE '%Allan%' OR location LIKE '%Pat%' OR location LIKE '%Allan%' OR profile LIKE '%Pat%' OR profile LIKE '%Allan%';

Page 74: Solving the Riddle of Search: Using Sphinx with Rails

It’s a bit long...

Page 75: Solving the Riddle of Search: Using Sphinx with Rails

And that’s a simple query!

Page 76: Solving the Riddle of Search: Using Sphinx with Rails

If we use Thinking Sphinx

Page 77: Solving the Riddle of Search: Using Sphinx with Rails

# given a query of ‘Pat Allan’

Person.search "Pat Allan", :match_mode => :any

Page 78: Solving the Riddle of Search: Using Sphinx with Rails

Much cleaner

Page 79: Solving the Riddle of Search: Using Sphinx with Rails

Can we start coding now?

Page 80: Solving the Riddle of Search: Using Sphinx with Rails

Not quite.

Page 81: Solving the Riddle of Search: Using Sphinx with Rails

Indexing

Page 82: Solving the Riddle of Search: Using Sphinx with Rails

Fields

Page 83: Solving the Riddle of Search: Using Sphinx with Rails

What textual data do we want

indexed?

Page 84: Solving the Riddle of Search: Using Sphinx with Rails

Enter theindexesmethod

Page 85: Solving the Riddle of Search: Using Sphinx with Rails

class Person < ActiveRecord::Base # ... define_index do indexes first_name, last_name, location, profile end # ...end

Page 86: Solving the Riddle of Search: Using Sphinx with Rails

# Combine columns into one fieldindexes [first_name, last_name], :as => :name

# Avoid core Ruby class methodsindexes :name

# Use association columnsindexes photos.captions, :as => :photos

Page 87: Solving the Riddle of Search: Using Sphinx with Rails

Attributes

Page 88: Solving the Riddle of Search: Using Sphinx with Rails

What do we want to sort and filter

by?

Page 89: Solving the Riddle of Search: Using Sphinx with Rails

Our friend thehas

method

Page 90: Solving the Riddle of Search: Using Sphinx with Rails

class Person < ActiveRecord::Base # ... define_index do # ... has created_at, admin, email, :id end # ...end

Page 91: Solving the Riddle of Search: Using Sphinx with Rails

# Multi-Value Attributes (MVAs)has tags.id, :as => :tag_ids

Page 92: Solving the Riddle of Search: Using Sphinx with Rails

What if I want to sort by a field?

Page 93: Solving the Riddle of Search: Using Sphinx with Rails

indexes first_name, last_name, :sortable => true

Page 94: Solving the Riddle of Search: Using Sphinx with Rails

What does all this code do?

Page 95: Solving the Riddle of Search: Using Sphinx with Rails

Defines a SQL query for Sphinx

Page 96: Solving the Riddle of Search: Using Sphinx with Rails

Complexity leads to slower

indexing

Page 97: Solving the Riddle of Search: Using Sphinx with Rails

So don’t forget to add database indexes as well!

Page 98: Solving the Riddle of Search: Using Sphinx with Rails

Warning for fans of fixtures...

Page 99: Solving the Riddle of Search: Using Sphinx with Rails

# In your define_index block:set_property( :sql_range_step => 10_000_000)

Page 100: Solving the Riddle of Search: Using Sphinx with Rails

Because it’s SQL...

Page 101: Solving the Riddle of Search: Using Sphinx with Rails

Can only usemodel columns,

not model methods

Page 102: Solving the Riddle of Search: Using Sphinx with Rails

Maybe that’s not good enough?

Page 103: Solving the Riddle of Search: Using Sphinx with Rails

Write yourown SQL!

Page 104: Solving the Riddle of Search: Using Sphinx with Rails

has "YEAR(created_at)", :as => :year, :type => :integer

Page 105: Solving the Riddle of Search: Using Sphinx with Rails

So, we’ve set up our index.Now what?

Page 106: Solving the Riddle of Search: Using Sphinx with Rails

rake thinking_sphinx:index

Page 107: Solving the Riddle of Search: Using Sphinx with Rails

(in /Users/pat/Code/ruby/ts_test_2.2.0)Generating Configuration to /Users/pat/Code/ruby/ts_test_2.2.0/config/development.sphinx.confindexer --config /Users/pat/Code/ruby/ts_test_2.2.0/config/development.sphinx.conf --allSphinx 0.9.8-release (r1371)Copyright (c) 2001-2008, Andrew Aksyonoff

using config file '/Users/pat/Code/ruby/ts_test_2.2.0/config/development.sphinx.conf'...indexing index 'person_core'...collected 1000 docs, 0.0 MBcollected 0 attr valuessorted 0.0 Mvalues, 100.0% donesorted 0.0 Mhits, 100.0% donetotal 1000 docs, 14436 bytestotal 0.191 sec, 75523.42 bytes/sec, 5231.60 docs/secindexing index 'person_delta'...collected 0 docs, 0.0 MBcollected 0 attr valuessorted 0.0 Mvalues, nan% donetotal 0 docs, 0 bytestotal 0.010 sec, 0.00 bytes/sec, 0.00 docs/secdistributed index 'person' can not be directly indexed; skipping.

Page 108: Solving the Riddle of Search: Using Sphinx with Rails

rake thinking_sphinx:start

Page 109: Solving the Riddle of Search: Using Sphinx with Rails

(in /Users/pat/Code/ruby/ts_test_2.2.0)searchd --pidfile --config /Users/pat/Code/ruby/ts_test_2.2.0/config/development.sphinx.confSphinx 0.9.8-release (r1371)Copyright (c) 2001-2008, Andrew Aksyonoff

using config file '/Users/pat/Code/ruby/ts_test_2.2.0/config/development.sphinx.conf'...Started successfully (pid 6220).

Page 110: Solving the Riddle of Search: Using Sphinx with Rails

rake thinking_sphinx:stop

Page 111: Solving the Riddle of Search: Using Sphinx with Rails

(in /Users/pat/Code/ruby/ts_test_2.2.0)Sphinx 0.9.8-release (r1371)Copyright (c) 2001-2008, Andrew Aksyonoff

using config file '/Users/pat/Code/ruby/ts_test_2.2.0/config/development.sphinx.conf'...stop: succesfully sent SIGTERM to pid 6220Stopped search daemon (pid 6220).

Page 112: Solving the Riddle of Search: Using Sphinx with Rails

rake ts:inrake ts:startrake ts:stop

Page 113: Solving the Riddle of Search: Using Sphinx with Rails

Can wesearch yet?

Page 114: Solving the Riddle of Search: Using Sphinx with Rails

Person.search

Page 115: Solving the Riddle of Search: Using Sphinx with Rails

Person.search "Pat Allan"

Page 116: Solving the Riddle of Search: Using Sphinx with Rails

Focus that search on a

specific field

Page 117: Solving the Riddle of Search: Using Sphinx with Rails

Person.search("Ruby", :conditions => {:name => "Pat Allan"})

Page 118: Solving the Riddle of Search: Using Sphinx with Rails

Conditions are not exactly like

ActiveRecord

Page 119: Solving the Riddle of Search: Using Sphinx with Rails

Strings only.

Page 120: Solving the Riddle of Search: Using Sphinx with Rails

And conditions should always

be a Hash.

Page 121: Solving the Riddle of Search: Using Sphinx with Rails

Filtering on Attributes

Page 122: Solving the Riddle of Search: Using Sphinx with Rails

Person.search("Ruby", :with => {:admin => true})

Page 123: Solving the Riddle of Search: Using Sphinx with Rails

Filteringby Ranges

Page 124: Solving the Riddle of Search: Using Sphinx with Rails

Person.search("Ruby", :with => { :created_at => 1.week.ago..Time.now })

Page 125: Solving the Riddle of Search: Using Sphinx with Rails

Filteringby Arrays

Page 126: Solving the Riddle of Search: Using Sphinx with Rails

Person.search("Ruby", :with => {:tag_ids => [1, 2, 3]})

Page 127: Solving the Riddle of Search: Using Sphinx with Rails

Matching all Array values

Page 128: Solving the Riddle of Search: Using Sphinx with Rails

Person.search("Ruby", :with_all => {:tag_ids => [1, 2, 3]})

Page 129: Solving the Riddle of Search: Using Sphinx with Rails

ExcludingFilter Values

Page 130: Solving the Riddle of Search: Using Sphinx with Rails

Person.search("Ruby", :without => {:admin => true})

Page 131: Solving the Riddle of Search: Using Sphinx with Rails

Match Modes

Page 132: Solving the Riddle of Search: Using Sphinx with Rails

:all

Page 133: Solving the Riddle of Search: Using Sphinx with Rails

(The Default)

Page 134: Solving the Riddle of Search: Using Sphinx with Rails

All words must exist in a

document

Page 135: Solving the Riddle of Search: Using Sphinx with Rails

:any

Page 136: Solving the Riddle of Search: Using Sphinx with Rails

Person.search("Pat Allan", :match_mode => :any)

Page 137: Solving the Riddle of Search: Using Sphinx with Rails

One of the words must exist in a

document

Page 138: Solving the Riddle of Search: Using Sphinx with Rails

:phrase

Page 139: Solving the Riddle of Search: Using Sphinx with Rails

The exact query must exist in the

document

Page 140: Solving the Riddle of Search: Using Sphinx with Rails

:boolean

Page 141: Solving the Riddle of Search: Using Sphinx with Rails

Use boolean logic in search

queries

Page 142: Solving the Riddle of Search: Using Sphinx with Rails

:extended

Page 143: Solving the Riddle of Search: Using Sphinx with Rails

Can match on specific fields,

and apply boolean logic

Page 144: Solving the Riddle of Search: Using Sphinx with Rails

Weighting

Page 145: Solving the Riddle of Search: Using Sphinx with Rails

Person.search("Pat Allan", :field_weights => { "name" => 100, "profile" => 50, "location" => 30 })

Page 146: Solving the Riddle of Search: Using Sphinx with Rails

define_index do # ... set_property :field_weights => { "name" => 100, "profile" => 50, "location" => 30 }end

Page 147: Solving the Riddle of Search: Using Sphinx with Rails

Sorting

Page 148: Solving the Riddle of Search: Using Sphinx with Rails

:relevance

Page 149: Solving the Riddle of Search: Using Sphinx with Rails

The Default

Page 150: Solving the Riddle of Search: Using Sphinx with Rails

By Attributes

Page 151: Solving the Riddle of Search: Using Sphinx with Rails

Or Fields flagged as :sortable

Page 152: Solving the Riddle of Search: Using Sphinx with Rails

:attr_asc

Page 153: Solving the Riddle of Search: Using Sphinx with Rails

Person.search "Pat Allan", :order => :created_at

Page 154: Solving the Riddle of Search: Using Sphinx with Rails

:attr_desc

Page 155: Solving the Riddle of Search: Using Sphinx with Rails

Person.search "Pat Allan", :order => :created_at, :sort_mode => :desc

Page 156: Solving the Riddle of Search: Using Sphinx with Rails

:extended

Page 157: Solving the Riddle of Search: Using Sphinx with Rails

Person.search "Pat Allan", :order => "last_name ASC, first_name ASC"

Page 158: Solving the Riddle of Search: Using Sphinx with Rails

:expr

Page 159: Solving the Riddle of Search: Using Sphinx with Rails

Person.search "Pat Allan", :sort_mode => :expr, :sort_by => "@weight * ranking"

Page 160: Solving the Riddle of Search: Using Sphinx with Rails

:time_segments

Page 161: Solving the Riddle of Search: Using Sphinx with Rails

Person.search "Pat Allan", :sort_mode => :time_segments, :sort_by => "created_at"

Page 162: Solving the Riddle of Search: Using Sphinx with Rails

• Last Hour

• Last Day

• Last Week

• Last Month

• Last 3 Months

• Everything Else

Page 163: Solving the Riddle of Search: Using Sphinx with Rails

Sorted by Segment, then

Relevance

Page 164: Solving the Riddle of Search: Using Sphinx with Rails

Multi-Model Searches

Page 165: Solving the Riddle of Search: Using Sphinx with Rails

ThinkingSphinx::Search.search( "Pat Allan")

Page 166: Solving the Riddle of Search: Using Sphinx with Rails

Pagination

Page 167: Solving the Riddle of Search: Using Sphinx with Rails

Always On

Page 168: Solving the Riddle of Search: Using Sphinx with Rails

Person.search "Pat Allan", :page => params[:page]

Page 169: Solving the Riddle of Search: Using Sphinx with Rails

You can request really big pages

though.

Page 170: Solving the Riddle of Search: Using Sphinx with Rails

Person.search "Pat Allan", :per_page => 1000, :page => params[:page]

Page 171: Solving the Riddle of Search: Using Sphinx with Rails

If you want massive pages...

Page 172: Solving the Riddle of Search: Using Sphinx with Rails

Person.search "Pat Allan", :page => params[:page], :per_page => 1_000_000, :max_matches => 1_000_000

Page 173: Solving the Riddle of Search: Using Sphinx with Rails

# config/sphinx.ymldevelopment: max_matches: 1000000test: max_matches: 1000000production: max_matches: 1000000

Page 174: Solving the Riddle of Search: Using Sphinx with Rails

Don’t forget to restart Sphinx

Page 175: Solving the Riddle of Search: Using Sphinx with Rails

Installing Thinking Sphinx

Page 176: Solving the Riddle of Search: Using Sphinx with Rails

Using Git?

Page 177: Solving the Riddle of Search: Using Sphinx with Rails

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

Page 178: Solving the Riddle of Search: Using Sphinx with Rails

git clone git://github.com/ freelancing-god/thinking-sphinx.git

Page 179: Solving the Riddle of Search: Using Sphinx with Rails

Want the gem instead?

Page 180: Solving the Riddle of Search: Using Sphinx with Rails

sudo gem install freelancing-god-thinking-sphinx --source http://gems.github.com

Page 181: Solving the Riddle of Search: Using Sphinx with Rails

# inside config/environment.rbconfig.gem( "freelancing-god-thinking-sphinx", :version => "1.1.6", :lib => "thinking_sphinx")

Page 182: Solving the Riddle of Search: Using Sphinx with Rails

# at the end of your Rakefile:require 'thinking_sphinx/tasks'

Page 183: Solving the Riddle of Search: Using Sphinx with Rails

Installing Sphinx

Page 184: Solving the Riddle of Search: Using Sphinx with Rails

Windows?

Page 185: Solving the Riddle of Search: Using Sphinx with Rails

Installer.Piece of Cake.

Page 186: Solving the Riddle of Search: Using Sphinx with Rails

*nix?

Page 187: Solving the Riddle of Search: Using Sphinx with Rails

apt, yum, etc.Should be painless.

Page 188: Solving the Riddle of Search: Using Sphinx with Rails

OS X?

Page 189: Solving the Riddle of Search: Using Sphinx with Rails

Well... it canbe a little

complicated

Page 190: Solving the Riddle of Search: Using Sphinx with Rails

./configuremakesudo make install

Page 191: Solving the Riddle of Search: Using Sphinx with Rails

With PostgreSQL?

Page 192: Solving the Riddle of Search: Using Sphinx with Rails

./configure --with-pgsql=/usr/local/ include/postgresqlmakesudo make install

Page 193: Solving the Riddle of Search: Using Sphinx with Rails

pg_config --pkgincludedir

Page 194: Solving the Riddle of Search: Using Sphinx with Rails

Or Use MacPorts

Page 195: Solving the Riddle of Search: Using Sphinx with Rails

... if you’re using it for MySQL or

PostgreSQLas well

Page 196: Solving the Riddle of Search: Using Sphinx with Rails
Page 197: Solving the Riddle of Search: Using Sphinx with Rails

Anyone still stuck?

Page 198: Solving the Riddle of Search: Using Sphinx with Rails

Examples in the Sample App

Page 199: Solving the Riddle of Search: Using Sphinx with Rails

git://github.com/freelancing-

god/sphinx-tute.git

Page 200: Solving the Riddle of Search: Using Sphinx with Rails

Set up Database

Page 201: Solving the Riddle of Search: Using Sphinx with Rails

Hack as we go

Page 202: Solving the Riddle of Search: Using Sphinx with Rails

Questions?

Page 203: Solving the Riddle of Search: Using Sphinx with Rails

Afternoon Break?

Page 204: Solving the Riddle of Search: Using Sphinx with Rails
Page 205: Solving the Riddle of Search: Using Sphinx with Rails

Part Two: Beyond the

Basics

Page 206: Solving the Riddle of Search: Using Sphinx with Rails

Delta Indexes

Page 207: Solving the Riddle of Search: Using Sphinx with Rails

Why?

Page 208: Solving the Riddle of Search: Using Sphinx with Rails

No single-document

updates

Page 209: Solving the Riddle of Search: Using Sphinx with Rails

Can only process a full index.

Page 210: Solving the Riddle of Search: Using Sphinx with Rails

Delta Index holds the changes

Page 211: Solving the Riddle of Search: Using Sphinx with Rails
Page 212: Solving the Riddle of Search: Using Sphinx with Rails

Page 213: Solving the Riddle of Search: Using Sphinx with Rails

Page 214: Solving the Riddle of Search: Using Sphinx with Rails

rake thinking_sphinx:index

Page 215: Solving the Riddle of Search: Using Sphinx with Rails

Page 216: Solving the Riddle of Search: Using Sphinx with Rails

What does this mean?

Page 217: Solving the Riddle of Search: Using Sphinx with Rails

We can track changes as

they happen

Page 218: Solving the Riddle of Search: Using Sphinx with Rails

... at the cost of a little extra overhead.

Page 219: Solving the Riddle of Search: Using Sphinx with Rails

How?

Page 220: Solving the Riddle of Search: Using Sphinx with Rails

1. Add a boolean column ‘delta’ to

your model.

Page 221: Solving the Riddle of Search: Using Sphinx with Rails

2. Tell your index to use a delta.

Page 222: Solving the Riddle of Search: Using Sphinx with Rails

define_index do # ... set_property :delta => trueend

Page 223: Solving the Riddle of Search: Using Sphinx with Rails

3. Stop,Re-index, Restart.

Page 224: Solving the Riddle of Search: Using Sphinx with Rails

rake thinking_sphinx:stoprake thinking_sphinx:indexrake thinking_sphinx:start

Page 225: Solving the Riddle of Search: Using Sphinx with Rails

... that’s the default

approach

Page 226: Solving the Riddle of Search: Using Sphinx with Rails

Datetime Deltas

Page 227: Solving the Riddle of Search: Using Sphinx with Rails

define_index do # ... set_property :delta => :datetime, :threshold => 1.dayend

Page 228: Solving the Riddle of Search: Using Sphinx with Rails

Use existing updated_at

column

Page 229: Solving the Riddle of Search: Using Sphinx with Rails

rake thinking_sphinx:index:delta

Page 230: Solving the Riddle of Search: Using Sphinx with Rails

Please Note: Not entirely reliable

Page 231: Solving the Riddle of Search: Using Sphinx with Rails

Delayed Deltas

Page 232: Solving the Riddle of Search: Using Sphinx with Rails

create_table :delayed_jobs do |t| # ...end

Page 233: Solving the Riddle of Search: Using Sphinx with Rails

define_index do # ... set_property :delta => :delayedend

Page 234: Solving the Riddle of Search: Using Sphinx with Rails

rake thinking_sphinx:delayed_delta

Page 235: Solving the Riddle of Search: Using Sphinx with Rails

Uses delayed_job

Page 236: Solving the Riddle of Search: Using Sphinx with Rails

Removes overheard from your web stack

Page 237: Solving the Riddle of Search: Using Sphinx with Rails

Give it a Shot

Page 238: Solving the Riddle of Search: Using Sphinx with Rails

Facets

Page 239: Solving the Riddle of Search: Using Sphinx with Rails

Search Summaries

Page 240: Solving the Riddle of Search: Using Sphinx with Rails
Page 241: Solving the Riddle of Search: Using Sphinx with Rails

define_index do # ... indexes location, country, :facet => true has admin, :facet => trueend

Page 242: Solving the Riddle of Search: Using Sphinx with Rails

Person.facets("Ruby")

Page 243: Solving the Riddle of Search: Using Sphinx with Rails

{ :country => { "Australia" => 15, "USA" => 11, "Germany" => 8, "Canada" => 12 }, :location => { "Melbourne" => 3, # ...}

Page 244: Solving the Riddle of Search: Using Sphinx with Rails

@facets.for(:country => "Australia")

Page 245: Solving the Riddle of Search: Using Sphinx with Rails

Give it a Shot

Page 246: Solving the Riddle of Search: Using Sphinx with Rails

Extended Configuration

Page 247: Solving the Riddle of Search: Using Sphinx with Rails

config/sphinx.yml

Page 248: Solving the Riddle of Search: Using Sphinx with Rails

Just likedatabase.yml

Page 249: Solving the Riddle of Search: Using Sphinx with Rails

Global Sphinx settings

Page 250: Solving the Riddle of Search: Using Sphinx with Rails

Common Example:

Partial Matching

Page 251: Solving the Riddle of Search: Using Sphinx with Rails

Rai* == Rails

Page 252: Solving the Riddle of Search: Using Sphinx with Rails

development: enable_star: 1 min_infix_len: 1test: enable_star: 1 min_infix_len: 1production: enable_star: 1 min_infix_len: 1

Page 253: Solving the Riddle of Search: Using Sphinx with Rails

Stop,Re-index, Restart.

Page 254: Solving the Riddle of Search: Using Sphinx with Rails

Deployment

Page 255: Solving the Riddle of Search: Using Sphinx with Rails

Using Capistrano?

Page 256: Solving the Riddle of Search: Using Sphinx with Rails

# config/deploy.rbload 'vendor/plugins/thinking-sphinx/lib/thinking_sphinx/deploy/capistrano'

Page 257: Solving the Riddle of Search: Using Sphinx with Rails

cap thinking_sphinx:configurecap thinking_sphinx:indexcap thinking_sphinx:install:sphinxcap thinking_sphinx:install:tscap thinking_sphinx:rebuildcap thinking_sphinx:restartcap thinking_sphinx:shared_sphinx_foldercap thinking_sphinx:startcap thinking_sphinx:stop

Page 258: Solving the Riddle of Search: Using Sphinx with Rails

Installing Sphinx

Page 259: Solving the Riddle of Search: Using Sphinx with Rails

Set Sphinx index location

Page 260: Solving the Riddle of Search: Using Sphinx with Rails

Sphinx does not like symlinks

Page 261: Solving the Riddle of Search: Using Sphinx with Rails

# config/sphinx.ymlproduction: searchd_file_path: "/var/www/sphinx-tute/shared/db/sphinx/production/"

Page 262: Solving the Riddle of Search: Using Sphinx with Rails

# config/deploy.rbafter "deploy:setup", "thinking_sphinx:shared_sphinx_folder"

Page 263: Solving the Riddle of Search: Using Sphinx with Rails

Regular indexing via

Cron

Page 264: Solving the Riddle of Search: Using Sphinx with Rails

Geo-Searching

Page 265: Solving the Riddle of Search: Using Sphinx with Rails

Need latitudeand longitude

attributes

Page 266: Solving the Riddle of Search: Using Sphinx with Rails

As floats

Page 267: Solving the Riddle of Search: Using Sphinx with Rails

In Radians

Page 268: Solving the Riddle of Search: Using Sphinx with Rails

define_index do # ... has latitude, longitudeend

Page 269: Solving the Riddle of Search: Using Sphinx with Rails

Stop,Re-index, Restart.

Page 270: Solving the Riddle of Search: Using Sphinx with Rails

Bar.search( "Las Vegas", :geo => [@lat, @lng], :sort => "@geodist ASC")

Page 271: Solving the Riddle of Search: Using Sphinx with Rails

@bars.each_with_geodist do |bar, distance| # distance is in metresend

Page 272: Solving the Riddle of Search: Using Sphinx with Rails

Using custom column names?

Page 273: Solving the Riddle of Search: Using Sphinx with Rails

define_index do # ... set_property( :latitude_attr => :updown, :longitude_attr => :leftright )end

Page 274: Solving the Riddle of Search: Using Sphinx with Rails

Geo-searching across multiple

models?

Page 275: Solving the Riddle of Search: Using Sphinx with Rails

ThinkingSphinx::Search.search( "pancakes", :geo => [@lat, @lng] :latitude_attr => :lat, :longitude_attr => :lng)

Page 276: Solving the Riddle of Search: Using Sphinx with Rails

Give it a Shot

Page 277: Solving the Riddle of Search: Using Sphinx with Rails

Are we done yet?

Page 278: Solving the Riddle of Search: Using Sphinx with Rails

http://ts.freelancing-gods.com

Page 281: Solving the Riddle of Search: Using Sphinx with Rails

Questions?

Page 282: Solving the Riddle of Search: Using Sphinx with Rails
Page 283: Solving the Riddle of Search: Using Sphinx with Rails

Thank you.