do_reindex

in lib/tasks/search.rake [46:105]


  def do_reindex (opts, in_place=false)
    start_time = Time.now

    
    new_index = TaskHelpers::ElasticsearchHelper.create_index

    
    
    
    
    
    
    
    
    old_index = TaskHelpers::ElasticsearchHelper.get_index
    alias_name = old_index.name
    alias_ = Tire::Alias.find alias_name
    if alias_.nil?
      
      
      
      raise RuntimeError, 'Cannot reindex in-place, no valid source index' if in_place
      LOG.warn 'deleting auto-created index to make room for the alias'
      old_index.delete
      
      
      
      move_alias_to(Content::ES_INDEX_NAME, new_index)
    end

    op = in_place ? 'reindex' : '(re)build index'
    LOG.info "preparing to #{op}"

    content_types = %w(Comment CommentThread)
    if in_place
      
      LOG.info 'copying documents from original index (this may take a while!)'
      old_index.reindex new_index.name
      LOG.info 'done copying!'
    else
      
      cursor = Content.where(:_type.in => content_types, :updated_at.lte => start_time)
      
      import_from_cursor(cursor, new_index, opts)
    end

    
    did_alias_move = move_alias_to(Content::ES_INDEX_NAME, new_index)

    if did_alias_move
      
      
      
      
      LOG.info "importing any documents that changed between #{start_time} and now"
      cursor = Content.where(:_type.in => content_types, :updated_at.gte => start_time)
      import_from_cursor(cursor, new_index, opts)
    end
  end