Skip to content

Commit ae82709

Browse files
committed
4268: Updated fixture loader to delete index before creating it
1 parent d38efb9 commit ae82709

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ above command downloads the fixtures from
5454
[GitHub](https://github.com/itk-dev/event-database-imports/tree/develop/src/DataFixtures/indexes) and loads them into
5555
ElasticSearch.
5656

57+
> [!TIP]
58+
> Use `task fixtures:load` to load all fixtures into Elasticsearch.
59+
5760
## Accessing the API
5861

5962
To access the API, a valid API key must be presented in the `X-Api-Key` header, e.g.

src/Fixtures/FixtureLoader.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function process(string $indexName, string $url): void
3939
{
4040
$items = $this->download($url);
4141

42+
$this->deleteIndex($indexName);
4243
$this->createIndex($indexName);
4344
$this->indexItems($indexName, $items);
4445
}
@@ -131,4 +132,28 @@ private function createIndex(string $indexName): void
131132
]);
132133
}
133134
}
135+
136+
/**
137+
* Deletes an index with the given name if it exists.
138+
*
139+
* @param string $indexName
140+
* The name of the index
141+
*
142+
* @throws ClientResponseException
143+
* If an error occurs during the Elasticsearch client request
144+
* @throws MissingParameterException
145+
* If the required parameter is missing
146+
* @throws ServerResponseException
147+
* If the server returns an error during the Elasticsearch request
148+
*/
149+
private function deleteIndex(string $indexName): void
150+
{
151+
if ($this->index->indexExists($indexName)) {
152+
// This creation of the index is not in den index service as this is the only place it should be used. In
153+
// production and in many cases, you should connect to the index managed by the backend (imports).
154+
$this->client->indices()->delete([
155+
'index' => $indexName,
156+
]);
157+
}
158+
}
134159
}

src/Service/ElasticSearch/ElasticSearchIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function indexExists($indexName): bool
2525
{
2626
try {
2727
/** @var Elasticsearch $response */
28-
$response = $this->client->indices()->getAlias(['name' => $indexName]);
28+
$response = $this->client->indices()->get(['index' => $indexName]);
2929

3030
return Response::HTTP_OK === $response->getStatusCode();
3131
} catch (ClientResponseException|ServerResponseException $e) {

0 commit comments

Comments
 (0)