I'm having trouble with figuring out the proper way to configure indexes on 
a Model using relations. I'd like to search for Orders by Customer Name, 
however the name lives in separate model which Order has_one of. So my 
index looking like so:

ThinkingSphinx::Index.define :order, with: :active_record, :delta => 
ThinkingSphinx::Deltas::DelayedDelta do
  join shipping_address
  indexes "concat_ws(' ', addresses.first_name, addresses.last_name)",
    as: :customer_name,
    sortable: true,
    infixes: true

  set_property enable_star: true
  set_property min_infix_len: 2
end

This works ok as far as indexing the proper information, however the delta 
indexing only works if I modify an address instance through its order:

order = Order.first
order.shipping_address.first_name = 'Boris'
order.save # address model and order index get the expected updates

address = Address.first
address.first_name = 'Natasha'
address.save # address model gets updated, but now the Order index is out 
of date

I can completely understand how this would be the expected behavior -- the 
index is on Order, so it only knows to change if Order is changed. I tried 
a couple of ways of forcing Address to save the model it belongs to, 
however nothing works.

class Address
  belongs_to :order, touch: true # the touch happens, but no delta index 
hooks are called

  after_commit :save_order # infinite loop
end

So that seems like a dead end. Then my thinking was to put the index on 
address, but I'm not sure how I can combine the address index with 
searching against other Order fields/attributes.

Order.joins(:shipping_address).where(shipping_address: { id: 
Address.search('Boris').map(&:id) }) 
# this doesn't seem right, even if there were no other Order search 
fields/attributes. For one thing,
# the address search could return addresses not corresponding to any 
order's shipping address, 
# and has pagination, so 20 of 200 address results could page 1 has 17 
orders, and page 2 has 12, etc etc. 

Feels like I must be making things harder for myself somehow, but I can't 
see what may be an obvious solution.

-- 
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/thinking-sphinx.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to