0

I have two models, named as Products and Variants, in which Variant model have association with Products as a Product have many Variants. Variant model have field named as "available_on" ... I want to implement search using two dates as check-in n checkout dates.

.

if variants for a product available for each date , check-in date to checkout date, result will map all those products and it is the result....

. . guide me how i should give conditions using Sunspot:solr

roughly my models are like this

product
{
product_id integer
has_many variants
}

variant
{
variant_id integer
available_on date
belongs_to product
}

check-in n checkout are the inputs for the search.

Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
Gopal S Rathore
  • 9,885
  • 3
  • 30
  • 38
  • i guess you can find answer - [here][1] [1]: http://stackoverflow.com/questions/2730295/exclude-draft-articles-from-solr-index-with-sunspot – okliv Jan 28 '13 at 04:52
  • this link specifies simple search for one model, I have asked for two or more models having associations. – Gopal S Rathore Jan 28 '13 at 05:11

2 Answers2

0

it will be easier to answer if you put your models in question but in general way if you have has_many relationship than you should index nested model

#variant model

searchable do

  integer :product_id
  time :check_in     
  time :check_out     

end

if you need to index something from parent has_many model you can use :multiple=>true option in this way

#product model

def variant_ids     
  variants.collect(&:id)
end

searchable do

  integer :variant_ids, :multiple=>true
  ...

end
okliv
  • 3,909
  • 30
  • 47
  • I have added models roughly, I got How to make searchable, now how to put condition for search? – Gopal S Rathore Jan 28 '13 at 05:46
  • provide example of condition you want to implement – okliv Jan 28 '13 at 05:48
  • I want to search for products who are available for checking date to checkout date, means for each date that product have a variant. for example If I input 2013-01-24 in check-in and 2013-01-26 as checkout date, then results will show the products having variants for date 24, 25 n 26. – Gopal S Rathore Jan 28 '13 at 05:57
  • so you need to index variants, search for it between two dates, than just group result of search by product using `@variants.group_by(&:product)` and render products from this array of hashes (`@variants.group_by(&:product).keys`) in any way you want... – okliv Jan 28 '13 at 06:03
0

Index each model, and return the variant/product you need from SOLR. If you're using Spree by the way, there's a gem for integrating w/ sunspot and spree. I just forked it: https://github.com/banane/spree_sunspot_search

Anna Billstrom
  • 2,482
  • 25
  • 33