嗨,我正在我的Rails + mongo应用程序中尝试使用elasticsearch-rails。
我正在为索引使用映射块,但是当我尝试通过以下方式将记录导入到elasticsearch中时
Article.import
它为我提供了文章的错误未定义方法as_indexed_json
而且,如果我为此创建任何文章和注释,则它将为elasticsearch服务器中的文章模型的所有属性创建索引,但不会为我在映射块中提到的嵌套注释属性创建索引
我在sqlite3数据库中尝试了同样的方法,但一切正常,但在mongodb数据库中却无法正常工作
gem 文件
..................... .....
..................... .....
gem "elasticsearch", :git => "git://github.com/elasticsearch/elasticsearch-ruby.git"
gem "elasticsearch-model", :git => "git://github.com/elasticsearch/elasticsearch-rails.git"
gem "elasticsearch-rails", :git => "git://github.com/elasticsearch/elasticsearch-rails.git"
config / initializers / elasticsearch.rb
Elasticsearch::Model.client = Elasticsearch::Client.new host: 'http://localhost:9200'
app / models / article.rb
class Article
include Mongoid::Document
include Mongoid::Timestamps
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
field :title, :type => String
field :content, :type => String
field :published_on, :type => Date
field :abstract, :type => String
has_many :comments
index_name "name_of_elasticsearch"
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
mapping do
indexes :_id, :as => "_id.to_s", :index => "not_analyzed"
indexes :title, :type => "string", :index => "not_analyzed"
indexes :content, :type => "string", :index => "not_analyzed"
indexes :published_on, :type => "date"
indexes :comments, type: 'nested' do
indexes :body, analyzer: 'snowball'
indexes :stars
indexes :pick
indexes :user, analyzer: 'keyword'
indexes :user_location, type: 'multi_field' do
indexes :user_location
indexes :raw, analyzer: 'keyword'
end
end
end
end
end
app / models / comments.rb
class Comment
include Mongoid::Document
include Mongoid::Timestamps
field :body, :type => String
field :pick, :type => String
field :stars, :type => Date
field :user, :type => String
field :user_location, :type => String
belongs_to article
end
提前致谢
最佳答案
您需要显式定义as_indexed_json
方法:
def as_indexed_json(options={})
as_json()
end
就我而言,这对完全相同的问题有所帮助。
关于ruby-on-rails - 带mongoid的elasticsearch-rails-本文的未定义方法as_indexed_json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28484788/