Преглед изворни кода

Add an option to set the op_type for the ElasticsearchHandler (#1766)

Co-authored-by: Identei <>
Identei пре 2 година
родитељ
комит
f58e4f47b3

+ 2 - 1
src/Monolog/Handler/ElasticsearchHandler.php

@@ -85,6 +85,7 @@ class ElasticsearchHandler extends AbstractProcessingHandler
                 'index'        => 'monolog', // Elastic index name
                 'type'         => '_doc',    // Elastic document type
                 'ignore_error' => false,     // Suppress Elasticsearch exceptions
+                'op_type'      => 'index',   // Elastic op_type (index or create) (https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#docs-index-api-op_type)
             ],
             $options
         );
@@ -162,7 +163,7 @@ class ElasticsearchHandler extends AbstractProcessingHandler
 
             foreach ($records as $record) {
                 $params['body'][] = [
-                    'index' => $this->needsType ? [
+                    $this->options['op_type'] => $this->needsType ? [
                         '_index' => $record['_index'],
                         '_type'  => $record['_type'],
                     ] : [

+ 2 - 0
tests/Monolog/Handler/ElasticsearchHandlerTest.php

@@ -33,6 +33,7 @@ class ElasticsearchHandlerTest extends TestCase
     protected array $options = [
         'index' => 'my_index',
         'type'  => 'doc_type',
+        'op_type' => 'index',
     ];
 
     public function setUp(): void
@@ -93,6 +94,7 @@ class ElasticsearchHandlerTest extends TestCase
             'index' => $this->options['index'],
             'type' => $this->options['type'],
             'ignore_error' => false,
+            'op_type' => $this->options['op_type'],
         ];
 
         if ($this->client instanceof Client8 || $this->client::VERSION[0] === '7') {