Rails autocomplete field (Find on one field)

222 views
Skip to first unread message

Shaliko

unread,
Nov 14, 2009, 8:19:11 AM11/14/09
to Thinking Sphinx
Hi

I have 2 model and 1 controller

class Shop < ActiveRecord::Base
has_many :addresses, :dependent => :destroy

define_index do
indexes :company, :sortable => true
indexes :newsletter_type
indexes addresses.zip, :as => :addresses_zip
end
end

class Address < ActiveRecord::Base
belongs_to :shop
end

class SearchController < ApplicationController

def show
@conditions = { :newsletter_type => 'TRD1'}
@conditions = @conditions.merge({:addresses_zip => params
[:zip].to_s}) unless params[:zip].blank?

@shops = Shop.search :conditions => @conditions, :include
=> :addresses, :page => params[:page], :per_page => Shop.per_page
end

def auto_complete
@shops = Shop.search "@addresses_zip #{params[:q]}", :match_mode
=> :extended
render :text => @shops.collect(&:addresses).flatten.collect
(&:zip).join("\n")
end

end


I have a search form which has 2 fields:

1) Search term - search from all fields (company, newsletter_type,
addresses_zip)
2) Zip - search only zip data and have autocomplete

Now this code works not correctly, because it does not look for full-
text.

Example:

I have 2 Shop's with the zip code 350072 and 350000

Thus if the 'Shop.search "@addresses_zip 350",: match_mode =>:
extended' - I will not have results

How to I organize a full-text search on one field in the index?

Shaliko

unread,
Nov 16, 2009, 2:15:52 AM11/16/09
to Thinking Sphinx
No ideas?

Pat Allan

unread,
Nov 16, 2009, 2:33:55 AM11/16/09
to thinkin...@googlegroups.com
Since you're not matching on full words, you'll need to add the
following settings to your Shop model (or globally in config/
sphinx.yml):

define_index do
# ...

# Assuming you don't want suggestions for less than 2 digits
set_property :min_prefix_len => 2
set_property :enable_star => true
end


And then change your autocomplete search to be the following:
Shop.search :conditions => {:addresses_zip => "#{params[:q]}*"}

If this doesn't work, let us know.

Cheers

--
Pat

Shaliko

unread,
Nov 16, 2009, 10:32:58 AM11/16/09
to Thinking Sphinx
It work - Thank you very much!

phil

unread,
Nov 17, 2009, 4:35:11 AM11/17/09
to Thinking Sphinx
Does this apply the min prefix and 'starability' to all fields in the
search now? If I am heavily indexing a model and add these in because
I want to index one field in this way will there be any performance
(either in memory or speed) issues?

Pat Allan

unread,
Nov 17, 2009, 6:43:43 AM11/17/09
to thinkin...@googlegroups.com
You can specify :prefixes => true (or :infixes => true) for specific
fields, and that means it only applies to them, instead of all.

indexes addresses.zip, :as => :addresses_zip, :prefixes => true

I'm not sure if there's a performance hit, but definitely the more
fields with infixes or prefixes, and the smaller the minimum length of
those, the larger your indexes get. Memory-wise, probably not much if
any change - only attributes are stored in memory constantly.

--
Pat
Reply all
Reply to author
Forward
0 new messages