Skip to content

Commit

Permalink
Merge pull request #58 from CheapHasz/bulk-index
Browse files Browse the repository at this point in the history
Added bulkIndex
  • Loading branch information
CheapHasz authored Jan 21, 2019
2 parents ff716a2 + bd07225 commit a0e5916
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"symfony/serializer": "^3.3",
"guzzlehttp/ringphp": "^1.1",
"symfony/yaml": "^3.3",
"symfony/var-dumper": "^3.3"
"symfony/var-dumper": "^3.3",
"webmozart/assert": "^1.0"
},
"suggest": {
"symfony/serializer": "Is required when working with the SymfonySerializerBridge"
Expand Down
9 changes: 9 additions & 0 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public function index(array $params)
$this->client->index($params);
}

/**
* @params array
*/
public function bulkIndex(array $params): array
{
$params['index'] = $this->getMainIndexName();
return $this->client->bulk($params);
}

/**
* @params array
*/
Expand Down
23 changes: 23 additions & 0 deletions src/ObjectIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Novaway\ElasticsearchClient;

use Webmozart\Assert\Assert;

class ObjectIndexer
{
/** @var Index */
Expand Down Expand Up @@ -34,6 +36,27 @@ public function index(Indexable $object, string $type = "_doc")
$this->index->index($params);
}

/**
* @param Indexable[] $objects
* @param string $type
*/
public function bulkIndex(array $objects, string $type = "_doc"): array
{
$params['type'] = $type;

Assert::allIsInstanceOf($objects, Indexable::class);

foreach ($objects as $indexable) {
$key = $indexable->shouldBeIndexed() ? 'index' : 'delete';
$body[] = [$key => ['_id' => $indexable->getId()]];
if ($indexable->shouldBeIndexed()) {
$body[] = $indexable->toArray();
}
}
$params['body'] = $body;
return $this->index->bulkIndex($params);
}

/**
* @param Indexable $object
* @param string $type
Expand Down
28 changes: 25 additions & 3 deletions test/functional/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Novaway\ElasticsearchClient\Score\ScriptScore;
use Novaway\ElasticsearchClient\Script\ScriptField;
use Symfony\Component\Yaml\Yaml;
use Test\Functional\Novaway\ElasticsearchClient\Context\Gizmos\DesindexableObject;
use Test\Functional\Novaway\ElasticsearchClient\Context\Gizmos\IndexableObject;

/**
Expand Down Expand Up @@ -166,11 +167,11 @@ public function iAddObjectsOfTypeToIndexWithData($objectType, $indexName, TableN
$objectIndexer = new ObjectIndexer($this->getIndex($indexName));

$objectListHash = $objectList->getHash();
$indexableObjects = [];
foreach ($objectListHash as $objectListRow) {
$indexableObject = new IndexableObject($objectListRow['id'], $objectListRow);
$objectIndexer->index($indexableObject, $objectType);
$indexableObjects[] = new IndexableObject($objectListRow['id'], $objectListRow);
}

$objectIndexer->bulkIndex($indexableObjects, $objectType);
sleep(1);
}

Expand Down Expand Up @@ -207,6 +208,27 @@ public function iDeleteTheObjectWithIdOfTypeIndexedIn($id, $objectType, $indexNa
$objectIndexer->removeById($id, $objectType);
}

/**
* @When I bulk delete the objects with ids :ids of type :objectType indexed in :indexName
*/
public function iBulkDeleteTheObjectWithIdOfTypeIndexedIn($ids, $objectType, $indexName)
{
$objectIndexer = new ObjectIndexer($this->getIndex($indexName));
$objects = [];
foreach ($ids as $id) {
$objects[] = new DesindexableObject($id, []);
}
$objectIndexer->bulkIndex($objects, $objectType);
}

/**
* @Then the object of type :objectType indexed in :indexName with id :id exists
*/
public function theObjectOfTypeIndexedInWithIdExists($objectType, $indexName, $id)
{
$this->assert->integer($this->httpGetStatus(sprintf('/%s/%s/%s', $indexName, $objectType, $id)))->isEqualTo(200);
}

/**
* @Then the object of type :objectType indexed in :indexName with id :id does not exist
*/
Expand Down
13 changes: 13 additions & 0 deletions test/functional/bootstrap/Gizmos/DesindexableObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


namespace Test\Functional\Novaway\ElasticsearchClient\Context\Gizmos;


class DesindexableObject extends IndexableObject
{
public function shouldBeIndexed(): bool
{
return false;
}
}
17 changes: 17 additions & 0 deletions test/functional/features/index.feature
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ Feature: Add and remove data to the elasticsearch index
When I delete the object with id "1" of type "_doc" indexed in "my_index"
Then the object of type "_doc" indexed in "my_index" with id "1" does not exist

@bulk-delete
Scenario: Delete bulk objects
Given I reload the index named "my_index"
And I add objects of type "_doc" to index "my_index" with data :
| id | first_name | nick_name | age |
| 1 | Barry | flash | 33 |
| 2 | Diana | Wonder Woman | 910 |
| 3 | Bruce | Batman | 45 |
| 4 | Barbara | Batgirl | 27 |
| 5 | Oliver | Green Arrow | 35 |
When I bulk delete the objects with ids "[1;3;5]" of type "_doc" indexed in "my_index"
Then the object of type "_doc" indexed in "my_index" with id "1" does not exist
And the object of type "_doc" indexed in "my_index" with id "2" exists
And the object of type "_doc" indexed in "my_index" with id "3" does not exist
And the object of type "_doc" indexed in "my_index" with id "4" exists
And the object of type "_doc" indexed in "my_index" with id "5" does not exist

@hotswap
Scenario: Hotswap
Given I reload the index named "my_index"
Expand Down

0 comments on commit a0e5916

Please sign in to comment.