Ruby on rails Rails 5,thinking sphinx,索引和搜索通过关系实现了功能

Ruby on rails Rails 5,thinking sphinx,索引和搜索通过关系实现了功能,ruby-on-rails,ruby,ruby-on-rails-5,sphinx,thinking-sphinx,Ruby On Rails,Ruby,Ruby On Rails 5,Sphinx,Thinking Sphinx,我有一个Rails应用程序,我想使用Thinking Sphinx进行搜索。我在以下模型之间有很多关系,产品有许多类型s到产品类型 # 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_t

我有一个Rails应用程序,我想使用Thinking Sphinx进行搜索。我在以下模型之间有很多关系,
产品
有许多
类型
s到
产品类型

# 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
索引操作中,我希望能够根据给定的
变量
ID筛选视图中显示的产品

我的相关索引目前看起来是这样的(注意,我很久没有使用ThinkingSphinx了):

我当前在链接到的
中传递了一组
:产品类型
ID,如下所示(如果有更好的方法,请告诉我):

在我的
ProductsController
中,我尝试根据给定的
类型
ID过滤结果,如下所示:

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
当我尝试在浏览器中查看视图时,会出现以下错误:

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

关于如何为这种情况正确设置索引(和查询)有什么想法吗?

这里有几个问题需要注意:

首先,在
rake ts:rebuild
过程中看到的错误指出,您没有在ProductType Sphinx索引中设置任何字段-没有
索引
调用要搜索的文本数据。你真的在搜索ProductType吗?如果是这样,您希望人们通过什么文本进行匹配

如果您不在该模型上搜索,则不需要为其设置Sphinx索引


其次,你的搜索问题——你用整数过滤
产品类型
,这很有意义。但是,在索引中,您将
产品类型
定义为字段(使用
索引
),而不是属性(使用
has
)。考虑到它是整数值,并且您可能不希望有人在搜索输入中键入ID,您几乎肯定会希望它成为一个属性-因此,将产品索引定义中该行的
索引更改为
has
,然后运行
ts:rebuild

谢谢@pat!我已将索引更新为如下所示:
具有product\u types.type.id,as::type\u id
。我在上找到了您的高级搜索示例,我也尝试过做类似的事情。我的搜索的相关部分目前看起来是这样的:
@products=Product.search参数[:query],带有所有选项[:带有所有选项]
选项[:带有所有选项]
看起来是这样的:
{:type\u id=>[2,3]}
。但我总是得到0个结果:
从'product_core'中选择*,其中'sphinx_deleted'=0,'type_id'=2和'type_id'=3限制0,20
。你知道我遗漏了什么吗?只是想确认一下:你希望得到所有同时连接到类型id 2和类型id 3的产品吗?(而不是连接到其中一个但不一定同时连接到两个的产品)。此外,它没有显示在您共享的代码中,但您是否在某个时候将
params[:product_types]
转换为类型实例?不,我希望所有产品都具有类型1或3。我用原始问题中的
params[:product\u types]
更改了该位。我已将其更改为:
options={:with\u all=>{}
options[:with\u all][:type\u id]=params[:filter][:type].keys.map(&:to\u I)
(结果为
{:type\u id=>[2,3]}
@products=Product.search参数[:query],with\u all:options[:with\u all]
,我可以稍后用更新的代码更新我的问题。感谢您的帮助@pat!对于此筛选器,您需要使用
:with
,而不是
:with\u all
。此外,您还需要确保您的属性名称与您正在筛选的内容匹配(您是否已将其设置为
在索引定义中键入\u id