尽管模型中包含了问题模块,但该模块不可用(NoMethodError)



我正在将我的应用程序从solr迁移到Elasticsearch以进行文档搜索。部署Elasticsearch的推荐模式是在Rails问题中包含Elasticsearch功能(即Elasticsearch::ModelElasticsearch::Model::Indexing),然后在模型中包含此模块。我已经实现了这一点。(当我正在处理这个问题时,一些方法暂时被评论掉了。)

#app/models/concerns/elasticsearch_user.rb
require 'active_support/concern'
module ElasticsearchUser
extend ActiveSupport::Concern
included do
  require 'elasticsearch/model'
  include Elasticsearch::Model
  include Elasticsearch::Model::Indexing
  #mapping do
  #  #to-do
  #end
  #def self.search(query)
  #  _elasticsearch_.search(
  #    {
  #        query: {
  #         multi_match: {
  #             query: query,
  #             fields: ['username^2', 'email', 'full_name']
  #         }
  #        }
  #    }
  #  )
  #end
  end
end

#User.rb
...
# Elasticsearch configuration
include ElasticsearchUser
# index_name "users" <-- This value is inferred automatically by the model name
settings index: { number_of_shards: 1 } do
  mappings dynamic: 'false' do
    indexes :username
    indexes :email
    indexes :full_name
  end
end
...
def as_indexed_json(options={})
  self.as_json(only: [:username, :email, :full_name],
               methods: [:full_name]
  )
end

我可以毫无问题地呼叫Model.importModel.search。调用类似Model._elasticsearch_.create_index!(带有和不带有_elasticsearch_命名空间)的东西会抛出NoMethodError。我曾尝试将Elasticsearch::Model直接包含在我的模型定义中,但没有成功。我还尝试过在我的模型顶部要求"active_support/correct",但也没有成功。很明显,模块/问题没有包含在我的模型中,但我不明白为什么。

这实际上与包含问题无关。我在搜索方法中添加了一些日志记录(we made it to the search),并在控制台输出中找到了它。在Elasticsearch人员的帮助下,我们意识到我使用的是_elasticsearch_,但它是带两个下划线的__elasticsearch__。在所有的事情中。。。

您使用的rails版本是否正确尝试控制台内

require 'active_support/concern'

如果返回nil模块未导入

最新更新