0

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

  • 导轨 3.2.6
  • 红宝石 1.9.3
  • 狮身人面像 2.0.4 (r3135)
  • 思考狮身人面像 2.0.12

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

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 给了我错误的结果。

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

当我使用范围搜索时,大多数时间: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]}

它仍然给我提供任何报价在该范围内具有高度或任何报价在该范围内具有宽度或与价格相同的页面。

4

2 回答 2

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_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))
于 2012-07-19T09:36:45.877 回答
0

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

控制器中的搜索代码:

@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
}

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

于 2012-07-18T14:22:50.927 回答