[英]elasticsearch-rails with mogodb - undefind method as_indexed_json for article
Hi I am trying to use elasticsearch-rails in my Rails + mongo application. 嗨,我正在我的Rails + mongo应用程序中尝试使用elasticsearch-rails。 I am using mapping block for indexes but when I try to import my record to elasticsearch by
我正在为索引使用映射块,但是当我尝试通过以下方式将记录导入弹性搜索时
Article.import
Its giving me error undefined method as_indexed_json for Article 它为我提供了文章的错误未定义方法as_indexed_json
And If I create any article & comments for that it create index for all attributes of article model in elasticsearch server but it not creating index for nested comments attributes which I mentioned in mapping block 而且,如果我为此创建任何文章和注释,它会为elasticsearch服务器中的文章模型的所有属性创建索引,但不会为我在映射块中提到的嵌套注释属性创建索引
I tried same with sqlite3 database and everything is working fine but it not working with mongodb database 我在sqlite3数据库中尝试了同样的方法,但一切正常,但在mongodb数据库中却无法正常工作
Gemfile 的Gemfile
..................... .....
..................... .....
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.rb
Elasticsearch::Model.client = Elasticsearch::Client.new host: 'http://localhost:9200'
app/models/article.rb 应用程序/模型/ 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 应用程序/模型/ 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
Thanks in advance 提前致谢
You need to define as_indexed_json
method explicitly: 您需要显式定义
as_indexed_json
方法:
def as_indexed_json(options={})
as_json()
end
It helped in my case with exactly the same issue. 就我而言,这对完全相同的问题有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.