ruby-on-rails - 带mongoid的elasticsearch-rails-本文的未定义方法as_indexed_json

标签 ruby-on-rails mongodb elasticsearch full-text-search

嗨,我正在我的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/

相关文章:

ruby-on-rails - WickedPDF 本地渲染与生产渲染不同

ruby-on-rails - 将数据库约束转换为 Rails 验证

mongodb - 复制集合并将其粘贴到 MongoDB 上具有不同名称的同一数据库

mysql - 在 Snow Leopard 上编译 mysql gem 时遇到问题

ruby-on-rails - RoR 响应输出类型

node.js - mongodb 中按日期分组?

node.js - 为什么 connect-mongo 为每个请求创建新 session ?

elasticsearch - 在子查询中使用父文档中的字段值

elasticsearch - 如何在elasticsearch中用冒号搜索模式?

python - 更新到 elasticsearch 2.3 后无法定位嵌套的地理点