2017-10-15 219 views
0

我有一个Rails应用程序,我想在其中使用思维狮身人面像进行搜索。我有一个有很多虽然关系以下型号,Product有很多Type秒通过ProductTypeRails 5,思维狮身人面像,索引和搜索已通过关系meny

# Product.rb 
has_many :product_types 
has_many :types, through: :product_types 

# Type.rb 
has_many :product_types 
has_many :products, through: :product_types 

# ProductType.rb 
belongs_to :product 
belongs_to :type 

在我ProductsController索引操作我希望能够过滤哪些产品的基础上给出Variant IDS视图中显示。

我的相关指标目前看起来是这样的(注意,我没有在很长一段时间使用ThinkingSphinx):

# product_index.rb 
ThinkingSphinx::Index.define :product, :with => :active_record do 
    indexes name, :sortable => true 
    indexes description 
    indexes brand.name, as: :brand, sortable: true 

    indexes product_types.type.id, as: :product_types 

    has created_at, updated_at 
end 

# type_index.rb 
ThinkingSphinx::Index.define :type, :with => :active_record do 
    indexes name, :sortable => true 
end 

# product_type_index.rb 
ThinkingSphinx::Index.define :product_type, :with => :active_record do 
    has product_id, type: :integer 
    has type_id, type: :integer 
end 

我目前通过的:product_types ID的数组在link_to,像这样(让我知道是否有更好的方式来做到这一点):

= link_to "Web shop", products_path(product_types: Type.all.map(&:id), brand: Brand.all.map(&:id)), class: "nav-link" 

在我ProductsController我试图筛选基于给定Type IDS这样的结果:

product_types = params[:product_types] 
@products = Product.search with_all: { product_types: product_types.collect(&:to_i) } 

当我运行rake ts:rebuild我得到以下错误:

indexing index 'product_type_core'... 
ERROR: index 'product_type_core': No fields in schema - will not index 

当我试图查看在浏览器中我得到以下错误的观点:

index product_core: no such filter attribute 'product_types' 
- SELECT * FROM `product_core` WHERE `sphinx_deleted` = 0 AND 
`product_types` = 1 AND `product_types` = 2 AND `product_types` = 3 
LIMIT 0, 20; SHOW META 

如何任何想法正确设置我的索引(和查询)为这种情况?

回答

1

有几个问题要注意这里:

首先,你在rake ts:rebuild所看到的错误被指出你没有设置你的ProductType狮身人面像索引的字段 - 没有indexes呼吁文本数据你希望搜索。你真的在搜索ProductType吗?如果是这样,你期望人们匹配哪些文本?

如果您不是在该模型上搜索,则不需要为其创建Sphinx索引。

其次,您的搜索问题 - 您正在使用整数对product_types进行筛选,这很有意义。但是,在索引中,您已将product_types定义为字段(使用indexes)而不是属性(使用has)。鉴于它是整数值,并且您可能不希望有人在搜索输入中键入ID,几乎可以肯定这是一个属性 - 所以请将产品索引定义中该行的indexes更改为has ,并运行ts:rebuild

+0

Thanks @pat!我已经更新了我的索引,如下所示:'有product_types.type.id,如::type_ids'。我在[Github](https://github.com/pat/thinking-sphinx-examples)上找到了你的高级搜索示例,并且我试图做类似的事情。我的搜索的相关部分如下所示:'@products = Product.search params [:query],with_all:options [:with_all]','options [:with_all]'看起来像这样:'{:type_ids => [2,3]}。但我总是得到0个结果:'SELECT * FROM'product_core'WHERE'sphinx_deleted'= 0 AND'type_ids'= 2 AND'type_ids'= 3 LIMIT 0,20'。任何想法我失踪? – Anders

+0

只需确认:您是否期望获得所有连接到* type id 2和type id 3的产品?(而不是连接到任一个但不一定两者的产品)。此外,它没有显示在你共享的代码中,但是你是否正在将'params [:product_types]'翻译成Type实例? – pat

+0

不,我希望所有的产品有1或3型。我已经改变了这一点'params [:product_types]'从我原来的问题。我改成了:'options = {:with_all => {}}','options [:with_all] [:type_ids] = params [:filter] [:type] .keys.map(&:to_i)'(导致'{:type_ids => [2,3]}'和'@products = Product.search params [:query],with_all:options [:with_all]',我可以用更新后的代码更新我的问题。对你的帮助@pat! – Anders