简体   繁体   中英

How to create index of an instance method in elasticsearch-rails?

What I want to do

https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model

Using this gem, I wanna create indexes of User model, including output of method named full_name . User has columns of id, first_name, last_name .

class User
  ...
  def full_name
    first_name + last_name
  end
  ...
end

What I did

module UserSearchable
  extend ActiveSupport::Concern

  included do
    include Searchable
    settings index: {
      ...
    }

    mappings do
      indexes :id, type: :integer
      indexes :first_name, type: text
      indexes :last_name, type: text
      indexes :full_name, type: text, as: 'full_name'
    end

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

I referred to this question. Index the results of a method in ElasticSearch (Tire + ActiveRecord)

But this doesn't work, ending up with not containing full_name index in the response.

I'm new to elasticsearch and elasticsearch-rails. How can I fix this?

Sorry, I just forgot to reload changes in the code! And It works without as: 'full_name' in current version of the gem.

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