gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:36:45 25 4
gpt4 key购买 nike

我想做什么

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

使用这个 gem,我想创建用户模型的索引,包括名为 full_name 的方法的输出。 Userid, 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

我提到了这个问题。 Index the results of a method in ElasticSearch (Tire + ActiveRecord)

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

我是 elasticsearch 和 elasticsearch-rails 的新手。我该如何解决这个问题?

最佳答案

抱歉,我只是忘记重新加载代码中的更改!在当前版本的 gem 中,它在没有 as: 'full_name' 的情况下工作。

关于ruby-on-rails - 如何在 elasticsearch-rails 中创建实例方法的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49346684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com