Question

Hello today i faced this problem with Sunspot Solr where I cannot make it work for exact email search, I have been trying for hours and finally i concluded to this solution but still it does not work as exact match.

For example I need to input an email mushos32@yahoo.gr and give me 1 result the person with this email, if i write mushos32@yahoo.g or any other combination except the original one it should return nothing

My current code is this

model
 searchable do
 text :email
 String :email

end

controller

 def search


 @users = User.search do
  keywords params[:query]
 end.results

 respond_to do |format|
  format.html { render :action => "index" }
  format.xml  { render :xml => @users }
 end
end

view/user/show

  <%= form_tag search_users_path, :method => :get do %>
<p>
  <%= text_field_tag :query, params[:search] %>
  <%= submit_tag "Search" ,:email => nil %>

</p>
<% if @users.empty? %>
    <p>Your search did not return any results. </p>
<% else %>
<% for user in @users %>
    <tr>

      <td><%= link_to user.name, user %> <%= link_to user.surname, user %></td>
      <%= hidden_field_tag :direction, params[:direction] %>
      <%= hidden_field_tag :sort, params[:sort] %>
      <td><%= image_tag user.photo.url(:thumb) %></td>
      <td><%= link_to "Connect", friendships_path(:friend_id => user), :method => :post %></td>
    </tr>
<%end%>

Could you help me ? I have tried reindexing, restarting solr server everything but doesnt seem to work

Était-ce utile?

La solution

Since you only want exact matches on the field, the easiest solution is to make it a string field rather than a text field:

class User < ActiveRecord::Base
  searchable do
    string :email
  end
end

So that you can search for users in your controller like this:

def search
  @users = User.search do
    with(:email, params[:query])
  end.results

  respond_to do |format|
    format.html { render :action => "index" }
    format.xml  { render :xml => @users }
  end
end

See the Sunspot documentation for more information.

Autres conseils

It seem using equal_to just work for integer, not working with String. At this moment I index string using Text instead String. But yeah, it's too bad

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top