mysql - 复杂的搜索设计

标签 mysql ruby-on-rails-3 search search-engine

我正在使用 mysql 在 Rails3 上实现菜谱搜索。

搜索的想法是,用户输入任意数量的成分,然后搜索输出建议做什么,并按产品缺陷顺序排序。

class Recipe < ActiveRecord::Base
  has_many :ingredients
end

# these records will be entered by user
class IngredientType < ActiveRecord::Base
  has_many :ingredients
end

# this table is join table
class Ingredient < ActiveRecord::Base
  belongs_to :ingredient_type
  belongs_to :recipe
end

实现此搜索的最有效方法是什么? 您会推荐哪些 gem 或技术? 谢谢您的解答

最佳答案

  def self.from_ingredients ingredients
    count_sql = Ingredient.
        select('COUNT(*)').
        joins(:recipes_ingredients).
        where('`recipes_ingredients`.`recipe_id` = `recipes`.`id`').
        where('`ingredients`.`id` in (?)', ingredients).to_sql

    where("(#{count_sql}) > 0").
        order("((`recipes`.`ingredients_count`) - (#{count_sql})) ASC")
  end

我设法通过在配方模型中创建这样的方法来找到解决方案。

关于mysql - 复杂的搜索设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5612453/

相关文章:

ruby-on-rails - 与 Thinking Sphinx 的深度关联和 Sphinx 索引?

ruby-on-rails - Rails 简单模型属性未保存到数据库

java - 如何使用java在网站中输入文本?

MYSQL - 不同数据库中的两个表

search - 搜索音频

mysql - LOAD DATA LOCAL INFILE 在 MySQL 中只导入一行

php - PDO - 显示 sql 错误

php - 在 WordPress 页面上使用 SQL

带有 LIKE 运算符的 Select 语句中的 MySQL 案例

ruby-on-rails - 如何测试 after_sign_in_path_for(resource)?