Skip to content

Commit

Permalink
Add index aliases support
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed May 7, 2021
1 parent ff7269c commit 5ccbbc3
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test: ## Run tests

coverage: ## Run tests and generate the code coverage report
@printf "\033[93m→ Running tests and generating the code coverage report\033[0m\n"
@bin/phpunit --testdox --coverage-text
@XDEBUG_MODE=coverage bin/phpunit --testdox --coverage-text
@printf "\n\033[92m✔︎ Tests are completed and the report is generated\033[0m\n"

style-check: ## Check the code style
Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ There is also an option to refresh index immediately:
$documentManager->index('my_index', $documents, true);
```

There is also an option to set a custom routing path:
In addition, you can set a custom routing path:

```php
$documentManager->index('my_index', $documents, false, $routingPath = 'document_field');
$documentManager->index('my_index', $documents, false, 'my_field');
```

This will route documents to an elasticsearch shard based on the `document_field` value.
This will route documents to an Elasticsearch shard based on the document's `my_field` value.
Routing path can be specified using "dot" notation to access nested fields.

`$routingPath` can be specified in dot notation to access nested fields.
### Delete

Remove a document from the index:
Expand All @@ -254,15 +254,13 @@ If you want the index to be refreshed immediately pass `true` as the third argum
$documentManager->delete('my_index', $documents, true);
```

If you want to delete the document only at a specified shard then pass a `$routingPath` as the fourth argument:
You can also set a custom routing path to delete the document from a specific shard:

```php
$documentManager->delete('my_index', $documents, false, $routingPath = 'document_field');
$documentManager->delete('my_index', $documents, false, 'my_field');
```

`$routingPath` can be specified in dot notation to access nested fields.

You can also delete documents using query:
Finally, you can delete documents using query:

```php
$documentManager->deleteByQuery('my_index', ['match_all' => new \stdClass()]);
Expand Down Expand Up @@ -385,4 +383,4 @@ $suggestions = $response->getSuggestions();

// get the aggregations
$aggregations = $response->getAggregations();
```
```
20 changes: 3 additions & 17 deletions src/Documents/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ElasticAdapter\Documents;

use ElasticAdapter\Support\Arr;
use ElasticAdapter\Support\ArrayableInterface;

final class Document implements ArrayableInterface
Expand Down Expand Up @@ -32,26 +33,11 @@ public function getContent(): array
}

/**
* Get a field's value using "dot" notation.
*
* @return mixed field value at key or null if it doesn't exist
* @return mixed
*/
public function getField(string $key)
{
$content = $this->getContent();
if (isset($content[$key])) {
return $content[$key];
}

foreach (explode('.', $key) as $segment) {
if (!is_array($content) || !array_key_exists($segment, $content)) {
return null;
}

$content = $content[$segment];
}

return $content;
return Arr::get($this->content, $key);
}

public function toArray(): array
Expand Down
22 changes: 16 additions & 6 deletions src/Documents/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ public function __construct(Client $client)
/**
* @param Document[] $documents
*/
public function index(string $indexName, array $documents, bool $refresh = false, ?string $routingPath = null): self
{
public function index(
string $indexName,
array $documents,
bool $refresh = false,
?string $routingPath = null
): self {
$params = [
'index' => $indexName,
'refresh' => $refresh ? 'true' : 'false',
Expand All @@ -31,11 +35,12 @@ public function index(string $indexName, array $documents, bool $refresh = false

foreach ($documents as $document) {
$index = ['_id' => $document->getId()];

if (isset($routingPath)) {
$index['routing'] = $document->getField($routingPath);
}

$params['body'][] = ['index' => $index];
$params['body'][] = compact('index');
$params['body'][] = $document->getContent();
}

Expand All @@ -47,8 +52,12 @@ public function index(string $indexName, array $documents, bool $refresh = false
/**
* @param Document[] $documents
*/
public function delete(string $indexName, array $documents, bool $refresh = false, ?string $routingPath = null): self
{
public function delete(
string $indexName,
array $documents,
bool $refresh = false,
?string $routingPath = null
): self {
$params = [
'index' => $indexName,
'refresh' => $refresh ? 'true' : 'false',
Expand All @@ -57,11 +66,12 @@ public function delete(string $indexName, array $documents, bool $refresh = fals

foreach ($documents as $document) {
$delete = ['_id' => $document->getId()];

if (isset($routingPath)) {
$delete['routing'] = $document->getField($routingPath);
}

$params['body'][] = ['delete' => $delete];
$params['body'][] = compact('delete');
}

$this->client->bulk($params);
Expand Down
28 changes: 28 additions & 0 deletions src/Support/Arr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace ElasticAdapter\Support;

final class Arr
{
/**
* Get an item from an array using "dot" notation.
*
* @return mixed
*/
public static function get(array $array, string $key)
{
if (isset($array[$key])) {
return $array[$key];
}

foreach (explode('.', $key) as $segment) {
if (!array_key_exists($segment, $array)) {
return null;
}

$array = $array[$segment];
}

return $array;
}
}
5 changes: 3 additions & 2 deletions tests/Unit/Documents/DocumentManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* @covers \ElasticAdapter\Documents\DocumentManager
*
* @uses \ElasticAdapter\Support\Arr
* @uses \ElasticAdapter\Documents\Document
* @uses \ElasticAdapter\Search\Hit
* @uses \ElasticAdapter\Search\SearchRequest
Expand Down Expand Up @@ -98,7 +99,7 @@ public function test_documents_can_be_indexed_with_custom_routing(): void
$this->assertSame($this->documentManager, $this->documentManager->index('test', [
new Document('1', ['title' => 'Doc 1']),
new Document('2', ['title' => 'Doc 2']),
], true, $routingPath = 'title'));
], true, 'title'));
}

public function test_documents_can_be_deleted_with_refresh(): void
Expand Down Expand Up @@ -156,7 +157,7 @@ public function test_documents_can_be_deleted_with_custom_routing(): void
$this->assertSame($this->documentManager, $this->documentManager->delete('test', [
new Document('1', ['title' => 'Doc 1']),
new Document('2', ['title' => 'Doc 2']),
], true, $routingPath = 'title'));
], true, 'title'));
}

public function test_documents_can_be_deleted_by_query_with_refresh(): void
Expand Down
10 changes: 6 additions & 4 deletions tests/Unit/Documents/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@

/**
* @covers \ElasticAdapter\Documents\Document
*
* @uses \ElasticAdapter\Support\Arr
*/
final class DocumentTest extends TestCase
{
public function test_document_getters(): void
{
$document = new Document('123456', ['title' => 'foo', 'user' => ['name' => 'Elasticman']]);
$document = new Document('123456', ['title' => 'book', 'price' => 10]);

$this->assertSame('123456', $document->getId());
$this->assertSame(['title' => 'foo', 'user' => ['name' => 'Elasticman']], $document->getContent());
$this->assertSame('foo', $document->getField('title'));
$this->assertSame('Elasticman', $document->getField('user.name'));
$this->assertSame(['title' => 'book', 'price' => 10], $document->getContent());
$this->assertSame('book', $document->getField('title'));
$this->assertSame(10, $document->getField('price'));
$this->assertNull($document->getField('not_defined_key'));
$this->assertNull($document->getField('user.not_defined_key'));
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/Support/ArrTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace ElasticAdapter\Tests\Unit\Support;

use ElasticAdapter\Support\Arr;
use PHPUnit\Framework\TestCase;

/**
* @covers \ElasticAdapter\Support\Arr
*/
final class ArrTest extends TestCase
{
public function caseProvider(): array
{
return [
[['product' => ['price' => 10]], 'product', ['price' => 10]],
[['product' => ['price' => 10]], 'product.price', 10],
[['order' => ['items' => [['name' => 'first'], ['name' => 'second']]]], 'order.items.0.name', 'first'],
[['left' => 1], 'right', null],
];
}

/**
* @param mixed $item
*
* @dataProvider caseProvider
* @testdox Item with key $key can be retrieved from array
*/
public function test_item_can_be_received_using_dot_notation(array $array, string $key, $item): void
{
$this->assertSame($item, Arr::get($array, $key));
}
}

0 comments on commit 5ccbbc3

Please sign in to comment.