Tag: 搜索

SQLite3“LIKE”或PostgreSQL“ILIKE”的通用Ruby解决方案?

我使用SQLite3进行开发,使用PostgreSQL进行部署。 但是,我面临以下问题: 我使用SQLite3简单搜索: def self.search(search) if search find(:all, :conditions => [“style LIKE ? OR construction LIKE ?”, “%#{search}%”, “%#{search}%”]) else find(:all) end end 但是,它不适用于PostgreSQL ,我需要替换ILIKE的LIKE才能解决问题: def self.search(search) if search find(:all, :conditions => [“style ILIKE ? OR construction ILIKE ?”, “%#{search}%”, “%#{search}%”]) else find(:all) end end 是否有“Ruby方式”在任何数据库中进行这些搜索? 编辑 – 基于您的答案我不相信我会找到一个通用的Ruby解决方案。 我遵循了Ruby on Rails教程:通过示例学习Rails – 作者:Michael Hartl […]

Ruby on Rails:在一次搜索中搜索两个模型

我有一个搜索页面,用户可以使用不同的下拉菜单搜索不同的项目。 他们可以一次搜索多个字段,如果找到与所有字段匹配的项目,搜索将返回一些内容。 inheritance我的项目控制器中的搜索操作: def search @search = params[:client], params[:industry], params[:tech], params[:keywords] @project_search = Project.search(*@search).order(sort_column + ‘ ‘ + sort_direction).paginated_for_index(per_page, page) @search_performed = !@search.reject! { |c| c.blank? }.empty? @project = Project.new(params[:project]) @all_technols = Technol.all @project_technol = @project.projecttechnols.build respond_to do |format| format.html # search.html.erb format.json { render :json => @project } end end 这是我的搜索视图。 Client : “-Any-“, […]

Ruby on Rails:表关系搜索

我仍然无法搜索存储在单独表中的技术,其中技术表(technol)和Project表之间通过名为projecttechnol的表之间存在关系。 当我尝试使用技术(tech1)搜索项目时,这是我的日志。 Parameters: {“utf8″=>”✓”, “client”=>””, “industry”=>””, “role”=>””, “technols”=>{“id”=>[“”, “1”, “”]}, “business_div”=>””, “project_owner”=>””, “start_date_dd”=>””, “start_date_A”=>””, “start_date_B”=>””, “status”=>””, “keywords”=>””, “per_page”=>”10”} User Load (0.6ms) SELECT “users”.* FROM “users” WHERE “users”.”id” = 1 LIMIT 1 Technol Load (0.3ms) SELECT “technols”.* FROM “technols” Project Load (0.5ms) SELECT “projects”.* FROM “projects” ORDER BY client Project Load (0.6ms) SELECT “projects”.* FROM “projects” […]

Ruby on Rails 2在Hash中搜索字符串

我需要帮助… 我有这样的哈希: @ingredients = Hash.new @ingredients[1] = “Biscottes Mini(recondo)” @ingredients[2] = “Abadejo” @ingredients[3] = “Acelga” @ingredients[4] = “Agua de Coco” @ingredients[5] = “Ajo” @ingredients[6] = “Almidón de Arroz” @ingredients[7] = “Anillos Con Avena Integral cheerios (nestle)” @ingredients[8] = “Apio” 当我写“scotte”时,我需要搜索那个哈希以找到“Biscottes Mini(recondo)” 一些帮助? THK!

Ruby在数组中找到偏移量

我正在寻找一种方法,以更清洁的方式在Ruby中执行以下操作: class Array def find_index_with_offset(offset, &block) [offset..-1].find &block end end offset = array.find_index {|element| element.meets_some_criterion?} the_object_I_want = array.find_index_with_offset(offset+1) {|element| element.meets_another_criterion?} 所以我在Ruby数组中搜索某个对象的索引,然后我进行后续搜索,找到匹配其他条件的第一个对象,并在数组中有更高的索引。 有一个更好的方法吗? 我对清洁工的意思是什么:不涉及明确切割数组的东西。 当你这样做几次时,计算切片索引会很快变得混乱。 我想继续操作原始arrays。 它更容易理解,更不容易出错。 NB。 在我的实际代码中,我没有使用猴子修补数组,但我想提请注意我希望我复制数组/可枚举的现有function这一事实 编辑 根据MladenJablanović的评论, offset + 1固定位置; 重写错误 根据MladenJablanović的评论增加了对“清洁剂”的解释

搜索句子

我有一个rails应用程序,并已决定进行搜索。 我有一个搜索文本域,控制器负责一切。 在我的搜索方法中 def self.search(search) if search str = [] search.split.each do |s| a = find(:all, :conditions => [‘title or content LIKE ?’, “%#{s}%” ]) str = (str + a).uniq end else find(:all) end end 我希望能够处理多个单词搜索。 如果删除每个…循环,它可以正常使用1个单词搜索。 无论如何,我想找到每个被搜索单词的相关post并返回其组合。 防爆。 如果有人搜索“最伟大的玩家”,它将返回标题或内容为“最大”的所有post实例以及任何具有标题或内容“播放器”的post

使用Searchkick在ElasticSearch中搜索子文档

假设我有一系列我想要搜索的嵌入式或子级文档,但返回他们的父母作为我的结果,如建筑物和单位: Building A – Unit 1F – Unit 1R – Unit 2F: 1200 sq ft – Unit 2R: 2300 sq ft Building B – Unit 202: 500 sq ft – Unit 203: 650 sq ft 现在假设我要归还所有单位> = 1000平方英尺的建筑物。我该怎么做?

如何用Sunspot突出显示单词?

我想在文本中突出显示已找到的单词,例如,如此处所示。 据我所知,我必须遵循以下步骤: 1)在我的模型中,我必须在我想要突出显示的字段中添加:stored => true选项: searchable do text :title, :stored => true text :description end 2)在我的控制器中,我必须声明我想要突出显示的字段: def search @search = Article.search do keywords params[:search] do highlight :title end end end 3)在视图中我不知道该怎么做,我试过这个: – @search.each_hit_with_result do |hit, result| %p= link_to raw(hit_title(hit)), article_path(result) 这是做方法hit_title : def hit_title(hit) if highlight = hit.highlight(:title) highlight.format { |word| “#{word}” } else h(hit.result.title) […]

更快速地查看另一个字符串中是否包含大量字符串

我有一个存储在数组中的大约300k常用字的列表。 所以,数组的1个元素= 1个单词。 另一方面,我有一个庞大的字符串列表,其中可能包含其中一个或多个300k字。 示例字符串将是: ifdxawesome453 。 现在,我需要针对常用词检查每个长字符串。 如果在该字符串中找到单词,则立即返回。 所以,我需要再次检查300k字ifdxawesome453并查看其中是否包含任何字。 所以我做的是: huge_list_of_words.any? do |word| random_long_word.include?(word) end 虽然对于随机长词的小样本来说这是可以的,但如果我有数百万个,那么突然需要几个小时才能完成这项工作。 有没有办法更快地做到这一点? 我想到的唯一方法就是如果我抽样出来,说这些300k中最常见的10k字,并先与它进行比较,如果找不到匹配,则与完整列表进行比较。 另一种大幅加速的方法是按大小对300k字的数组进行分组。 当我然后将长随机字与它进行比较时,我首先检查单词的大小是否过滤掉任何更长的单词。 然后我留下相同大小或更少单词的索引,并从具有最小大小的单词开始搜索它们。

使用Datamapper和Sinatra进行简单搜索

我对Ruby和后端开发很新。 话虽这么说,我正在尝试创建一个简单的搜索表单。 我使用Sinatra作为框架,使用Datamapper作为我的ORM。 做这个的最好方式是什么? 下面是我的架构我希望搜索操作同时搜索磁贴和类别。 require ‘sinatra’ require ‘datamapper’ DataMapper.setup(:default, “sqlite3://#{Dir.pwd}/cal.db”) class Event include DataMapper::Resource property :id, Serial property :title, String property :text, Text property :contact_name, String property :contact_email, String property :location, String property :event_start_time, String property :event_end_time, String property :category, String property :created_at, DateTime property :approved, Boolean, :default => false end DataMapper.auto_upgrade! post ‘/search’ […]