【问题标题】:Thinking sphinx, filtering with associations?思考狮身人面像,过滤关联?
【发布时间】:2012-07-18 13:15:50
【问题描述】:

我的 Rails 应用程序中的 Thinking Sphinx 存在严重问题。我的应用规格如下:

  • Rails 3.2.6
  • Ruby 1.9.3
  • 狮身人面像 2.0.4 (r3135)
  • 思考狮身人面像 2.0.12

在我的应用程序中,我有一个 Page 模型和一个 Offer 模型,pages 有很多 offersPage 模型中定义的索引如下所示:

define_index do

  #other indexes defined here
  #...

  has offers.width, as: :offers_width
  has offers.height, as: :offers_height
  has offers.price, as: :offers_price

  set_property delta: true
end

然后我在 Page 模型上进行搜索,其中 pages 是根据搜索查询和条件选择的。但是,当我尝试使用:with 过滤器时,Sphinx 给了我错误的结果。

当我只使用一个过滤器时,例如pricewidth,结果还可以,但是当我尝试组合过滤器时,例如price and width,我得到的结果包含给定的报价pricewidth,而不是 pricewidth

当我使用范围搜索时,大多数时候:with 参数是范围,而不仅仅是整数值。

编辑 1
使用以下方式查询即时消息:

Page.search Riddle.escape(query), conditions: conditions, with: has_cond, star: true, per_page: Page.per_page, page: page

has_cond 是:

{:offers_height=>[175..200], :offers_width=>[175..200], :offers_price=>[10..80]}

它仍然会为我提供 ANY 报价的高度在该范围内或 ANY 报价的宽度在该范围内的页面该范围或与价格相同的东西。

【问题讨论】:

  • 你能发布搜索电话吗?
  • 当然,我已经编辑了我的帖子。

标签: ruby-on-rails ruby activerecord thinking-sphinx


【解决方案1】:

好的,我找到了解决方案。 sphinx 给我错误结果的原因是因为他在单个页面中汇总了所有报价。我不得不将索引从页面移动到报价,现在查询按预期工作。我的报价索引如下所示:

define_index do

  indexes page(:description), as: :page_description
  indexes page(:address), as: :page_address
  indexes page(:title), as: :page_title
  indexes page(:status), as: :page_status
  indexes page.tags(:name), as: :page_tags_name
  indexes page.category(:id), as: :page_category_id
  indexes page.country(:id), as: :page_country_id
  indexes page(:verified), as: :page_verified

  has page(:id), as: :page_id
  has :width
  has :height
  has :price
end

我需要先查询sphinx,然后进行主动记录查询。

@sphinx_query = Offer.search Riddle.escape(query), with: has_cond, 
    conditions: conditions, group_function: :attr, group_by: 'page_id', 
    star: true, per_page: Page.per_page, page: page
@pages = Page.find(@sphinx_query.map(&:page_id))

【讨论】:

    【解决方案2】:

    我相信错误将出现在您的搜索查询本身中。我刚刚进行了一些测试,一切都按预期工作......有范围和没有范围。

    控制器中的搜索代码:

    @titles = Title.search(params[:q], 
                                  :rank_mode => :bm25,
                                  :match_mode => :any,
                                  :star => true,
                                  :limit => 35,
                                  :with_all => {
                                    :runtime => [75..100],
                                    :year => 1976
                                  })
    

    索引代码:

    class Title < ActiveRecord::Base
        ...
        define_index do
          # fields
          indexes clean_name, :sortable => true
          # attributes
          has id, year, runtime, created_at, updated_at
        end
        ...
    end
    

    搜索结果 (JSON)

    {
        resultList: [
            {
                director: "Allan Arkush, Joe Dante",
                id: 34089,
                name: "Hollywood Boulevard",
                rating: "R",
                runtime: 83,
                year: 1976,
                text: "Hollywood Boulevard (1976)",
                identify: "title"
            },
            {
                director: "Patrick M. Wright",
                id: 40875,
                name: "Hollywood High",
                rating: "R",
                runtime: 81,
                year: 1976,
                text: "Hollywood High (1976)",
                identify: "title"
            }
        ],
        resultCount: 2
    }
    

    对于我正在处理的项目,它不使用连接表——但这不重要。除了执行连接之外,索引仍然相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多