gpt4 book ai didi

ruby-on-rails - rails Elasticsearch 关系属性未编入索引

转载 作者:数据小太阳 更新时间:2023-10-29 07:49:42 33 4
gpt4 key购买 nike

基本上我有 3 个模型(书籍、章节、作者),我想在索引章节时包含一些书籍和作者属性。

这是我的Chapter.rb

class Chapter < ActiveRecord::Base
belongs_to :book, :counter_cache => true
include Elasticsearch::Model

index_name [Rails.env, model_name.collection.gsub(/\//, '-')].join('_')

mappings do
indexes :id, type: :integer
indexes :title, type: :string
indexes :description, type: :string
indexes :content, type: :string
indexes :updated_at, type: :date # Date example
indexes :book_title
indexes :book_type
indexes :author_name
indexes :book_id
end

def book_title
book.title
end
def book_type
book.book_type
end
def author_name
" #{book.author.firstname} #{book.author.lastname} "
end

def to_indexed_json
to_json methods: [:book_title, :book_type, :author_name]
end
end

http://localhost:9200/development_chapters/_mapping?pretty 显示正确的映射

{
"development_chapters" : {
"mappings" : {
"chapter" : {
"properties" : {
"author_name" : {
"type" : "string"
},
"book_title" : {
"type" : "string"
},....
}
}
}
}
}

那为什么我在搜索结果中没有得到 author_name、book_title 等...

<Elasticsearch::Model::Response::Result:0x00000105e393a0 @result=#<Hashie::Mash _id="415" _index="development_chapters" _score=1.0 _source=#<Hashie::Mash book_id=153 content="[\"Explicabo accusantium odit .\"]" created_at="2015-04-22T18:43:58.586Z" description="You can't generate the application without quantifying the cross-platform SDD bandwidth!" id=415 title="Future Communications Orchestrator" updated_at="2015-04-22T18:43:58.586Z"> _type="chapter">>

最佳答案

您正在定义错误的序列化方法。 Elasticsearch::Model 搜索方法 as_indexed_json 并且您正在定义 to_indexed_json。在 elasticesearch-model gem 中,您可以找到示例 https://github.com/elastic/elasticsearch-rails/blob/master/elasticsearch-model/examples/activerecord_associations.rb#L82

它应该看起来像这样:

def as_indexed_json(options = {})
as_json methods: [:book_title, :book_type, :author_name]
end

关于ruby-on-rails - rails Elasticsearch 关系属性未编入索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806473/

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