Search code examples
ruby-on-rails-3solrsunspot-railsfuzzy-searchexact-match

Is there a way with Solr to search fuzzy AND exact?


thank you for taking your time

Problem

I have a problem with searching records based on a fuzzy name search and an exact Regio search. This is what I have now:

What I have now

# :klantnaam is a param from a search-form
@param = Staffingcustomer.search do
    fulltext params[:klantnaam]
end
@staffingcustomers = @param.results

There is another field, 'Regio'.
The results should only be with a certain 'Regio'.

Example requirements

So for example,
I'm a user with Regio 3. I want to visualize/select all Staffingcustomers with Fuzzy name search Anne and my own Regio 3. So, I don't want to see Staffingcustomers with Regio 2.

What happens now is:

@param = Staffingcustomer.search do
     fulltext 'anne'
end
@staffingcustomers = @param.results

Will return all Staffingcustomers with all different Regios...

After some research / logic thinking

I think it could be something like the following:

@param = Staffingcustomer.search do
    fulltext params[:klantnaam]
end
@staffingcustomers = @param.results.find(:all, :conditions => {:regio => 3})

or

@param = Staffingcustomer.search do
    fulltext params[:klantnaam] and fulltext '3'   ## or 3, without the quotes
end
@staffingcustomers = @param.results

But that just doesn't seem to work.

Can you help me with this issue?

The logic problem

Actually this is a fuzzy search on an 'exact selected' part of a huge table.

Hope you can help.

Thanks in advance.

Kers.


Solution

  • I think you want something like:

    # find Staffing Customers with Regio 3 and text of params[:klantnaam]
    @param = Staffingcustomer.search do
      with(:regio, 3)
      fulltext params[:klantnaam]
    end
    @staffingcustomers = @param.results
    

    Refer to https://github.com/sunspot/sunspot#readme . You'll also have to index regio in Searchable block of your Staffingcustomer model as whatever data type you stored in the DB (integer, for example).