[英]What's the idiomatic way of writing a search function in Rails?
I have a search algorithm for my Rails app that works right now, but I really don't think this is how it should be done. 我有一个适用于我的Rails应用程序的搜索算法,该算法现在可以运行,但是我真的不认为应该这样做。 Essentially, I do something of the following nature:
本质上,我做以下事情:
def search
users = User.all
//Implement search algorithm on the users array
end
I really feel like this shouldn't be how I should be doing it. 我真的觉得这不应该是我应该做的。 I feel like I'm supposed to be using
SQL
or the ActiveRecord methods to get exactly the rows I need, but when the search gets pretty intensive I feel like that would be a very tough SQL
command to write. 我感觉应该使用
SQL
或ActiveRecord方法来获取我需要的行,但是当搜索变得非常密集时,我觉得这将是编写非常困难的SQL
命令。 What's the idiomatic way of writing a search algorithm for searching for specific things in the database? 编写用于在数据库中搜索特定内容的搜索算法的惯用方式是什么? For example, names.
例如,名称。 What if I wanted to search for names and inputted
ire
. 如果我想搜索姓名并输入
ire
怎么办。 I'd want Irene
to show up before desiree
, but both have that substring. 我希望
Irene
出现在desiree
之前,但是两者都具有该子字符串。 Just an example. 只是一个例子。
I usually elastic when I need real search, like the stemming for 'Irene' you mention: 我通常会在需要实际搜索时保持弹性,例如您提到“ Irene”的词干:
https://github.com/elasticsearch/elasticsearch-ruby https://github.com/elasticsearch/elasticsearch-ruby
For smaller needs, you can use the full text search capabilities in postgres, with a gem like this one I have used: 对于较小的需求,您可以在postgres中使用全文搜索功能,并将其与我使用过的类似gem一起使用:
https://github.com/textacular/textacular https://github.com/textacular/textacular
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.