0
votes

I have a product table and a variants table

Product - title, description, id
Variant - id, product_id, size_label, qty, price

Product has_many variants
Variant belongs_to product

I have defined my product index like

ThinkingSphinx::Index.define :product, :with => :active_record, delta: true do
  indexes title, facet: true
  indexes variants.size_label, as: :sizelabel, facet: true

  has variants.id, as: :variant_ids
  has variants.qty, as: :variant_qty
end

If I have product with 5 variants, eg with size_label S, M, L, XL, XXL then the sizelabel facet is returned as one string "S M L XL XXL"

Am I setting up the index wrong - expecting the variants to be returned as a separate hash (like other facets are)

Using Rails 3.2.19, Sphinx 2.1.9, Thinking Sphinx 3.1.1

1

1 Answers

1
votes

If fields are aggregates (which is true of your size labels field), the values are concatenated together as a single string, as Sphinx only understands single string values for fields, not arrays.

Also, it's worth noting that while Sphinx does support single string value attributes, and multi-value integer/timestamp/bigint attributes, but not multi-value string attributes. So, what I'd propose is the following: pull size labels out into a separate table, then have size_label_id in your Variant model. Then, add the following attribute:

has variants.size_label_id, as: :size_label_ids, facet: true

When you get your facet results back, you'll need to translate them to the meaningful text. I've written some code that should be useful (though some adapting is likely required).