R
R
rda19022014-11-27 11:19:35
Ruby on Rails
rda1902, 2014-11-27 11:19:35

What is the correct way to add a has_and_belongs_to_many relationship to a sphinx index?

Hello everyone)) In the project, sphinx is used as a search. There are entities

class Catalog::Product < ActiveRecord::Base
  has_and_belongs_to_many :writers, class_name: 'Catalog::Author', join_table: "catalog_authors_products"
end

class Catalog::Author < ActiveRecord::Base
  has_and_belongs_to_many :products, class_name: 'Catalog::Product', join_table: 'catalog_authors_products'
end

Index file
ThinkingSphinx::Index.define 'catalog/product', with: :active_record do
  indexes :title, facet: true
  indexes :id
  indexes :sku, as: :sku_field
  indexes writers(:first_name), as: :writer_first_name

  has created_at
  has available
  has price
  has sku
end

when adding
indexes writers(:first_name), as: :writer_first_name
and when trying to do a rake ts:rebuild an error pops up ERROR: index 'catalog_product_core': sql_range_query: Unknown column 'catalog_authors.product_id' in 'on clause' (DSN=mysql://root:***@localhost:3306/byblos_dev) . Googled, everyone writes the habtm connection in this format
indexes writers(:first_name), as: :writer_first_name
or so , but the error is the same. In the development.sphinx.conf fileindexes writers.first_name, as: :writer_first_name
sql_query = SELECT SQL_NO_CACHE catalog_products.id * 2 + 0 AS id, catalog_products.title AS title, catalog_products.authors AS authors, catalog_products.id AS id, catalog_products.sku AS sku_field, GROUP_CONCAT(DISTINCT catalog_authors.first_name SEPARATOR ' ') AS writer_first_name , catalog_products.id AS sphinx_internal_id, 'Catalog::Product' AS sphinx_internal_class, 0 AS sphinx_deleted, UNIX_TIMESTAMP(catalog_products.created_at) AS created_at, catalog_products.available AS available, catalog_products.price AS price, catalog_products.sku AS sku FROM catalog_products LEFT OUTER JOIN catalog_authors ON catalog_authors.product_id = catalog_products.id WHERE (catalog_products.id BETWEEN $start AND $end) GROUP BY catalog_products.id, catalog_products.title, catalog_products.authors, catalog_products.id, catalog_products.sku,catalog_products.id, catalog_products.created_at, catalog_products.available, catalog_products.price, catalog_products.sku ORDER BY NULL
there is a string
LEFT OUTER JOIN catalog_authors ON catalog_authors.product_id = catalog_products.id
apparently the sphinx does not correctly determine the connection!?
How to correctly add the has_and_belongs_to_many relation to the index?
Thanks in advance))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question