2012-07-18 63 views
0

我在我的Rails應用程序中思考獅身人面像有嚴重的問題。我的應用程序的規格如下:思考獅身人面像,過濾與協會?

  • 的Rails 3.2.6
  • 的Ruby 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給了我錯誤的結果。

當我只用一個過濾器,例如pricewidth,結果都OK,但是當我試圖合併過濾器,像price and width,我得到包含與給定price OR width,不提供結果pricewidth

當我使用範圍搜索時,大多數時間:with參數是範圍而不僅僅是整數值。使用

編輯1個
查詢IM:

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

它仍然給我哪裏任何提供具有高度的網頁範圍或任何報價具有該範圍內的寬度或與價格相同的東西。

+0

你能發佈搜索電話嗎? – 2012-07-18 16:24:50

+0

當然,我已經編輯了我的帖子。 – skipi 2012-07-18 20:08:19

回答

1

好吧,我找到了解決方案。斯芬克斯給我錯誤結果的原因是因爲他在單頁內集合了所有優惠。我必須將索引從網頁移動到優惠,現在查詢按預期工作。我提供的索引如下所示:

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

隨着我正在研究的項目,它不使用連接表 - 但這應該不重要。除正在執行的連接之外,索引是同樣的。

+0

我用示例查詢更新了我的問題。 – skipi 2012-07-19 06:34:24