Reputation: 2941
I have a Rails App in which I want to use Thinking Sphinx for search. I have a has many though relationship between the following models, Product
has many Type
s through ProductType
.
# 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
In my ProductsController
index action I want to be able to filter which products are shown in the view based on given Variant
ids.
My relevant indexes currently looks like this (note, I haven't used ThinkingSphinx in a long time):
# 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
I currently pass an array of :product_types
ids in a link_to
, like this (let me know if there is a better way to do it):
= link_to "Web shop", products_path(product_types: Type.all.map(&:id), brand: Brand.all.map(&:id)), class: "nav-link"
In my ProductsController
I try to filter the result based on the given Type
ids like this:
product_types = params[:product_types]
@products = Product.search with_all: { product_types: product_types.collect(&:to_i) }
When I run rake ts:rebuild
I get the following error:
indexing index 'product_type_core'...
ERROR: index 'product_type_core': No fields in schema - will not index
And when I tries to view the view in the browser I get the following error:
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
Any ideas in how to properly set up my indexes (and query) for this case?
Upvotes: 0
Views: 688
Reputation: 16226
There's a few issues to note here:
Firstly, the error you're seeing during rake ts:rebuild
is pointing out that you've not set any fields in your ProductType Sphinx index - no indexes
calls for text data you wish to search on. Are you actually searching on ProductType at all? If so, what text are you expecting people to match by?
If you're not searching on that model, there's no need to have a Sphinx index for it.
Secondly, the issue with your search - you're filtering on product_types
with integers, which makes sense. However, in your index, you've defined product_types
as a field (using indexes
) rather than an attribute (using has
). Given it's integer values and you're likely not expecting someone to type in an ID into a search input, you'll almost certainly want this to be an attribute instead - so change the indexes
to a has
for that line in your Product index definition, and run ts:rebuild
.
Upvotes: 1