简体   繁体   中英

Indexing non-model objects for search in Rails with Elasticsearch

Right now my Rails application allows users to search only the database fields. They can upload a picture and then search with the name. I am also extracting the metadata and storing it in YAML files. So for fileA, the metadata is stored in fileA.yml file.

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). However, I'd like to provide a metadata search also, so how do I index that file's YAML file also?

I'm okay with switching to some other search provider, like Solr, if it's needed also.

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.

For example with elasticsearch-rails you override the as_indexed_json method, for example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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