gpt4 book ai didi

ruby-on-rails - NoMethodError(# 的未定义方法 `highlight'

转载 作者:数据小太阳 更新时间:2023-10-29 06:35:54 25 4
gpt4 key购买 nike

我将为我的 ruby​​ on rails 项目使用 Elasticsearch 。当我搜索一些在我的文章中使用过多的词时,出现此错误。

NoMethodError (undefined method `highlight' for #<Elasticsearch::Model::Response::Result:0x007f062ed26708>)

我在日志制作中得到了这个。这就是我所做的一切:在 Controller 中:

      # POST /search/article
def search
render json: Article.search(params[:query]), each_serializer: ElasticsearchResultsSerializer
end

这是我的article.rb模型

  #default_scope { order('created_at DESC') }
scope :visible, -> { where(enabled: true) }

after_commit on: [:create] do
self.keywords = self.keywords.each {|str| str.force_encoding("UTF-8")}
__elasticsearch__.index_document if self.enabled?
end

after_commit on: [:update] do
self.keywords = self.keywords.each {|str| str.force_encoding("UTF-8")}
__elasticsearch__.update_document if self.enabled?
end

after_commit on: [:destroy] do
__elasticsearch__.delete_document
end

settings index: { number_of_shards: 1, number_of_replicas: 0 }

mappings dynamic: 'false' do
indexes :content, type: "string", index_options: 'offsets'
indexes :title, type: "string"
indexes :description, type: "string"
indexes :category, type: "string"
indexes :created_at, type: "date"
indexes :keywords, type: "string"
end
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: ['title^10', 'content^5', 'description^2', 'keywords', 'category']
}
},
highlight: {
pre_tags: ['<em>'],
post_tags: ['</em>'],
fields: { title: {}, content: {} }
}
}
)
end

def as_indexed_json(options={})
as_json(
only: [:content, :title, :id, :category, :keywords, :description]
)
end

我还使用了序列化器

class ElasticsearchResultsSerializer < ActiveModel::Serializer
attributes :_id, :highlight, :_score, :_source

def _source
@article = object._index.singularize.capitalize.constantize.find(object._id)
@serializer = "#{object._index.singularize.capitalize}Serializer".constantize
@serializer.new(@article)
end
end

最佳答案

也许是一个愚蠢的观察,但你有没有尝试改变值(value)

:highlight:_highlight ?

class ElasticsearchResultsSerializer < ActiveModel::Serializer
attributes :_id, :_highlight, :_score, :_source

def _source
@article = object._index.singularize.capitalize.constantize.find(object._id)
@serializer = "#{object._index.singularize.capitalize}Serializer".constantize
@serializer.new(@article)
end
end

关于ruby-on-rails - NoMethodError(#<Elasticsearch::Model::Response::Result> 的未定义方法 `highlight',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43464752/

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