【问题标题】:Different conditions for different fields in thinking-sphinxThinking-sphinx中不同领域的不同条件
【发布时间】:2017-10-18 14:08:30
【问题描述】:

我有:

ThinkingSphinx::Index.define :new_post, with: :real_time do
  indexes title, sortable: true
  indexes text

  has state, type: :string
  has forum_hidden, type: :boolean
  has created_at, type: :timestamp
  has publish_at, type: :timestamp
  has reminde_at, type: :timestamp
  has deleted_at, type: :timestamp
  has content_category_ids, type: :integer, multi: true
end

我需要获取所有记录,其中@title=query 具有任何值 publish_at 或 @text=query 具有 publish_at = 1.month.ago..Time.current

也就是说,我需要将这两个请求结合起来:

NewPost.search(conditions: { title: query })
NewPost.search(conditions: { text: query }, with: { publish_at: 1.month.ago..Time.current })

结果需要摘录

更新

@title 和@text 字段的published_at 间隔总是不同的,取决于用户的权限。比如可能有这样的情况:

NewPost.search(conditions: { title: query }, with: { publish_at: 1.year.ago..Time.current })
NewPost.search(conditions: { text: query }, with: { publish_at: 6.month.ago..Time.current })

所有不属于这些条件的结果根本不应该显示

【问题讨论】:

  • 现在已经看到了编辑,您可能别无选择,只能运行两个查询,然后合并...怀疑 ts 是否具有联合功能

标签: sphinx thinking-sphinx


【解决方案1】:

想要“组合”多个标准时,主要的事情是决定“计算”如何计算产生该效果的权重。 Sphinx 计算权重并按此排序。而不是多个不同查询的联合

例如 https://freelancing-gods.com/thinking-sphinx/searching.html#sorting

ThinkingSphinx.search(
  :select => '*, WEIGHT() + IF(publish_at>NOW()-2592000,1000,0) AS custom_weight',
  :order  => 'custom_weight DESC'
)

将使用“全文”搜索权重,但如果在上个月添加 1000。

使用 sphinx NOW() 函数 http://sphinxsearch.com/docs/current.html#expr-func-now

您可能还需要字段权重 https://freelancing-gods.com/thinking-sphinx/searching.html#fieldweights 提升 title 匹配超过“文本”匹配

:field_weights => { :title=>10 }

把所有东西放在一起(如果 ruby​​ 语法正确的话......)

NewPost.search( query , 
  :select => '*, weight() + IF(publish_at>NOW()-2592000,1000,0) as custom_weight',
  :order  => 'custom_weight DESC', 
  :field_weights => { :title=>10 }
)

...理论上,标题匹配将是第一个。最近的也开始了。不完全符合您的要求,但很接近。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多