1

親子ドキュメント マッピングがあり、親には contact_id フィールドが 1 つしかありません。そして、新しい子ドキュメントを挿入するときに、この親ドキュメントが存在することを確認する必要があります。既に存在する場合と存在しない場合があります。

そのため、Bulk API を使用して、存在しない場合は親を挿入し、1 つの要求で子を挿入します。

私の質問は、どちらの方法がより高速かということです: updatewithdoc_as_upsertおよびdetect_noopORindexおそらく既に存在する同じデータを持つ新しいレコード:

{ update: { _index: 'index_name', _type: 'contact', _id: 25, _routing: 14}}
{ doc: { contact_id: 25 }, doc_as_upsert: true, detect_noop: true }
{ index: { _index: 'index_name', _type: 'event', _routing: 14, _parent: 25}}
{ ... event document body ...}

また

{ index: { _index: 'index_name', _type: 'contact', _id: 25, _routing: 14}}
{ contact_id: 25 }
{ index: { _index: 'index_name', _type: 'event', _routing: 14, _parent: 25}}
{ ... event document body ...}
4

1 に答える 1

4

同じように動作するようです:

                   user     system      total        real
update_10k_x1  6.460000   1.720000   8.180000 ( 79.737009)
index_10k_x1   6.300000   1.680000   7.980000 ( 80.067855)
update_10k_x2  12.660000   3.350000  16.010000 (159.787347)
index_10k_x2   12.690000   3.380000  16.070000 (160.276717)
update_10k_x3  18.870000   5.000000  23.870000 (242.023184)
index_10k_x3   18.940000   5.030000  23.970000 (240.063431)

ベンチマークコードは次のとおりです。

require 'benchmark'
require 'elasticsearch-ruby'

$client = Elasticsearch::Client.new

def update_10k(n)
  index_name = "#{__method__}_x#{n}"
  n.times do
    (1..10000).each do |id|
      body = []
      body << { update: {_index: index_name, _type: :contact, _id: id }}
      body << { doc: { contact_id: id }, doc_as_upsert: true, detect_noop: true }
      $client.bulk body: body
    end
  end
end

def index_10k(n)
  index_name = "#{__method__}_x#{n}"
  n.times do
    (1..10000).each do |id|
      body = []
      body << { index: {_index: index_name, _type: :contact, _id: id }}
      body << { contact_id: id }
      $client.bulk body: body
    end
  end
end

Benchmark.bm do |x|
  (1..3).each do |n|
    x.report("update_10k_x#{n}") { update_10k(n) }
    x.report("index_10k_x#{n}") { index_10k(n) }
  end
end
于 2015-05-06T01:37:32.327 に答える