简体   繁体   中英

Search subdocuments in ElasticSearch with Searchkick

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?

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}}

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