按轨道相关性排序

时间:2017-02-21 02:33:44

标签: ruby-on-rails postgresql

以下是我在搜索页面中过滤项目所做的工作:

var braken = document.querySelector(".board li.braken");
    var champagne = document.querySelector(".board li.champagne");

    braken.addEventListener("click", function () {
        braken.classList.remove("braken")
        braken.classList.add("champagne");
    })
    champagne.addEventListener("click", function () {
        champagne.classList.remove("champagne")
        champagne.classList.add("braken")
    })

我想不过滤这些项目,而是按相关性,某种评分指标对其进行排序;选择的标签,字符串等更加巧合 - 分数越高,任务显示越高。

我正在考虑这种疑问:

@experiences = Mission.where(active: true)
@experiences = @experiences.where(accept_families: true) if filter_params['forFamily'].present? && filter_params['forFamily'] == true
@experiences = @experiences.includes(:country_missions).where(:country_missions => { :country_id => filter_params['countries'] }) if filter_params['countries'].present?
@experiences = @experiences.includes(:skill_all).where(:skill_missions => { :skill_id => filter_params['skills'] }) if filter_params['skills'].present?

months = []
months.push(12, 1, 2) if filter_params['start_winter'].present? && filter_params['start_winter'] == true
@experiences = @experiences.joins(:start_dates).includes(:start_dates).where('extract(month from start_dates.start) IN (?)', months) unless months.blank?

但那太简单了;我无法想象如何获得"而#34;每个标签的大小写:例如,每个filter_params [' countries']包含一系列ID:[212,30,1]。 对于模型项中存在的每个国家/地区,分数增加1。

我该怎么做?

我正在使用Postgres作为数据库。

我已经尝试了类似

的内容
SELECT title, company, user, CASE 
  WHEN upper(company_name) LIKE '%#{word[0].upcase}%' THEN 3
  WHEN upper(company_name) LIKE '%#{company_name.upcase}%' THEN 2
  ELSE 0 END as score      
  FROM selected_tables
  WHERE company_name LIKE '%#{company_name}%'
  ORDER BY score DESC;

但这并不正确。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你会找到这样的东西:

SELECT title, company, user,
       CASE WHEN upper(company_name) LIKE '%#{word[0].upcase}%' THEN 3 ELSE 0 END
     + CASE WHEN upper(company_name) LIKE '%#{company_name.upcase}%' THEN 2 ELSE 0 END
      as score      
  FROM selected_tables
 WHERE company_name LIKE '%#{company_name}%'
 ORDER BY score DESC;