简体   繁体   English

索引非模型对象以在Elasticsearch中的Rails中进行搜索

[英]Indexing non-model objects for search in Rails with Elasticsearch

Right now my Rails application allows users to search only the database fields. 现在,我的Rails应用程序允许用户仅搜索数据库字段。 They can upload a picture and then search with the name. 他们可以上传图片,然后搜索名称。 I am also extracting the metadata and storing it in YAML files. 我还提取元数据并将其存储在YAML文件中。 So for fileA, the metadata is stored in fileA.yml file. 因此,对于fileA,元数据存储在fileA.yml文件中。

With Elasticsearch, I was able to index the table 'file' and allow users to search according to filetype, filename, etc. (whatever's stored on the DB). 使用Elasticsearch,我能够为表“文件”建立索引,并允许用户根据文件类型,文件名等(无论存储在数据库中的是什么)进行搜索。 However, I'd like to provide a metadata search also, so how do I index that file's YAML file also? 但是,我也想提供元数据搜索,那么如何也索引该文件的YAML文件呢?

I'm okay with switching to some other search provider, like Solr, if it's needed also. 我也可以切换到其他搜索提供商,例如Solr,如果需要的话。

You don't mention what library (elasticsearch-rails, searchkick etc.) you are using to integrate with elasticsearch but they all allow you to customise the data that is sent for indexing. 您没有提到要与Elasticsearch集成的库(elasticsearch-rails,searchkick等),但是它们都允许您自定义发送给索引的数据。

For example with elasticsearch-rails you override the as_indexed_json method, for example 例如,使用as_indexed_json -rails覆盖as_indexed_json方法,例如

def as_indexed_json(options={})
  as_json(options).merge(data_from_yaml)
end

for searchkick, you would do 对于搜索踢,你会做

def search_data
  as_json.merge(data_from_yaml)
end

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

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