3

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?

Taichi
  • 2,297
  • 6
  • 25
  • 47
  • Did you remember to include it in your model user.rb? You need `include UserSearchable` at the top of class definition. – lacostenycoder Mar 18 '18 at 10:29
  • Thank you for comment. Of course, I include it ! I can index first_name and last_name correctly! – Taichi Mar 18 '18 at 10:32
  • did you try adding `methods: [:country_ja, :full_name], ` take a look at https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model#relationships-and-associations – lacostenycoder Mar 18 '18 at 10:40
  • You can also do this by using Elasticsearch mapping, see https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html – sramalingam24 Mar 18 '18 at 14:36
  • Sorry :country_ja was typo of :full_name. fixed it. – Taichi Mar 19 '18 at 01:48

1 Answers1

0

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

Taichi
  • 2,297
  • 6
  • 25
  • 47