简体   繁体   中英

Indexing Method on Object Returned From Method - Elasticsearch

I'm using the elasticsearch-rails gem with Rails 6 . I'm trying to index the return value of a carrierwave function asset_host . I can index the file function, but I'm not sure of the syntax to index the asset_host method on the Uploader object returned by `file.

From my model, I can access the function with the following code:

Asset.first.file.asset_host

I've added the asset_host file to my mapping with this code:

indexes :file do
    indexes :asset_host, type: "text"
end

And this is my attempt to add the method to the as_indexed_json function which is where I'm having issues:

def as_indexed_json(options={})
    as_json(methods: {file: [:asset_host]})
end

Here is the error I am getting with this code for as_indexed_json when I try to reindex my documents:

TypeError ([:file, [:asset_host]] is not a symbol nor a string)

I guess this functionality isn't supported? Is there a workaround?

Seems like an as_json problem. Maybe you could try

as_json(
  only: [],  # exclude all "normal" attributes
  include: {file: {only: :asset_host}}
)

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