简体   繁体   English

如何在elasticsearch-rails中创建实例方法的索引?

[英]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 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 . 使用这个gem,我想创建用户模型的索引,包括名为full_name的方法的输出。 User has columns of id, first_name, last_name . User具有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) 在ElasticSearch中对方法的结果编制索引(Tire + ActiveRecord)

But this doesn't work, ending up with not containing full_name index in the response. 但这不起作用,最终在响应中不包含full_name索引。

I'm new to elasticsearch and elasticsearch-rails. 我是Elasticsearch和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. 并且它不使用as: 'full_name'在当前版本的gem中as: 'full_name'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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