Skip to content

Commit

Permalink
feat: custom dictionaries (#673)
Browse files Browse the repository at this point in the history
* feat: add custom dictionary methods (tests still WIP)

* chore: add docblocks

* feat: update tests

* debug: use API key dealer

* Revert "debug: use API key dealer"

This reverts commit dcf05df.

* feat: refactor tests to rely on objectID checks instead of nbHits

* feat: finzalize CTS

* chore: address feedback

* chore: remove unused method

* Add missing part of the url for stopABTest() method (#666)

* Handling of param array in the $queries array for multipleQueries method (#663)

* Add Algolia CTS (#665)

* Indexing and Settings tests for SearchIndexTest.php

* Add SearchTest method

* Add SynonymsTest method

* Add testQueryRules method

* Add testBatching method

* Add testReplacing and testExists methods + move sample data

* Add SearchClientTest.php

* Add multiQueries test

* Adding AccountTest.php and remove usage of SyncClient

* Adding SecuredApiKeysTest

* Adding AnalyticsClientTest.php

* Adding AnalyticsClientTest.php

* Adding InsightsClientTest.php

* Adding RecommendationClientTest.php + CS fixer

* Adding Mcm tests

* Add stopAbTest() test

* Remove unnecessary files

* Handling of param array in the $queries array for multipleQueries method (#663)

* Remove unnecessary files

* First required changes

* Removing self::assert* notations

* fix(cts): delete indexes initialisation

* Using wait() on saveObject() rather than multiResponse

* Removing all static arrays for indices

* Removing unwanted setPersonalizationStrategy call

* use secured index name

* Required changes after code review

* Adding loop to check if indices exist before additing the A/B Tests

* Adding security into loops

* Set cpts to 10

* Set cpts to 20

* Set cpts to 10

* Set cpts to 10

Co-authored-by: Chloe Liban <[email protected]>

* chore(CTS): add retry on tests (#672)

* chore(cts): fix api keys test

* chore(CTS): fix flakiness

* Revert "feat: custom dictionaries (#662)"

This reverts commit d4e3112.

* chore: address feedback

* chore: remove unused method

* chore(test): fix helper call

* fix: rebase

Co-authored-by: Devin Beeuwkes <[email protected]>
Co-authored-by: Damien Couchez <[email protected]>
  • Loading branch information
3 people authored Apr 5, 2021
1 parent 1c133ca commit 7f94af6
Show file tree
Hide file tree
Showing 3 changed files with 407 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/Response/DictionaryResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Algolia\AlgoliaSearch\Response;

use Algolia\AlgoliaSearch\Config\SearchConfig;
use Algolia\AlgoliaSearch\RequestOptions\RequestOptions;
use Algolia\AlgoliaSearch\SearchClient;

final class DictionaryResponse extends AbstractResponse
{
/* @var \Algolia\AlgoliaSearch\SearchClient */
private $client;

/* @var \Algolia\AlgoliaSearch\Config\SearchConfig */
private $config;

/* @var bool */
private $done = false;

/**
* DictionaryResponse constructor.
*/
public function __construct(array $apiResponse, SearchClient $client, SearchConfig $config)
{
$this->apiResponse = $apiResponse;
$this->client = $client;
$this->config = $config;
}

/**
* Wait for the task from this response to finish.
*
* @param array|RequestOptions $requestOptions
*
* @return $this
*/
public function wait($requestOptions = array())
{
$retryCount = 1;
$time = $this->config->getWaitTaskTimeBeforeRetry();

while (!$this->done) {
$res = $this->getTask($this->apiResponse['taskID'], $requestOptions);

if ('published' === $res['status']) {
$this->done = true;
break;
}

$retryCount++;
$factor = ceil($retryCount / 10);
usleep($factor * $time); // 0.1 second
}

return $this;
}

/**
* Get the task details.
*
* @param int|string $taskId
* @param array|RequestOptions $requestOptions
*
* @return mixed
*/
private function getTask($taskId, $requestOptions = array())
{
if (!$taskId) {
throw new \InvalidArgumentException('taskID cannot be empty');
}

return $this->client->custom(
'GET',
\Algolia\AlgoliaSearch\api_path('/1/task/%s', $taskId),
$requestOptions
);
}
}
146 changes: 146 additions & 0 deletions src/SearchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Algolia\AlgoliaSearch\RequestOptions\RequestOptions;
use Algolia\AlgoliaSearch\Response\AddApiKeyResponse;
use Algolia\AlgoliaSearch\Response\DeleteApiKeyResponse;
use Algolia\AlgoliaSearch\Response\DictionaryResponse;
use Algolia\AlgoliaSearch\Response\IndexingResponse;
use Algolia\AlgoliaSearch\Response\MultipleIndexBatchIndexingResponse;
use Algolia\AlgoliaSearch\Response\RestoreApiKeyResponse;
Expand Down Expand Up @@ -458,4 +459,149 @@ public function hasPendingMappings($requestOptions = array())
$requestOptions
);
}

/**
* Save entries to the given dictionary.
*
* @param string $dictionary
* @param array<array<string, mixed>> $entries
* @param array|RequestOptions $requestOptions
*
* @return DictionaryResponse
*/
public function saveDictionaryEntries($dictionary, $entries, $requestOptions = array())
{
$response = $this->api->write(
'POST',
api_path('/1/dictionaries/%s/batch', $dictionary),
array(
'clearExistingDictionaryEntries' => false,
'requests' => Helpers::buildBatch($entries, 'addEntry'),
),
$requestOptions
);

return new DictionaryResponse($response, $this, $this->config);
}

/**
* Replace all dictionary entries.
*
* @param string $dictionary
* @param array<array<string, mixed>> $entries
* @param array $requestOptions
*
* @return DictionaryResponse
*/
public function replaceDictionaryEntries($dictionary, $entries, $requestOptions = array())
{
$response = $this->api->write(
'POST',
api_path('/1/dictionaries/%s/batch', $dictionary),
array(
'clearExistingDictionaryEntries' => true,
'requests' => Helpers::buildBatch($entries, 'addEntry'),
),
$requestOptions
);

return new DictionaryResponse($response, $this, $this->config);
}

/**
* Delete dictionary entries by their objectID.
*
* @param string $dictionary
* @param array<string> $objectIDs
* @param array $requestOptions
*
* @return DictionaryResponse
*/
public function deleteDictionaryEntries($dictionary, $objectIDs, $requestOptions = array())
{
$entries = array_map(function ($objectID) {
return array('objectID' => $objectID);
}, $objectIDs);

$response = $this->api->write(
'POST',
api_path('/1/dictionaries/%s/batch', $dictionary),
array(
'clearExistingDictionaryEntries' => false,
'requests' => Helpers::buildBatch($entries, 'deleteEntry'),
),
$requestOptions
);

return new DictionaryResponse($response, $this, $this->config);
}

/**
* Clear all entries in the given dictionary.
*
* @param string $dictionary
* @param array|RequestOptions $requestOptions
*
* @return DictionaryResponse
*/
public function clearDictionaryEntries($dictionary, $requestOptions = array())
{
return $this->replaceDictionaryEntries($dictionary, array(), $requestOptions);
}

/**
* Search the dictionary for entries.
*
* @param string $dictionary
* @param string $query
* @param array|RequestOptions $requestOptions
*
* @return mixed
*/
public function searchDictionaryEntries($dictionary, $query, $requestOptions = array())
{
return $this->api->read(
'POST',
api_path('/1/dictionaries/%s/search', $dictionary),
array('query' => $query),
$requestOptions
);
}

/**
* Update the settings for all dictionaries.
*
* @param array<mixed> $dictionarySettings
* @param array $requestOptions
*
* @return DictionaryResponse
*/
public function setDictionarySettings($dictionarySettings, $requestOptions = array())
{
$response = $this->api->write(
'PUT',
api_path('/1/dictionaries/*/settings'),
$dictionarySettings,
$requestOptions
);

return new DictionaryResponse($response, $this, $this->config);
}

/**
* Get the settings for all dictionaries.
*
* @param array|RequestOptions $requestOptions
*
* @return mixed
*/
public function getDictionarySettings($requestOptions = array())
{
return $this->api->read(
'GET',
api_path('/1/dictionaries/*/settings'),
array(),
$requestOptions
);
}
}
Loading

0 comments on commit 7f94af6

Please sign in to comment.