繁体   English   中英

Elastic Search Rails查找关联的模型属性

[英]Elastic Search Rails find associated model attributes

我在Rails项目中使用Elastic Search。 我正在使用elasticsearch-model gem进行实时全文搜索。 假设模型A属于模型B。如果我在模型AI上进行搜索,则根据我的搜索参数将获得10条关于A的记录。

A.search(x) => Gives 10 records which includes all attributes of A.

但是,当我通过如下方式通过A访问模型B时:ABsome_attrib

 undefined method B for <Elasticsearch::Model::Response::Result>

如何在ElasticSearch中加载关联?

我也希望搜索结果中的所有记录。 如何在搜索方法中指定match_all?

有关associated_model的信息,请参见此链接https://github.com/elastic/elasticsearch-rails/blob/master/elasticsearch-model/examples/activerecord_associations.rb

Include Elasticsearch::Model and callbacks关注的Include Elasticsearch::Model and callbacks ,并进行索引映射。
在两个关联的模型中都包含模块。

注意:不要忘记导入模型。

您可以使用as_indexed_json方法配置模型关联。 这是伪代码:

def as_indexed_json(options={})
 self.as_json(
   include: {
     b: { only: :col1}
   }
 )
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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