2
votes

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?

1
Did you remember to include it in your model user.rb? You need include UserSearchable at the top of class definition.lacostenycoder
Thank you for comment. Of course, I include it ! I can index first_name and last_name correctly!Taichi
did you try adding methods: [:country_ja, :full_name], take a look at github.com/elastic/elasticsearch-rails/tree/master/…lacostenycoder
You can also do this by using Elasticsearch mapping, see elastic.co/guide/en/elasticsearch/reference/current/…sramalingam24
Sorry :country_ja was typo of :full_name. fixed it.Taichi

1 Answers

0
votes

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