简体   繁体   中英

Sunspot / solr complex query

Say i have some products that i want to search.

I want to find the products that are within some min and/or max values say length, width and height.

I can do :

any_of do

  with(:length).greater_than_or_equal_to(length_min)
  with(:length).less_than_or_equal_to(length_max)

  with(:width).greater_than_or_equal_to(width_min)
  with(:width).less_than_or_equal_to(width_min)

  with(:height).greater_than_or_equal_to(height_min)
  with(:height).less_than_or_equal_to(height_min)
end

This will give me products that match any of these.

I want to return all products but sorted by the products that match the most criteria.

So for example :

Product A that is within all the length, width and height ranges with be first, then Product C that matches just length and width and then Product B that doesnt match any of the length, width or height ranges.

Anyone know how to do this ?

Thanks a lot Rick

Looking at the documentation on https://github.com/sunspot/sunspot , it appears that by using the any_of do will return results if any of the items match any criteria.

# Posts that do not have an expired time or have not yet expired
Post.search do
  any_of do
    with(:expired_at).greater_than(Time.now)
    with(:expired_at, nil)
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM