简体   繁体   English

多关键字搜索

[英]Multi keyword search

I currently have a basic search that will allow me to type in a word and it will return back all records that are like that search. 我目前有一个基本的搜索,可以输入一个单词,它会返回所有与该搜索类似的记录。 It is as followed: 如下:

application.html.erb application.html.erb

        <%= form_tag games_path, :method => 'get' do %>
            <%= hidden_field_tag :direction, params[:direction] %>
            <%= hidden_field_tag :sort, params[:sort] %>
            <%= text_field_tag :search, params[:search] %>
        <%= submit_tag t('.searchb'), :game_name => nil %>
        <% end %>

controller and Model 控制器和型号

        @games = Game.search(params[:search])

  def self.search(search)
     search = search.to_s.strip.split
     search.inject(scoped) do |combined_scope, search|
     combined_scope.where(['game_name LIKE ? OR genre LIKE ? OR console LIKE ?', "%#{search}%", "%#{search}%", "%#{search}%"])
    end
  end   

What I now wish to do though is the ability to enter multiple words in to the search bar and have it return all related records to those words rather than any record that has them all. 我现在想做的是能够在搜索栏中输入多个单词,并使其将所有相关记录返回到那些单词,而不是所有包含它们的记录。

eg If I type fighting, action 例如,如果我打架,行动

I want it to return all fighting games and all action games. 我希望它返回所有格斗游戏和所有动作游戏。

How would I go about implementing this? 我将如何实施呢? Model: 模型:

EDIT 编辑

You should use a fulltext search engine like Sphinx for this! 您应该为此使用Sphinx等全文搜索引擎! There is a gem called thinking-sphinx wich offers an interface between ruby and the Sphinx server! 有一个名为thinking-sphinx宝石,它在ruby和Sphinx服务器之间提供了接口! The only downside is that you need to update the database periodly. 唯一的缺点是您需要定期更新数据库。 It doesnt support live updates. 它不支持实时更新。

You have to take the key words, loop them and create queries to achieve this. 您必须使用关键字,对其进行循环并创建查询以实现此目的。 You can use inject to make it easy. 您可以使用注入使其变得容易。 This might help. 可能会有所帮助。

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

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