Monday, 15 August 2011

ruby on rails 3.2 - Fuzzy "begins with" search with Solr and Sunspot on multiple words -



ruby on rails 3.2 - Fuzzy "begins with" search with Solr and Sunspot on multiple words -

i'm trying provide text search find cities, based on name , name of country belong to.

for instance, want homecoming milan, italy search terms mil ita, or los angeles, united states search on los ang uni.

these current models have (validations omitted):

class city < activerecord::base belongs_to :country searchable text :name text :country country.name end end end

and

class country < activerecord::base end

this relevant part of solr schema.xml file:

<fieldtype class="solr.textfield" positionincrementgap="100" name="text"> <analyzer type="index"> <tokenizer class="solr.standardtokenizerfactory"/> <filter class="solr.standardfilterfactory"/> <filter class="solr.lowercasefilterfactory"/> <!--<filter class="solr.stopfilterfactory" words="stopwords.txt" ignorecase="true"/>--> <filter class="solr.isolatin1accentfilterfactory"/> <filter class="solr.trimfilterfactory" /> <filter class="solr.edgengramfilterfactory" mingramsize="3" maxgramsize="30"/> </analyzer> <analyzer type="query"> <tokenizer class="solr.standardtokenizerfactory"/> <filter class="solr.standardfilterfactory"/> <filter class="solr.lowercasefilterfactory"/> <!--<filter class="solr.stopfilterfactory" words="stopwords.txt" ignorecase="true"/>--> <filter class="solr.isolatin1accentfilterfactory"/> <filter class="solr.trimfilterfactory" /> </analyzer> </fieldtype>

i'm performing search in controller with:

search = city.search fulltext params[:q] paginate page: 1, per_page: 10 end

what happens here, if search mil milan, italy result, searching mil ita homecoming nothing.

any ideas?

thank in advance,

r.

found solution, thought i'd share.

all need build 'custom field' text want enable search on, (in city model):

searchable text :composite_name "#{name} #{country.name}" end end

this works.

r.

solr ruby-on-rails-3.2 sunspot-solr

No comments:

Post a Comment