我使用 MongoDB 來存盤有關產品的資訊。每個產品都有一個標題(字串)、描述(字串)等
我的寶石檔案
ruby '3.1.0'
gem 'mongoid', '~> 7.3.0'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
gem 'mini_magick'
gem 'kaminari-mongoid'
gem 'shopify_api'
gem 'bson_ext', '~> 1.5.1'
gem "graphql"
gem 'graphiql-rails'
gem 'simple_enum', '~> 2.3.0' , require: 'simple_enum/mongoid'
gem 'fog-aws'
gem 'carrierwave-aws'
gem 'elasticsearch', '~> 7.0'
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'rails', '~> 6.1.4', '>= 6.1.4.4'
在我的 project/app/models/product.rb 檔案中,我有:
class Product
include Elasticsearch::Model
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Timestamps::Created
include Mongoid::Timestamps::Updated
index_name "products-#{Rails.env}"
settings index: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :title, analyzer: 'english'
indexes :description_html, analyzer: 'english'
indexes :vendor, analyzer: 'english'
indexes :product_type, analyzer: 'english'
end
end
field :title, type: String, default: ""
field :description_html, type: String, default: ""
field :vendor, type: String, default: ""
field :product_type, type: String, default: ""
def as_indexed_json
as_json({only: [:title, :description_html, :product_type, :vendor]})
end
end
我的初始化檔案 elastic.rb
require 'elasticsearch'
ENV['RAILS_ENV'] = "development" # Set to your desired Rails environment name
require '/var/www/supfoxyapi.com/api/config/environment.rb'
class SyncData
def initialize
config = {
host: "http://localhost:9200/",
transport_options: {
request: { timeout: 5 }
},
}
Elasticsearch::Model.client = Elasticsearch::Client.new(config)
unless Product.__elasticsearch__.index_exists?
Product.__elasticsearch__.create_index! force: true
end
Product.import
puts Product.search('Droplet').count
end
end
SyncData.new
我在這一行的問題 Product.import
當我嘗試匯入產品時出現錯誤
raise NotImplemented, "Method not implemented for default adapter"
^^^^^
from /home/vertalm/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/elasticsearch-model-7.2.0/lib/elasticsearch/model/importing.rb:145:in `import'
from /home/vertalm/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/elasticsearch-model-7.2.0/lib/elasticsearch/model.rb:115:in `import'
from elastic.rb:17:in `initialize'
from elastic.rb:23:in `new'
from elastic.rb:23:in `<main>'
我花了半天時間尋找解決方案,但沒有運氣。
順便說一句,我根本不使用活動記錄
有什么幫助嗎?
uj5u.com熱心網友回復:
您需要添加一個transform方法來告訴 elasticsearch 如何存盤資訊。
來自代碼注釋的示例
# transform = lambda do |a|
# {index: {_id: a.id, _parent: a.author_id, data: a.__elasticsearch__.as_indexed_json}}
# end
#
# Article.import transform: transform
這就是 Active Record 使用的
lambda { |model| { index: { _id: model.id, data: model.__elasticsearch__.as_indexed_json } } }
elastic-rails 已經為Active Record實作了配接器,但默認配接器基本上是空的,并引發了您發布的例外。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/421027.html
標籤:
