Question

Suppose I have a series of embedded or child documents that I'd like to search on, but return their parents as my results, like Buildings and Units:

Building A
- Unit 1F
- Unit 1R
- Unit 2F: 1200 sq ft
- Unit 2R: 2300 sq ft

Building B
- Unit 202: 500 sq ft
- Unit 203: 650 sq ft

Now suppose I want to return all buildings that have units >= 1000 sq ft. How would I do that?

Was it helpful?

Solution

Store the unit sizes as an Array:

class Building

  def search_data
    {
      # ... other fields
      unit_sq_ft: units.map(&:sq_ft)
    }
  end

end

and search with:

Building.search "pool", where: {unit_sq_ft: {gte: 1000}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top