繁体   English   中英

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

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

我想做的事

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

使用这个gem,我想创建用户模型的索引,包括名为full_name的方法的输出。 User具有id, first_name, last_name列。

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

我做了什么

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

我提到了这个问题。 在ElasticSearch中对方法的结果编制索引(Tire + ActiveRecord)

但这不起作用,最终在响应中不包含full_name索引。

我是Elasticsearch和elasticsearch-rails的新手。 我怎样才能解决这个问题?

抱歉,我只是忘了重新加载代码中的更改! 并且它不使用as: 'full_name'在当前版本的gem中as: 'full_name'

暂无
暂无

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

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