简体   繁体   English

在Rails中编写搜索功能的惯用方式是什么?

[英]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.

相关问题 ActiveRecord在联接中搜索空值的方式是什么? - What's the ActiveRecord way to search for nulls in a join? 在 Rails 中进行计数的正确方法是什么? - What's the right way to do counts in Rails? 在数据库中搜索UPC代码的最佳方法是什么? - What's the best way to search for a UPC code in a Database? 消毒destroy_all的最佳方法是什么-Rails - What's the best way to sanitize destroy_all - Rails 编写此查询的更好方法是什么? - What is the better way of writing this query? 在编写 upsert 过程时,使用 NULL 参数是否是处理主键仅存在于更新案例中的惯用方法? - When writing an upsert procedure, is using NULL parameters an idiomatic way to handle cases where the primary key only exists in the update case? 在不使用Hibernate一次加载所有结果的情况下迭代结果集的惯用方式是什么? - What is the idiomatic way to iterate over a result set without loading all results at once using Hibernate? 编写此oracle sql查询的更好方法是什么? - What is a better way of writing this oracle sql query? 在 MS ACCESS 替换表中搜索所有相关数字的最简单方法是什么? 更好的说明说明 - What's the easiest way to search a MS ACCESS Table of Replacement for all numbers that are related? Better explanation in description 按日期优化在 5000 万行中搜索 1000 行的查询的最佳方法是什么? Oracle - What's the best way to optimize a query of search 1000 rows in 50 million by date? Oracle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM