简体   繁体   English

太阳黑子搜索完全匹配电子邮件

[英]Sunspot search by exact match on emails

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. 您好今天我遇到了Sunspot Solr的问题,我无法使其完全适用于电子邮件搜索,我已经尝试了几个小时,最后我总结了这个解决方案,但它仍然不能完全匹配。

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 例如,我需要输入一封电子邮件mushos32@yahoo.gr并给我1个结果这个电子邮件的人,如果我写mushos32@yahoo.g或任何其他组合,除了原来的,它应该什么也不返回

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 我尝试重新索引,重新启动solr服务器的一切,但似乎没有工作

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. 有关更多信息,请参阅Sunspot文档

It seem using equal_to just work for integer, not working with String. 似乎使用equal_to只是为整数工作,而不是使用String。 At this moment I index string using Text instead String. 此时我使用Text而不是String来索引字符串。 But yeah, it's too bad 但是,这太糟糕了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM