<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title><![CDATA[Mohammad Sarwar - Elasticsearch Labs]]></title>
    <description><![CDATA[Articles and tutorials from the Search team at Elastic]]></description>
    <copyright><![CDATA[© 2026. Elasticsearch B.V. All Rights Reserved]]></copyright>
    <image>
      <title><![CDATA[Mohammad Sarwar - Elasticsearch Labs]]></title>
      <url>https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/blt1121c0bf0e8a6e65/6a88da6340a1841030ef456f/search-labs-thumbnail.png</url>
      <link>https://www.elastic.co/search-labs/author/mohammad-sarwar</link>
    </image>
    <link>https://www.elastic.co/search-labs/author/mohammad-sarwar</link>
    <atom:link href="https://www.elastic.co/search-labs/rss/author/mohammad-sarwar.xml" rel="self" type="application/rss+xml"/>
    <language><![CDATA[en]]></language>
    <lastBuildDate>Wed, 25 Nov 2026 18:13:44 GMT</lastBuildDate>
  <item>
    <title><![CDATA[2 lines, 1 document: Pairing Elasticsearch bulk actions in Logstash]]></title>
    <description><![CDATA[Logstash holds the Elasticsearch credentials, masks the emails and gives each tenant its own index. The client never needs to know your cluster exists, and the repo runs on Docker Compose.]]></description>
    <content:encoded><![CDATA[<ul><li>An Elasticsearch bulk API request is two lines per document, and Logstash reads newline-delimited JSON (NDJSON) one line at a time, so an action line and its document arrive as separate events. The gateway in this blog post pairs them back up before anything gets indexed. On the way through, it masks emails and drops debug events, and it routes each tenant to its own index. This means a client that can only POST a bulk body to a URL never has to hold an Elasticsearch credential. It’s one config file and one ordered worker, and the repo runs on Docker Compose. It works for clients that speak raw <code>_bulk</code>, but apps built on an official Elasticsearch client will not.</li></ul><h2><strong>Prerequisites</strong></h2><ul><li><p><a href="https://docs.docker.com/compose/install/">Docker Compose</a></p></li><li><p><a href="https://curl.se/download.html">curl</a> and <a href="https://jqlang.org/download/">jq</a></p></li><li><p>About 4 GB of free memory for the three containers (Elasticsearch, Logstash, and Kibana)</p></li></ul><p>The <a href="https://github.com/someshwaranM/logstash-bulk-gateway">repo</a> was tested with Elasticsearch, Logstash, and Kibana 9.5.6.</p><h2><strong>What the Logstash bulk gateway needs to handle</strong></h2><p>We’ll build for the following situation:</p><ul><li><p>A client sends <code>_bulk</code> requests to one URL and can't be modified.</p></li><li><p>A proxy or load balancer in front sets two headers on every request: <code>X-Request-Id</code>, which is unique per request, and <code>X-Tenant-Id</code>. It overwrites whatever the client sent, and clients can't reach Logstash directly.</p></li><li><p>Each tenant's data must go to its own index.</p></li><li><p>Emails must be masked, and debug events must be dropped.</p></li><li><p>Only Logstash holds the Elasticsearch credentials.</p></li><li><p>Kibana is there so you can see what was indexed.</p></li></ul><img src="https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/bltc72aaa2b2865f053/6b0534f04499b4975c512c23/image3.png" alt="Logstash bulk API gateway architecture: legacy client POSTs _bulk, Logstash pairs lines, writes to Elasticsearch" /><h2><strong>Why not send bulk requests straight to Elasticsearch?</strong></h2><p>Often, you should. Elasticsearch <a href="https://www.elastic.co/docs/manage-data/ingest/transform-enrich/ingest-pipelines">ingest pipelines</a> can mask fields and drop documents,  and they can set the index. If that's all you need, use one and skip Logstash.</p><p>Logstash earns its place when:</p><ul><li><p>The client must not know that Elasticsearch exists, or it can't hold a credential.</p></li><li><p>You need to enrich events from outside Elasticsearch, like a database or an HTTP service.</p></li><li><p>You want to write the same data to more than one place, like Elasticsearch and Kafka.</p></li><li><p>You want one set of rules in front of several destinations.</p></li></ul><h2><strong>How the Elasticsearch bulk API format works (NDJSON)</strong></h2><p>A bulk request is <a href="https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-bulk">NDJSON</a>, rather than a JSON array. Most operations take two lines: an action line that says what to do; and a source line with the document. A delete takes only the action line:</p>{ "index": { "_id": "1" } }
{ "order_id": "A-1001", "email": "jane.doe@example.com", "total": 42.5, "level": "info" }
{ "create": { "_id": "2" } }
{ "order_id": "A-1002", "email": "bob.smith@example.com", "total": 10, "level": "info" }
{ "index": { "_id": "3" } }
{ "order_id": "A-1003", "email": "debug@example.com", "total": 0, "level": "debug" }
{ "update": { "_id": "1" } }
{ "doc": { "total": 45.0, "email": "jane.doe@example.com" } }
{ "delete": { "_id": "2" } }<p>Each line has to be on its own line, not pretty-printed, and the body must end with a newline. When you send it, read the file as raw bytes. If a JSON library reserializes the body, you lose the newlines.</p><h2><strong>Why Logstash splits a bulk request into separate events</strong></h2><p>Point the Logstash <a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-inputs-http">http input</a> at this body with the <code>json_lines</code> codec, and Logstash makes one event per line.</p><p>That split is the problem. After the action and source are separate events, nothing connects <code>{ "index": { "_id": "1" } }</code> to the document that follows it. And if more than one worker handles events from the same request, they can be processed out of order, so a source line can end up with the wrong action.</p><p>So the pipeline has two jobs: </p><ul><li><p>Keep the events of one request together.</p></li><li><p>Handle them in order.</p></li></ul><h2><strong>Building the Logstash bulk API gateway pipeline</strong></h2><p>The whole pipeline is one file, <code>bulk-gateway.conf</code>, in the <a href="https://github.com/someshwaranM/logstash-bulk-gateway">repo</a>.. In this post, we discuss each step in turn.</p><h3><strong>Configuring the Logstash http input for NDJSON</strong></h3><p>The <a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-inputs-http">http input</a> picks its codec from the request's <code>Content-Type</code>, and the default mapping doesn’t split a bulk body into lines. This input block overrides that and normalizes what arrives:</p>input {
  http {
    port =&gt; 8080
    ecs_compatibility =&gt; disabled
    request_headers_target_field =&gt; "[@metadata][headers]"
    remote_host_target_field     =&gt; "[@metadata][remote_host]"
    codec =&gt; json_lines { decode_size_limit_bytes =&gt; 20971520 }
    additional_codecs =&gt; {}
  }
}<ul><li><p><code>codec =&gt; json_lines</code> makes line splitting the behavior for every request. Bulk clients send <code>application/x-ndjson</code> or <code>application/json</code>(or no content type at all), so the codec cannot depend on what the client declares.</p></li><li><p><code>additional_codecs =&gt; {}</code> empties the default map. Without this, <code>application/json</code> would still route to the <code>json</code> codec, which treats the whole body as one document.</p></li><li><p><code>decode_size_limit_bytes =&gt; 20971520</code> caps a single line (meaning, one action or one document) at 20 MB.</p></li><li><p><code>ecs_compatibility =&gt; disabled</code> stops the input from adding its own <code>http</code>, <code>url</code>, <code>host</code>, and <code>user_agent</code> fields to every event, where they could collide with the client’s own fields.</p></li><li><p><code>request_headers_target_field</code> and <code>remote_host_target_field</code> move the request headers and the client address into <code>[@metadata]</code>, which is where the next filter reads the tenant and request ID from.</p></li></ul><h3><strong>Reading tenant and request ID from the headers</strong></h3><p>In Logstash, the above http input puts the request headers in metadata, under <code>[@metadata][headers]</code>, because of the <code>request_headers_target_field</code> setting in the input block. It also lowercases the header names and turns dashes into underscores, so <code>X-Request-Id</code> arrives as <code>x_request_id</code>. </p><p>Two Ruby filters handle the header metadata. The first processor removes everything in metadata except what the input puts there, because a client can put its own <code>@metadata</code> key in a document and Logstash merges it into the event. The second reads <code>x_request_id</code> and <code>x_tenant_id</code> from <code>[@metadata][headers]</code>:</p># keep only what the input put in metadata
ruby {
  code =&gt; '
    md = event.get("[@metadata]") || {}
    md.keys.each { |k| event.remove("[@metadata][#{k}]") unless %w[headers remote_host].include?(k) }
  '
}
ruby {
  code =&gt; '
    h = event.get("[@metadata][headers]") || {}
    req = h["x_request_id"]
    tenant = h["x_tenant_id"]
    if req.nil? || req.empty? || tenant.nil? || tenant.empty?
      event.tag("missing_headers")
      event.set("[@metadata][reject]", true)
    elsif tenant !~ /\A[a-z0-9_-]{1,64}\z/
      event.tag("invalid_tenant")
      event.set("[@metadata][reject]", true)
    else
      event.set("[@metadata][req_id]", req)
      event.set("[@metadata][tenant]", tenant)
    end
  '
}
# a client @timestamp that isn't ISO 8601: convert epoch milliseconds
if "_timestampparsefailure" in [tags] {
  ruby {
    code =&gt; '
      v = event.get("_@timestamp")
      v = Integer(v, 10) if v.is_a?(String) &amp;&amp; v.match?(/\A\d+\z/)
      if v.is_a?(Integer)
        event.timestamp = LogStash::Timestamp.at(v / 1000.0)
        event.remove("_@timestamp")
        t = (event.get("tags") || []) - ["_timestampparsefailure"]
        t.empty? ? event.remove("tags") : event.set("tags", t)
      end
    '
  }
}<p>The filter tags the event <code>missing_headers</code>, if either header is missing, and <code>invalid_tenant</code>, if the tenant name isn't 1–64 lowercase letters, digits, dashes, or underscores, so a tenant like Acme/X is rejected. Both also set a reject flag in metadata, and that flag is what sends the event to <code>stdout</code>. Nothing flagged is written to the tenant's index, so bad input is visible and not quietly indexed. The tenant check matters because the tenant becomes part of an index name, and Elasticsearch answers an invalid name with a 400.</p><p><code>stdout</code> is added there so rejects are easy to see. In a production setup, send them wherever suits you; for example, an Elasticsearch output to a separate rejected-documents index, a file, or a queue like Kafka, so you can fix them and resend. </p><p>You could also use the Logstash <a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-inputs-dead_letter_queue">dead letter queue input plugin</a> to retry failed documents. Only the Elasticsearch output writes to it, for documents that Elasticsearch rejects with a 400 or 404, so it can't hold these pipeline rejects.</p><p>The last filter in the block deals with the client's <code>@timestamp</code>. Logstash keeps a valid International Organization for Standardization (ISO) 8601 value and tags anything else with <code>_timestampparsefailure</code>. Elasticsearch also accepts epoch milliseconds, so the filter converts those back. Any other value stays tagged and goes to <code>stdout</code>.</p><h3><strong>Pairing action and source lines with the aggregate filter</strong></h3><p>An <em>action line</em> is any event with a top-level <code>index</code>, <code>create</code>, <code>update</code>, or <code>delete</code> key that holds an object. The first Ruby filter below finds it and saves its operation and ID. The pipeline puts <code>index</code>, <code>create</code>, and <code>update</code> in a small queue for that tenant and request, using the <a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-filters-aggregate">aggregate filter</a>, and drops the event. The next source line takes the oldest waiting action and picks up its operation and ID. A <code>delete</code> has no source line, so it skips the queue:</p><img src="https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/blt466eb76a85105ae8/6b0535864474747bc9001a7a/image2.png" alt="How Logstash pairs Elasticsearch bulk action and source lines into index, update and delete operations" /># find the action line, and remember its operation and ID
ruby {
  code =&gt; '
    %w[index create update delete].each do |k|
      if event.get(k).is_a?(Hash)
        event.set("[@metadata][is_action]", true)
        event.set("[@metadata][op]", k)
        event.set("[@metadata][op_id]", event.get("[#{k}][_id]"))
        break
      end
    end
  '
}
# delete has no source line: send it on if it has an _id
if [@metadata][op] == "delete" {
  if [@metadata][op_id] {
    mutate {
      add_field =&gt; {
        "[@metadata][action]" =&gt; "delete"
        "[@metadata][doc_id]" =&gt; "%{[@metadata][op_id]}"
      }
    }
  } else {
    mutate {
      add_tag   =&gt; ["missing_id"]
      add_field =&gt; { "[@metadata][reject]" =&gt; "true" }
    }
  }
}
# index, create, update: remember the action line, then drop it
aggregate {
  id =&gt; "queue_push"
  task_id =&gt; "%{[@metadata][tenant]}:%{[@metadata][req_id]}"
  code =&gt; '
    map["pending"] ||= []
    map["pending"] &lt;&lt; {
      "op" =&gt; event.get("[@metadata][op]"),
      "id" =&gt; event.get("[@metadata][op_id]")
    }
  '
  timeout =&gt; 3600
  inactivity_timeout =&gt; 300
}
drop { }
# any other line is a source line: take the oldest waiting action
aggregate {
  id =&gt; "queue_pop"
  task_id =&gt; "%{[@metadata][tenant]}:%{[@metadata][req_id]}"
  code =&gt; '
    op = (map["pending"] || []).shift
    if op.nil?
      event.tag("orphan_source_line")
      event.set("[@metadata][reject]", true)
    elsif op["op"] == "update" &amp;&amp; op["id"].nil?
      event.tag("missing_id")
      event.set("[@metadata][reject]", true)
    else
      event.set("[@metadata][action]", op["op"])
      event.set("[@metadata][doc_id]", op["id"]) if op["id"]
    end
  '
}<p>With this code, we should be aware of the following considerations:</p><ul><li><p><strong>Rejected lines.</strong> A source line with nothing waiting gets <code>orphan_source_line</code>, and an <code>update</code> or <code>delete</code> with no <code>_id</code> gets <code>missing_id</code>. Both are rejected; they go to <code>stdout</code> and aren’t indexed.</p></li><li><p><strong>Lines without an </strong><strong><code>_id</code></strong><strong>.</strong> An <code>index</code> or <code>create</code> without an <code>_id</code> is accepted. Elasticsearch generates one.</p></li><li><p><strong>Found by shape.</strong> A document with its own top-level field named <code>index</code>, <code>create</code>, <code>update</code>, or <code>delete</code> and holding an object would be mistaken for an action line.</p></li><li><p><strong>The shift operation </strong>removes and returns the first item in the list, because events arrive in order.</p></li><li><p><strong>Timeouts.</strong> They clean up queues that never finish, like a truncated request. <code>inactivity_timeout =&gt; 300</code> clears a queue after 300 seconds without an event, and <code>timeout =&gt; 3600</code> is a hard cap from its first event.</p></li><li><p><strong>Why so long.</strong> If the output is waiting on Elasticsearch (for example, retrying after a 429 or a connection error), the worker can sit in the output between an action line and its source line, and a queue that expires in between turns the source line into an orphan. Within 30 seconds, a 50-second stall lost a document in a test. An open queue only costs memory.</p></li><li><p><strong>Open queues.</strong> Finished requests keep their queue until the inactivity timeout, so <code>aggregate_maps</code> takes five to seven minutes to fall back to 0.</p></li><li><p><strong>Queue key.</strong> It's the tenant plus the request ID, so two tenants that reuse a request ID can't mix queues. All timeout options live in the first aggregate filter, as the <a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-filters-aggregate">aggregate filter docs</a> require.</p></li></ul><p>Pairing only works if events are handled in order, and Logstash doesn't guarantee to preserve message ordering by default. Events in a batch (and whole batches) can be reordered. So the pipeline runs one worker with ordered processing, which the <a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-filters-aggregate">aggregate filter docs</a> and the <a href="https://www.elastic.co/docs/reference/logstash/processing#event-ordering">event ordering docs</a> both specify. For example, processing financial transactions, where a debit has to be applied before the credit that follows it, is the classic case. The <code>pipeline.ordered</code> defaults to <code>auto</code>, which already turns ordering on with one worker. Setting it to <code>true</code> prevents Logstash from starting if someone raises the worker count later. Both settings are in <code>logstash.yml</code> in the repo and are described by the <a href="https://www.elastic.co/docs/reference/logstash/logstash-settings-file"><code>logstash.yml</code> reference</a>:</p>pipeline.workers: 1
pipeline.ordered: true<h3><strong>Masking emails, dropping events, and routing per tenant</strong></h3><p>Now that every event has its operation and ID, the rules are simple. An update source has to be exactly <code>{ "doc": { ... } }</code>. The Elasticsearch output wraps updates itself, so the pipeline unwraps doc first. Without that, you'd store a nested doc field instead of updating the document. </p><p><code>The script</code><code>, </code><code>upsert</code>, <code>doc_as_upsert</code>, and the other update options aren't supported. The Elasticsearch output takes them as settings on the output itself, which apply to every event it sends, so it can't use the option that a client chose for one update. Passed through as plain fields, they'd end up in the document and do nothing. So the following code tags these updates <code>unsupported_update</code> and sends them to <code>stdout</code> instead. If you need them, send the updates straight to Elasticsearch.</p>if [@metadata][action] == "update" {
  ruby {
    code =&gt; '
      d = event.get("doc")
      extra = event.to_hash.keys - ["doc", "@timestamp", "@version"]
      if !d.is_a?(Hash) || !extra.empty?
        event.tag("unsupported_update")
        event.set("[@metadata][reject]", true)
      else
        event.remove("doc")
        ts = d.delete("@timestamp")
        d.each { |k, v| event.set(k, v) }
        if ts
          event.timestamp = ts.is_a?(Integer) ? LogStash::Timestamp.at(ts / 1000.0) : LogStash::Timestamp.parse_iso8601(ts.to_s)
          event.set("[@metadata][keep_timestamp]", true)
        end
      end
    '
  }
}
if [level] == "debug" { drop { } }
if [email] {
  mutate { gsub =&gt; ["email", "^(.)[^@]*@", "\1***@"] }
}
mutate { add_field =&gt; { "[@metadata][index]" =&gt; "orders-%{[@metadata][tenant]}" } }
mutate { remove_field =&gt; ["@version"] }
if [@metadata][action] == "update" and ![@metadata][keep_timestamp] {
  mutate { remove_field =&gt; ["@timestamp"] }
}<p>The index comes from the tenant header, not from the client. That way a client can't write into another tenant's index, even by accident, as long as your proxy sets <code>X-Tenant-Id</code> from an authenticated identity and clients can't reach Logstash directly. The http input processor has no authentication unless you set user and password, or Transport Layer Security (TLS) client certificates.</p><p>The email rule only masks a top-level email field (a string or list of strings) and keeps the domain, so it isn't full personally identifiable information (PII) redaction. Logstash adds <code>@version</code> and <code>@timestamp</code> to every event. The pipeline drops <code>@version</code> and keeps <code>@timestamp</code> on <code>index</code> and <code>create</code>, which the Kibana step uses. A valid <code>@timestamp</code> from the client wins. On an <code>update</code>, <code>@timestamp</code> is removed so it doesn't overwrite the original, unless the client sent a new one.</p><h3><strong>Configuring the Elasticsearch output with dynamic action and index</strong></h3><p>The last step is the <a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-outputs-elasticsearch">Elasticsearch output</a>. What's specific to this pipeline is that <code>action</code> and <code>index</code> aren’t fixed values, and neither is <code>document_id</code>. They're populated from the metadata collected from the earlier steps set for each event. An <code>index</code> or <code>create</code> without an <code>_id</code> goes to a second output with no <code>document_id</code>, so Elasticsearch generates one. Without that split, every ID-less document would be written with the literal text <code>%{[@metadata][doc_id]}</code> as its ID, and each one would overwrite the last. Anything that the pipeline flagged, like <code>missing_headers</code> or <code>invalid_tenant</code>, goes to <code>stdout</code> instead. The credentials live in Logstash, too, and the <code>${ENV_VALUES}</code> values are <a href="https://www.elastic.co/docs/reference/logstash/environment-variables">environment variables</a> that Logstash replaces at startup:</p>if [@metadata][reject] or "_jsonparsefailure" in [tags] or "_timestampparsefailure" in [tags] or "_rubyexception" in [tags] or "_tagsparsefailure" in [tags] {
  stdout { codec =&gt; rubydebug { metadata =&gt; true } }
} else if [@metadata][doc_id] {
  elasticsearch {
    hosts       =&gt; ["${ES_HOST}"]
    user        =&gt; "${ES_USER}"
    password    =&gt; "${ES_PASSWORD}"
    action      =&gt; "%{[@metadata][action]}"
    index       =&gt; "%{[@metadata][index]}"
    document_id =&gt; "%{[@metadata][doc_id]}"
  }
} else {
  elasticsearch {
    hosts    =&gt; ["${ES_HOST}"]
    user     =&gt; "${ES_USER}"
    password =&gt; "${ES_PASSWORD}"
    action   =&gt; "%{[@metadata][action]}"
    index    =&gt; "%{[@metadata][index]}"
  }
}<p>The output uses the <a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-outputs-elasticsearch#_retry_policy">Elasticsearch bulk API</a> to send events in batches. So you take a bulk request apart, and the output builds a new one on the other side. In the next section, we cover what happens to a document that Elasticsearch rejects. </p><h2><strong>Limitations of a Logstash bulk API gateway</strong></h2><p>Logstash acknowledges the request as soon as it accepts the body, so the client gets a 200 OK and never learns what Elasticsearch did with its documents. Here’s what happens to each outcome:</p><p><strong>Elasticsearch response</strong></p><p><strong>What happens</strong></p><p>400 or 404; for example, a mapping error</p><p>Goes to the <a href="https://www.elastic.co/docs/reference/logstash/dead-letter-queues">dead letter queue</a>, which is off by default in Logstash and enabled in the repo with <code>dead_letter_queue.enable: true</code> in <code>logstash.yml</code></p><p>409, a version conflict</p><p>Logged and dropped</p><p>Any other error</p><p>Retried</p><p>Any outcome at all</p><p>The client already received a 200 OK and never learns the result</p><ul><li>The <a href="https://www.elastic.co/docs/manage-data/data-store/data-streams/failure-store">failure store</a> is a similar feature, but it only fits data streams, which take new documents through creation, so it doesn’t fit here.</li></ul><ul><li><p><strong>Rejected input.</strong> A request without the headers, a bad tenant name, a timestamp that isn't ISO 8601 or epoch milliseconds, a <code>tags</code> field that's an object, and an update with a script or upsert all go to <code>stdout</code> and never to Elasticsearch. The client still receives a 200 OK response.</p></li><li><p><strong>Crashes. </strong>Enable <code>queue.type: persisted</code> explicitly from the <code>logstash.yml</code> to use <a href="https://www.elastic.co/docs/reference/logstash/persistent-queues">persistent queues</a> (not enabled by default). The persistent queues keep events across a restart, but the pairing queues are in memory. After a crash, a replayed source line can arrive without its action line and be rejected as an orphan. In a kill -9 test (a simulated crash test), by setting <code>queue.checkpoint.writes: 1</code>, we lose every document of a request split across two batches. The default settings recover them, but nothing guarantees that, so treat the persistent queue as a help, not a guarantee.</p></li><li><p><strong>One worker.</strong> Ordered processing means one CPU thread for the filters. If you need ordered processing across more than one CPU thread, run several independent pipelines and split the traffic by tenant at the proxy.</p></li><li><p><strong>Ignored bulk features.</strong> Only the <code>operation</code> and <code>_id values</code> are used. The <code>_index</code>, <code>routing</code>, <code>if_seq_no</code>, <code>if_primary_term</code>, per-action pipeline, URL parameters, and index in the path are dropped.</p></li><li><p><strong>Watch the queues.</strong> Logstash's <a href="https://www.elastic.co/docs/reference/logstash/monitoring-logstash">monitoring API</a> shows how many queues are open. Run <code>curl localhost:9600/_node/stats/pipelines/main</code>, and look at <code>aggregate_maps</code>, which falls back to 0 five to seven minutes after the last request. To see it in Kibana, connect Logstash with <a href="https://www.elastic.co/docs/reference/logstash/monitoring-logstash-with-elastic-agent">Elastic Agent or Metricbeat</a>.</p></li></ul><p><code>aggregate_maps</code> counts the queue(s), one for each request from the last few minutes, so you can check the queues using the <code>jq</code> command:</p># Request
curl -s localhost:9600/_node/stats/pipelines/main | jq '.pipelines.main.plugins.filters[] | select(.id=="queue_push") | {id, aggregate_maps}'
# Response
{
  "id": "queue_push",
  "aggregate_maps": 2 
}<ul><li><p><strong>Demo settings.</strong> The repo uses a simple password and no TLS, so it runs anywhere. For production, authenticate using an API key and TLS.</p></li></ul><h2><strong>Running the Logstash bulk gateway with Docker Compose</strong></h2><p>Everything is in a repo. Along with Docker, you need curl and jq, as listed in the prerequisites.</p>git clone https://github.com/someshwaranM/logstash-bulk-gateway.git
cd logstash-bulk-gateway
docker compose up -d
./scripts/send.sh
./scripts/verify.sh<p><code>send.sh</code> posts the sample body with the two headers. <code>verify.sh</code> checks the end state and should print six <code>PASS</code> lines:</p><ul><li><p>Document 1 exists.</p></li><li><p>Document 1 has a total 45, so the update was applied.</p></li><li><p>Document 1 has the email masked.</p></li><li><p>Document 1 has no nested doc field.</p></li><li><p>Document 2 is gone, because it was created and then deleted.</p></li><li><p>Document 3 was never stored, because it was a debug event.</p></li></ul><p>Then run <code>./scripts/edge-cases.sh</code>. It sends problem requests that a real client can produce, and it prints 18 <code>PASS</code> lines. It covers the following cases:</p><ul><li><p>A body sent as application/json</p></li><li><p>Documents without an <code>_id</code></p></li><li><p>Documents with their own <code>url</code>, <code>host</code>, and <code>tags</code> fields</p></li><li><p>Client timestamps in different formats</p></li><li><p>An invalid tenant name</p></li><li><p>An update with a script</p></li><li><p>An update that keeps the original <code>@timestamp</code></p></li><li><p>A mapping error that ends up in the dead letter queue </p></li></ul><p>After that, try a few things:</p><ul><li><p>Send the same file as another tenant, with <code>./scripts/send.sh sample/bulk-acme.ndjson globex</code>. You’ll get a second index, <code>orders-globex</code>.</p></li><li><p>Send a request without the headers. Look for <code>missing_headers</code> in the Logstash logs.</p></li><li><p>Set <code>pipeline.workers: 2</code>, and recreate the Logstash container. Compose says it started, but Logstash exits a few seconds later with an error, and the log explains why.</p></li><li><p>Send a request with the tenant <code>Not/Valid</code>. The client still gets <code>OK</code> back, and the events show up on <code>stdout</code> with an <code>invalid_tenant</code> tag.</p></li></ul><p>To look at the documents, open Kibana at <code>http://localhost:5601</code> and log in with the user <em>elastic</em> and the password <em>changeme</em>. In Discover, create a data view for <code>orders-*</code> with <code>@timestamp</code> as the <code>time</code> field. You'll see the documents with the emails masked, and no document for the debug event.</p><p>If port 8080 is busy on your machine, set the <code>LOGSTASH_PORT</code> environment variable first as outlined in the README.</p><h2><strong>Logstash as a bulk gateway</strong></h2><p>Bulk requests don't survive a trip through Logstash on their own. The <code>json_lines</code> codec splits them into single lines, and the link between an action and its source is gone. One small queue per request (plus an ordered worker) puts it back.</p><p>After that works, Logstash is a helpful gateway for a client that you can't change. It can mask and drop, and it can route, and it keeps the Elasticsearch credentials to itself.</p><p>But it isn’t the right answer for everything. For example, if:</p><ul><li><p>An ingest pipeline does what you need, use that. </p></li><li><p>You need to scale past one worker, split the traffic first. </p></li><li><p>You can change the client, send the data straight to Elasticsearch. </p></li><li><p>The client is built on an official Elasticsearch client, this gateway won't work for it.</p></li></ul><p>Clone the repo, and send the sample. Break it on purpose. That's the fastest way to see why each piece is there.</p><h2><strong>Additional references</strong></h2><ul><li><p><a href="https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-bulk">Bulk API</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-inputs-http">Logstash http input plugin</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-filters-aggregate">Logstash aggregate filter plugin</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/logstash/plugins/plugins-outputs-elasticsearch">Logstash Elasticsearch output plugin</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/logstash/environment-variables">Using environment variables in Logstash configs</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/logstash/processing#event-ordering">Logstash event ordering (processing details)</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/logstash/logstash-settings-file">logstash.yml settings reference</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/logstash/persistent-queues">Persistent queues</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/logstash/dead-letter-queues">Dead letter queues</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/logstash/monitoring-logstash-with-elastic-agent">Monitoring Logstash with Elastic Agent</a></p></li><li><p><a href="https://www.elastic.co/docs/manage-data/ingest/transform-enrich/ingest-pipelines">Ingest pipelines</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/logstash/monitoring-logstash">Monitoring Logstash with APIs</a></p></li><li><p><a href="https://www.elastic.co/docs/manage-data/data-store/data-streams/failure-store">Failure store</a></p></li><li><p><a href="https://www.elastic.co/docs/reference/elasticsearch/clients/javascript/connecting#product-check">Automatic product check (Elasticsearch JavaScript client)</a></p></li></ul>]]></content:encoded>
    <link>https://www.elastic.co/search-labs/blog/logstash-elasticsearch-bulk-api</link>
    <guid isPermaLink="true">https://www.elastic.co/search-labs/blog/logstash-elasticsearch-bulk-api</guid>
    <category><![CDATA[Index Data]]></category>
    <category><![CDATA[Operations]]></category>
    <dc:creator><![CDATA[Mohammad Sarwar,Someshwaran Mohankumar]]></dc:creator>
    <enclosure url="https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/blt853008e5c32b62b4/6b05346b5fe6e6d3b9830162/image1.png" length="0" type="image/png"/>
    <pubDate>Wed, 25 Nov 2026 15:30:00 GMT</pubDate>
  </item>
  </channel>
</rss>