Module: Eson::More::Transplant

Defined in:
lib/eson/more/transplant.rb

Defined Under Namespace

Modules: Functions

Instance Method Summary collapse

Instance Method Details

#merge_parents(old_parent, new_parent) ⇒ Object

Like #transplant, but removes the old parent.

Parameters:

  • old_parent (String, Hash, Object#id)

    the old parent document, either as an Object from which the ID can be retrieved.

  • new_parent (String, Hash, Object#id)

    the new parent document, either as an Object from which the ID can be retrieved.



91
92
93
94
# File 'lib/eson/more/transplant.rb', line 91

def merge_parents(old_parent, new_parent)
  transplant(old_parent, new_parent)
  delete(:id => old_parent)
end

#transplant(old_parent, new_parent, doc = nil) ⇒ Object

Transplants a document from one parent to another by deleting the old one and reindexing it with a new parent. If no doc is given, all documents of the old parent will be transplanted.

To change the index and parent type this command operates on, use Client#with:

client.with :index => "foo", :type => "blog" do |c|
  c.transplant(1, 2, 3)
end

Transplant does not refresh automatically.

Parameters:

  • old_parent (String, Hash, Object#id)

    the old parent document, either as an Object from which the ID can be retrieved.

  • new_parent (String, Hash, Object#id)

    the new parent document, either as an Object from which the ID can be retrieved.

  • doc (String, Hash, #id, #to_h) (defaults to: nil)

    the document to transplant, either as an Object from which the ID can be retrieved.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/eson/more/transplant.rb', line 61

def transplant(old_parent, new_parent, doc = nil)
  old_pid, new_pid, doc_id = Functions.extract_ids(old_parent, new_parent, doc)

  if doc.nil?
    hits = Functions.get_children(self, old_pid)
  else
    hits = Functions.get_doc(self, old_pid, doc)
  end

  unless hits.empty?
    bulk do |b|
      hits.each do |hit|
        b.delete :type => hit["_type"],
                 :id => hit["_id"],
                 :routing => old_pid
        b.index  :type => hit["_type"],
                 :id => hit["_id"],
                 :parent => new_pid,
                 :doc => hit["_source"]
      end
    end
  end
end