2014-04-22 70 views
2

我有Ruby on Rails 4項目與Thinking Sphinx 3。我有幾個型號:思維獅身人面像 - 與布爾字段嵌套關聯

class Investment < ActiveRecord::Base 
    has_many :investment_parts 
end 

class InvestmentPart < ActiveRecord::Base 
    belongs_to :investment 
    has_many :check_orders, dependent: :destroy 
    has_many :legal_entity_insurance_companies, through: :check_orders, class_name: "LegalEntities::InsuranceCompany" 
end 

class CheckOrder < ActiveRecord::Base 
    belongs_to :legal_entity_insurance_company, class_name: "LegalEntities::InsuranceCompany" 
    belongs_to :investment_part 
end 

我需要CheckOrders,其中有布爾字段approved找到Investmens。

create_table "check_orders", force: true do |t| 
    ... 
    t.boolean "approved", default: false 
    end 

所以,我想的是:

ThinkingSphinx::Index.define :investment, with: :active_record do 
    indexes investment_parts.check_orders.approved, as: :insuranced_investment 
end 

,但搜索找到什麼:

2.0.0p353 :008 > CheckOrder.pluck(:approved) 
    (0.6ms) SELECT "check_orders"."approved" FROM "check_orders" 
=> [true, true, true] 
2.0.0p353 :009 > Investment.search("", conditions: {insuranced_investment: true}) 
    Sphinx Retrying query "SELECT * FROM `investment_core` WHERE MATCH('@insuranced_investment true') AND `sphinx_deleted` = 0 LIMIT 0, 20 OPTION max_matches=50000; SHOW META" after error: Lost connection to MySQL server during query 
    Sphinx Query (3.5ms) SELECT * FROM `investment_core` WHERE MATCH('@insuranced_investment true') AND `sphinx_deleted` = 0 LIMIT 0, 20 OPTION max_matches=50000 
    Sphinx Found 0 results 
=> [] 

所以我決定嘗試屬性過濾器:

ThinkingSphinx::Index.define :investment, with: :active_record do 
    has investment_parts.check_orders.approved, as: :insuranced_investment_attr 
end 

但它產生的錯誤:

$ rake ts:rebuild 
Stopped searchd daemon (pid: 15516). 
Generating configuration to /Projects/my-project/config/development.sphinx.conf 
Sphinx 2.1.4-release (rel21-r4421) 
... 
using config file '/Projects/my-project/config/development.sphinx.conf'... 
... 
indexing index 'investment_core'... 
ERROR: source 'investment_core_0': expected attr type ('uint' or 'timestamp' or 'bigint') in sql_attr_multi, got 'bool insuranced_investment_attr from field'. 
ERROR: index 'investment_core': failed to configure some of the sources, will not index. 
... 
total 15 reads, 0.000 sec, 0.7 kb/call avg, 0.0 msec/call avg 
total 50 writes, 0.000 sec, 0.5 kb/call avg, 0.0 msec/call avg 
Started searchd successfully (pid: 15556). 

我該如何解決這個問題?

回答

2

Daiku是正確的,屬性會滿足您的需要更好的,但事實是,獅身人面像無法處理boolean類型的多值屬性。

所以,需要一點解決方法 - 您需要確保包含check_orders的連接,然後您需要將SQL布爾轉換爲整數(true爲1,false爲0)。我認爲以下應該做的伎倆(選擇您使用的數據庫的選項):

join investment_parts.check_orders 

# for PostgreSQL: 
has "array_to_string(array_agg(DISTINCT (CASE check_orders.approved THEN 1 ELSE 0 END)), ',')", 
    as: :insuranced_investment_attr, type: :integer, multi: true 
# for MySQL: 
has "GROUP_CONCAT(DISTINCT (CASE check_orders.approved THEN 1 ELSE 0 END) SEPARATOR ',')", 
    as: :insuranced_investment_attr, type: :integer, multi: true 
+0

謝謝你的答案,帕特!但是,PostgreSQL的解決方案不起作用。我在我的'investment_index'文件中粘貼了你的代碼,但是之後我在重建索引時遇到這樣的錯誤:'indexing index'investment_core'... 錯誤:index'investment_core':sql_range_query:ERROR:語法錯誤處於或接近「THEN 「 LINE 1:... ng(array_agg(DISTINCT(CASE check_orders.approved THEN 1 ELS ... (DSN = pgsql:// serj:*** @ localhost:5432/my-project_development)。如何修復... – ExiRe

+2

它看起來像我發現了問題!它應該是'CASE當...'。:) – ExiRe

+0

再次感謝您的解決方案!PostgreSQL的編輯後它完美!真棒! – ExiRe

0

屬性過濾器絕對是一種布爾型字段。試着告訴獅身人面像的屬性類型:

has investment_parts.check_orders.approved, as: :insuranced_investment_attr, type: :boolean 
相關問題