【问题标题】:Elasticsearch - What is faster? Index identical document or update with detect_noop: true?Elasticsearch - 什么更快?使用detect_noop索引相同的文档或更新:true?
【发布时间】:2015-05-04 23:30:57
【问题描述】:

我有一个父子文档映射,而父级只有一个contact_id 字段。 当我插入新的子文档时,我需要确保这个父文档存在。它可能已经存在,也可能不存在。

所以我使用 Bulk API 插入一个父级(如果它不存在)并在一个请求中插入一个子级。

我的问题是哪种方法更快:updatedoc_as_upsertdetect_noopindex 新记录与可能已经存在的相同数据:

{ 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 ...}

【问题讨论】:

  • 我相信您无法使用 Elasticsearch 实现这一目标。
  • @VineethMohan 实现什么?它已经起作用了。我在问这些请求中哪个执行得更好。
  • 为什么不对其进行基准测试?

标签: elasticsearch elasticsearch-dsl


【解决方案1】:

看起来它的表现是一样的:

                   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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多