Ruby on rails 无法在ElasticSearch中索引非模型字段
我将Rails 6与ElasticSearchRails一起使用。我试图索引函数的结果,但它没有出现在索引中,并且不可搜索。这是我的班级Ruby on rails 无法在ElasticSearch中索引非模型字段,ruby-on-rails,ruby,
elasticsearch,ruby-on-rails-6,Ruby On Rails,Ruby,
elasticsearch,Ruby On Rails 6,我将Rails 6与ElasticSearchRails一起使用。我试图索引函数的结果,但它没有出现在索引中,并且不可搜索。这是我的班级 class Asset < ApplicationRecord include Elasticsearch::Model include Elasticsearch::Model::Callbacks def my_field return "this is my field" end
class Asset < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def my_field
return "this is my field"
end
def as_indexed_json(options={})
as_json(only: [:my_field])
end
settings index: {
number_of_shards: 10,
max_ngram_diff: 50
},
analysis: {
filter: {
ngram_filter: {
type: "ngram",
min_gram: "3",
max_gram: "10",
}
},
analyzer: {
ngram_filter: {
type: "custom",
tokenizer: "standard",
filter: ["lowercase", "ngram_filter"]
}
}
} do
mapping do
indexes :my_field, type: "text", analyzer: "standard"
end
end
end
当我为模型中定义的数据库字段编制索引时,一切正常,但我需要索引的函数的返回值。您尝试过这个吗,我希望它能正常工作
class Asset < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def my_field
{ my_field: "this is my field" }
end
def as_indexed_json(options={})
as_json(only: [:id]).merge(my_field)
end
end
类别资产
class Asset < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def my_field
{ my_field: "this is my field" }
end
def as_indexed_json(options={})
as_json(only: [:id]).merge(my_field)
end
end