[英]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.