使用Searchkick搜索ElasticSearch中的子文档 [英] Search subdocuments in ElasticSearch with Searchkick

查看:85
本文介绍了使用Searchkick搜索ElasticSearch中的子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一系列我想搜索的嵌入式或子文档,但是将他们的父母作为我的结果返回,如建筑物和单位:

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

建筑B
- 单位202:500平方英尺
- 单位203:650平方英尺

现在假设我要返回所有建筑物,单位> = 1000平方英尺,我该怎么做?

解决方案

将单位大小存储为数组:

  class Building 

def search_data
{
#...其他字段
unit_sq_ft:units.map(&:sq_ft)
}
end

end

并搜索:

  Building.searchpool,其中:{unit_sq_ft:{gte:1000}} 


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

这篇关于使用Searchkick搜索ElasticSearch中的子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆