Skip to content

Commit

Permalink
[BC] Removed first argument of IndexManager::reindex(), IndexManager:…
Browse files Browse the repository at this point in the history
…:delete(), IndexManager::update(), IndexManager::persistRaw()
  • Loading branch information
pmishev committed Jul 15, 2019
1 parent 5cd1bf1 commit 00052c8
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 89 deletions.
3 changes: 2 additions & 1 deletion Document/Provider/ElasticsearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct($documentClass, DocumentMetadataCollector $metadataC
*/
public function getDocuments()
{
$repo = $this->sourceIndexManager->getRepository($this->sourceDocumentClass);
$repo = $this->sourceIndexManager->getRepository();

/** @var ScrollAdapter $scrollAdapter */
$scrollAdapter = $repo->find(
Expand All @@ -84,6 +84,7 @@ public function getDocuments()
* Build and return a document from the data source, ready for insertion into ES
*
* @param int|string $id
*
* @return array
*/
public function getDocument($id)
Expand Down
8 changes: 4 additions & 4 deletions Document/Repository/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function count(array $searchBody = [], array $additionalRequestParams = [
*/
public function reindex($id)
{
$this->indexManager->reindex($this->documentClass, $id);
$this->indexManager->reindex($id);
}

/**
Expand All @@ -135,7 +135,7 @@ public function reindex($id)
*/
public function delete($id)
{
$this->indexManager->delete($this->documentClass, $id);
$this->indexManager->delete($id);
}

/**
Expand All @@ -148,7 +148,7 @@ public function delete($id)
*/
public function update($id, array $fields = [], $script = null, array $params = [])
{
$this->indexManager->update($this->documentClass, $id, $fields, $script, $params);
$this->indexManager->update($id, $fields, $script, $params);
}

/**
Expand All @@ -170,6 +170,6 @@ public function persist(DocumentInterface $document)
*/
public function persistRaw(array $documentArray)
{
$this->indexManager->persistRaw($this->documentClass, $documentArray);
$this->indexManager->persistRaw($documentArray);
}
}
70 changes: 38 additions & 32 deletions Manager/IndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,13 @@ public function getRepository()
/**
* Returns the data provider object for a type (provided in short class notation, e.g AppBundle:Product)
*
* @param string $documentClass The document class for the type
*
* @return ProviderInterface
*/
public function getDataProvider($documentClass)
public function getDataProvider()
{
$provider = $this->providerRegistry->getProviderInstance($documentClass);
$provider = $this->providerRegistry->getProviderInstance(
$this->getDocumentClass()
);

return $provider;
}
Expand Down Expand Up @@ -519,20 +519,19 @@ public function rebuildIndex($deleteOld = false, $cancelExistingRebuild = false)
* Rebuilds the data of a document and adds it to a bulk request for the next commit.
* Depending on the connection autocommit mode, the change may be committed right away.
*
* @param string $documentClass The document class in short notation (i.e. AppBundle:Product)
* @param string|int $id
*/
public function reindex(string $documentClass, $id)
public function reindex($id)
{
$documentMetadata = $this->metadataCollector->getDocumentMetadata($documentClass);
$documentClass = $this->getDocumentClass();

$dataProvider = $this->getDataProvider($documentClass);
$dataProvider = $this->getDataProvider();
$document = $dataProvider->getDocument($id);

switch (true) {
case $document instanceof DocumentInterface:
if (get_class($document) !== $documentMetadata->getClassName()) {
throw new Exception(sprintf('Document must be "%s", but "%s" was returned from data provider', $documentMetadata->getClassName(), get_class($document)));
if (get_class($document) !== $documentClass) {
throw new Exception(sprintf('Document must be [%s], but [%s] was returned from data provider', $documentClass, get_class($document)));
}
$this->persist($document);
break;
Expand All @@ -542,9 +541,9 @@ public function reindex(string $documentClass, $id)
throw new Exception(sprintf('The returned document array must include an "_id" field: (%s)', serialize($document)));
}
if ($document['_id'] != $id) {
throw new Exception(sprintf('The document id must be "%s", but "%s" was returned from data provider', $id, $document['_id']));
throw new Exception(sprintf('The document id must be [%s], but "%s" was returned from data provider', $id, $document['_id']));
}
$this->persistRaw($documentClass, $document);
$this->persistRaw($document);
break;

default:
Expand All @@ -560,12 +559,11 @@ public function reindex(string $documentClass, $id)
* Adds document removal to a bulk request for the next commit.
* Depending on the connection autocommit mode, the removal may be committed right away.
*
* @param string $documentClass The document class in short notation (i.e. AppBundle:Product)
* @param string $id Document ID to remove.
* @param string $id Document ID to remove.
*/
public function delete($documentClass, $id)
public function delete($id)
{
$documentMetadata = $this->metadataCollector->getDocumentMetadata($documentClass);
$documentMetadata = $this->metadataCollector->getDocumentMetadata($this->indexSettings['class']);

$this->getConnection()->addBulkOperation(
'delete',
Expand All @@ -583,16 +581,15 @@ public function delete($documentClass, $id)
/**
* Adds a document update to a bulk request for the next commit.
*
* @param string $documentClass The document class in short notation (i.e. AppBundle:Product)
* @param string $id Document id to update.
* @param array $fields Fields array to update (ignored if script is specified).
* @param string $script Script to update fields.
* @param array $queryParams Additional params to pass with the payload (upsert, doc_as_upsert, _source, etc.)
* @param array $metaParams Additional params to pass with the meta data in the bulk request (_version, _routing, etc.)
*/
public function update($documentClass, $id, array $fields = [], $script = null, array $queryParams = [], array $metaParams = [])
* @param string $id Document id to update.
* @param array $fields Fields array to update (ignored if script is specified).
* @param string $script Script to update fields.
* @param array $queryParams Additional params to pass with the payload (upsert, doc_as_upsert, _source, etc.)
* @param array $metaParams Additional params to pass with the meta data in the bulk request (_version, _routing, etc.)
*/
public function update($id, array $fields = [], $script = null, array $queryParams = [], array $metaParams = [])
{
$documentMetadata = $this->metadataCollector->getDocumentMetadata($documentClass);
$documentMetadata = $this->metadataCollector->getDocumentMetadata($this->indexSettings['class']);

// Add the id of the updated document to the meta params for the bulk request
$metaParams = array_merge(
Expand Down Expand Up @@ -637,20 +634,19 @@ public function persist(DocumentInterface $document, array $metaParams = [])
}

$documentArray = $this->documentConverter->convertToArray($document);
$this->persistRaw(get_class($document), $documentArray, $metaParams);
$this->persistRaw($documentArray, $metaParams);
}

/**
* Adds a prepared document array to a bulk request for the next commit.
* Depending on the connection autocommit mode, the update may be committed right away.
*
* @param string $documentClass The document class in short notation (i.e. AppBundle:Product)
* @param array $documentArray The document to index in ES
* @param array $metaParams Additional params to pass with the meta data in the bulk request (_version, _routing, etc.)
* @param array $documentArray The document to index in ES
* @param array $metaParams Additional params to pass with the meta data in the bulk request (_version, _routing, etc.)
*/
public function persistRaw($documentClass, array $documentArray, array $metaParams = [])
public function persistRaw(array $documentArray, array $metaParams = [])
{
$documentMetadata = $this->metadataCollector->getDocumentMetadata($documentClass);
$documentMetadata = $this->metadataCollector->getDocumentMetadata($this->indexSettings['class']);

$this->getConnection()->addBulkOperation(
'index',
Expand Down Expand Up @@ -705,7 +701,7 @@ protected function copyDataFromOldToNewIndex(string $newIndex, string $oldIndex)
$this->setWriteAlias($newIndex);

if (is_array($document)) {
$this->persistRaw($documentClass, $document);
$this->persistRaw($document);
} else {
$this->persist($document);
}
Expand Down Expand Up @@ -775,4 +771,14 @@ protected function getLiveIndexPreparedForRebuilding($cancelExistingRebuild, $re

return $liveIndex;
}

/**
* Get FQN of document class managed by this index manager
*
* @return string
*/
protected function getDocumentClass()
{
return $this->metadataCollector->getDocumentMetadata($this->indexSettings['class'])->getClassName();
}
}
6 changes: 3 additions & 3 deletions Resources/doc/crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ $im = $this->get('sfes.index.product');

## Repositories

When you need to work with a specific type, you can do so through the Repository class of an entity.
When you need to work with documents in the index, you can do so through the Repository class of an entity.
The default Repository is mostly a convenience class, as its methods are also available either through the IndexManager or the Finder services. However it may be useful to have type-specific methods in your own custom repositories.
You can get a type's repository through the index manager that manages it:
You can get a document's repository through the index manager that manages it:

```php
$repo = $im->getRepository('AppBundle:Product');
$repo = $im->getRepository();
```

## Create a document
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ $im->getConnection()->commit();

When retrieving results, you can get a specific language value like this:
```php
$repo = $this->get('sfes.index.product')->getRepository('AppBundle:Product');
$repo = $this->get('sfes.index.product')->getRepository();
$product = $repo->getById(5);
$title = $product->title->getValue('en');
```
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/results.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ In addition to that, the **DocumentIterator** itself has some extra methods:
* `getAggregations()` Return the aggregations, if you have requested any.

```php
$repo = $this->get('sfes.index.product')->getRepository('AppBundle:Product');
$repo = $this->get('sfes.index.product')->getRepository();
$searchBody = [
'query' => [
'match_all' => (object) []
Expand Down
4 changes: 2 additions & 2 deletions Resources/doc/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
## Get a document by id

```php
$repo = $this->get('sfes.index.product')->getRepository('AppBundle:Product');
$repo = $this->get('sfes.index.product')->getRepository();
$product = $repo->getById(5); // 5 is the _id of the document in Elasticsearch
```
> The result is returned as an instance of the Product class. An optional second parameter of getByID() allows you to specify different format of the result. See [results types](#resulttypes) below for more information.
## Find documents using a search query

```php
$repo = $this->get('sfes.index.product')->getRepository('AppBundle:Product');
$repo = $this->get('sfes.index.product')->getRepository();
$searchBody = [
'query' => [
'match_all' => (object) []
Expand Down
2 changes: 1 addition & 1 deletion Tests/AbstractElasticsearchTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function populateElasticsearchWithData($indexManager, array $data)
if (!empty($data)) {
foreach ($data as $type => $documents) {
foreach ($documents as $document) {
$indexManager->persistRaw($type, $document);
$indexManager->persistRaw($document);
}
}
try {
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Finder/Adapter/KnpPaginatorAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function getDataArray()
public function testPagination()
{
/** @var Repository $repo */
$repo = $this->getIndexManager('bar')->getRepository('AcmeBarBundle:Product');
$repo = $this->getIndexManager('bar')->getRepository();
$paginator = $this->getContainer()->get('knp_paginator');

$query = ['query' => ['match_all' => (object) []], 'sort' => ['_uid' => ['order' =>'asc']]];
Expand Down Expand Up @@ -128,7 +128,7 @@ public function testPagination()
public function testInvalidResultsType()
{
/** @var Repository $repo */
$repo = $this->getIndexManager('bar')->getRepository('AcmeBarBundle:Product');
$repo = $this->getIndexManager('bar')->getRepository();
$paginator = $this->getContainer()->get('knp_paginator');

$query = ['query' => ['match_all' => (object) []]];
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Finder/Adapter/ScrollAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function getDataArray()
public function testScanScroll()
{
/** @var Repository $repo */
$repo = $this->getIndexManager('bar')->getRepository('AcmeBarBundle:Product');
$repo = $this->getIndexManager('bar')->getRepository();

$query = ['query' => ['term' => ['title' => ['value' => 'product']]]];

Expand Down
Loading

0 comments on commit 00052c8

Please sign in to comment.