使用 Elasticsearch 索引非模型对象以在 Rails 中搜索
Indexing non-model objects for search in Rails with Elasticsearch
现在我的 Rails 应用程序允许用户仅搜索数据库字段。他们可以上传图片,然后使用名称进行搜索。我还提取元数据并将其存储在 YAML 文件中。因此对于文件 A,元数据存储在 fileA.yml 文件中。
使用 Elasticsearch,我能够索引 table 'file' 并允许用户根据文件类型、文件名等(无论存储在数据库中的什么)进行搜索。但是,我也想提供元数据搜索,那么我该如何索引该文件的 YAML 文件呢?
如果需要的话,我可以切换到其他搜索提供程序,例如 Solr。
您没有提及您使用什么库(elasticsearch-rails、searchkick 等)来与 elasticsearch 集成,但它们都允许您自定义发送用于索引的数据。
例如使用 elasticsearch-rails 覆盖 as_indexed_json
方法,例如
def as_indexed_json(options={})
as_json(options).merge(data_from_yaml)
end
对于 searchkick,你会做
def search_data
as_json.merge(data_from_yaml)
end
现在我的 Rails 应用程序允许用户仅搜索数据库字段。他们可以上传图片,然后使用名称进行搜索。我还提取元数据并将其存储在 YAML 文件中。因此对于文件 A,元数据存储在 fileA.yml 文件中。
使用 Elasticsearch,我能够索引 table 'file' 并允许用户根据文件类型、文件名等(无论存储在数据库中的什么)进行搜索。但是,我也想提供元数据搜索,那么我该如何索引该文件的 YAML 文件呢?
如果需要的话,我可以切换到其他搜索提供程序,例如 Solr。
您没有提及您使用什么库(elasticsearch-rails、searchkick 等)来与 elasticsearch 集成,但它们都允许您自定义发送用于索引的数据。
例如使用 elasticsearch-rails 覆盖 as_indexed_json
方法,例如
def as_indexed_json(options={})
as_json(options).merge(data_from_yaml)
end
对于 searchkick,你会做
def search_data
as_json.merge(data_from_yaml)
end