Skip to content

Commit

Permalink
Merge pull request #1523 from patrickkusebauch/feature/Elastica
Browse files Browse the repository at this point in the history
Support for Elastica client up to v7.0.0, with optional drop support for ES types
  • Loading branch information
Seldaek authored Dec 14, 2020
2 parents 9fc7a8a + 79d1213 commit 5fbd261
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"phpunit/phpunit": "^8.5",
"predis/predis": "^1.1",
"rollbar/rollbar": "^1.3",
"ruflin/elastica": ">=0.90 <3.0",
"ruflin/elastica": ">=0.90 <7.0.1",
"swiftmailer/swiftmailer": "^5.3|^6.0",
"phpstan/phpstan": "^0.12.59"
},
Expand Down
7 changes: 6 additions & 1 deletion src/Monolog/Formatter/ElasticaFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function getIndex(): string
return $this->index;
}

/**
* @deprecated since Elastica 7 type has no effect
*/
public function getType(): string
{
return $this->type;
Expand All @@ -72,7 +75,9 @@ protected function getDocument(array $record): Document
{
$document = new Document();
$document->setData($record);
$document->setType($this->type);
if(method_exists($document, 'setType')) {
$document->setType($this->type);
}
$document->setIndex($this->index);

return $document;
Expand Down
2 changes: 1 addition & 1 deletion src/Monolog/Handler/ElasticaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* $client = new \Elastica\Client();
* $options = array(
* 'index' => 'elastic_index_name',
* 'type' => 'elastic_doc_type',
* 'type' => 'elastic_doc_type', Types have been removed in Elastica 7
* );
* $handler = new ElasticaHandler($client, $options);
* $log = new Logger('application');
Expand Down
7 changes: 4 additions & 3 deletions tests/Monolog/Formatter/ElasticaFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ public function testFormat()
$this->assertInstanceOf('Elastica\Document', $doc);

// Document parameters
$params = $doc->getParams();
$this->assertEquals('my_index', $params['_index']);
$this->assertEquals('doc_type', $params['_type']);
$this->assertEquals('my_index', $doc->getIndex());
if(method_exists($doc, 'getType')) {
$this->assertEquals('doc_type', $doc->getType());
}

// Document data values
$data = $doc->getData();
Expand Down
66 changes: 63 additions & 3 deletions tests/Monolog/Handler/ElasticaHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function providerTestConnectionErrors()
}

/**
* Integration test using localhost Elastic Search server
* Integration test using localhost Elastic Search server version <7
*
* @covers Monolog\Handler\ElasticaHandler::__construct
* @covers Monolog\Handler\ElasticaHandler::handleBatch
Expand Down Expand Up @@ -209,6 +209,61 @@ public function testHandleIntegration()
$client->request("/{$this->options['index']}", Request::DELETE);
}

/**
* Integration test using localhost Elastic Search server version 7+
*
* @covers Monolog\Handler\ElasticaHandler::__construct
* @covers Monolog\Handler\ElasticaHandler::handleBatch
* @covers Monolog\Handler\ElasticaHandler::bulkSend
* @covers Monolog\Handler\ElasticaHandler::getDefaultFormatter
*/
public function testHandleIntegrationNewESVersion()
{
$msg = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['foo' => 7, 'bar', 'class' => new \stdClass],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];

$expected = $msg;
$expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);
$expected['context'] = [
'class' => '[object] (stdClass: {})',
'foo' => 7,
0 => 'bar',
];

$client = new Client();
$handler = new ElasticaHandler($client, $this->options);

try {
$handler->handleBatch([$msg]);
} catch (\RuntimeException $e) {
$this->markTestSkipped("Cannot connect to Elastic Search server on localhost");
}

// check document id from ES server response
$documentId = $this->getCreatedDocId($client->getLastResponse());
$this->assertNotEmpty($documentId, 'No elastic document id received');

// retrieve document source from ES and validate
$document = $this->getDocSourceFromElastic(
$client,
$this->options['index'],
null,
$documentId
);
$this->assertEquals($expected, $document);

// remove test index from ES
$client->request("/{$this->options['index']}", Request::DELETE);
}


/**
* Return last created document id from ES response
* @param Response $response Elastica Response object
Expand All @@ -226,13 +281,18 @@ protected function getCreatedDocId(Response $response)
* Retrieve document by id from Elasticsearch
* @param Client $client Elastica client
* @param string $index
* @param string $type
* @param ?string $type
* @param string $documentId
* @return array
*/
protected function getDocSourceFromElastic(Client $client, $index, $type, $documentId)
{
$resp = $client->request("/{$index}/{$type}/{$documentId}", Request::GET);
if($type === null) {
$path = "/{$index}/_doc/{$documentId}";
} else {
$path = "/{$index}/{$type}/{$documentId}";
}
$resp = $client->request($path, Request::GET);
$data = $resp->getData();
if (!empty($data['_source'])) {
return $data['_source'];
Expand Down

0 comments on commit 5fbd261

Please sign in to comment.