Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb ***NoMethodError异常:未定义的方法“%u id”_Mongodb_Ruby On Rails 4_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Tire - Fatal编程技术网 elasticsearch,tire,Mongodb,Ruby On Rails 4,elasticsearch,Tire" /> elasticsearch,tire,Mongodb,Ruby On Rails 4,elasticsearch,Tire" />

Mongodb ***NoMethodError异常:未定义的方法“%u id”

Mongodb ***NoMethodError异常:未定义的方法“%u id”,mongodb,ruby-on-rails-4,elasticsearch,tire,Mongodb,Ruby On Rails 4,elasticsearch,Tire,我正在开发RubyOnRails应用程序。我正在通过轮胎宝石使用Elasticsearch。Elasticsearch索引了3个模型。这些模型包括新闻、新闻标签和分类。新闻与新闻标签有多对多的关系,新闻标签与分类有多对多的关系。所有这些模型都是mongodb 映射索引:- mapping do indexes :cover, analyzer: 'snowball' indexes :title_en indexes :title_ar

我正在开发RubyOnRails应用程序。我正在通过轮胎宝石使用Elasticsearch。Elasticsearch索引了3个模型。这些模型包括新闻、新闻标签和分类。新闻与新闻标签有多对多的关系,新闻标签与分类有多对多的关系。所有这些模型都是mongodb

映射索引:-

    mapping do
        indexes :cover, analyzer: 'snowball'
        indexes :title_en
        indexes :title_ar
        indexes :content_brief_ar
        indexes :content_brief_en
        indexes :body_ar
        indexes :body_en
        indexes :likes, type: 'nested' do
        end
        indexes :comments, type: 'nested' do
        end

        indexes :news_tags, type: 'nested' do
          indexes :name_en, analyzer: 'snowball'
          indexes :name_ar, analyzer: 'snowball'
          indexes :categorizations, type: 'nested' do
            indexes :ar_name, analyzer: 'snowball'
            indexes :en_name, analyzer: 'snowball'
          end
        end
      end
      def to_indexed_json
    {
      cover: self.cover,
      title_ar: self.title_ar,
      title_en: self.title_en,
      content_brief_ar: self.content_brief_ar,
      content_brief_en: self.content_brief_en,
      body_ar: self.body_ar,
      body_en: self.body_en,
      news_tags: self.news_tags,
      likes: self.likes,
      comments: self.comments
    }.to_json
  end
我怎么找的

@news = News.tire.search do
        query do
          nested path: 'news_tags' do
            query do
              boolean do
                  should {terms 'news_tags.categorization_ids', categorization_ids }
              end
            end
          end
        end
        page = page_param.to_i
        search_size = per_page_param.to_i
        from (page - 1) * search_size
        size search_size
      end
在我意识到检索结果的类型不是News,而是Item,并且在模型News中定义了一些方法,这些方法经常被调用之前,这一切都很顺利。因此,我寻找包装的项目是新闻,我发现一些有趣的东西

将此行添加到config/initializers/tire.rb

Tire.configure do    
   wrapper ProxyObject
end
这个文件是app/models/proxy_object.rb

class ProxyObject < SimpleDelegator
  delegate :class, :is_a?, :to => :_proxied_object

  def initialize(attrs={})
    klass = attrs['_type'].camelize.classify.constantize
    @_proxied_object = klass.new
    _assign_attrs(attrs)
    super(_proxied_object)
  end

  private

  def _proxied_object
    @_proxied_object
  end

  def _assign_attrs(attrs={})
     attrs.each_pair do |key, value|
       unless _proxied_object.respond_to?("#{key}=".to_sym)
         _proxied_object.class.send(:attr_accessor, key.to_sym)
       end
       _proxied_object.send("#{key}=".to_sym, value)
     end
  end
end
为什么我会收到这个错误消息?
它甚至不是描述性的。我不知道问题出在哪里。那么,有人能帮我吗。

我找到了解决办法。在搜索查询中添加load:true将解决此问题

@news = News.tire.search load: true do
        query do
          nested path: 'news_tags' do
            query do
              boolean do
                  should {terms 'news_tags.categorization_ids', categorization_ids }
              end
            end
          end
        end
        page = page_param.to_i
        search_size = per_page_param.to_i
        from (page - 1) * search_size
        size search_size
      end
谢谢大家

(byebug) @news.count
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 1.9747ms
  MOPED: 127.0.0.1:27017 UPDATE       database=nabda-net_staging collection=news_tags selector={"$and"=>[{"_id"=>{"$in"=>[]}}]} update={"$pull"=>{"news_ids"=>BSON::ObjectId('5888aaba6f6d6154d0000000')}} flags=[:multi]
                         COMMAND      database=nabda-net_staging command={:getlasterror=>1, :w=>1} runtime: 0.6387ms
*** NoMethodError Exception: undefined method `_id' for #<Hash:0x00000004b78190>
@news = News.tire.search load: true do
        query do
          nested path: 'news_tags' do
            query do
              boolean do
                  should {terms 'news_tags.categorization_ids', categorization_ids }
              end
            end
          end
        end
        page = page_param.to_i
        search_size = per_page_param.to_i
        from (page - 1) * search_size
        size search_size
      end