diff --git a/README.md b/README.md index 7cabd51..f4c2383 100644 --- a/README.md +++ b/README.md @@ -475,6 +475,22 @@ $new_types = $mySearchEngine->addType('product'); // Add new type $mySearchEngine->deleteType('product'); // Remove the type and all items within it. ``` +**Warning:** Type deletion is an *asynchronous operation*, that keeps going after the `deleteType` method has returned `true`. If you try to perform `addItem`, `deleteItem`, `updateItem` or `updateItems` right after a `deleteType` operation, chances are you get an `IndexingInProgress` exception, because those ops aren't allowed while the `deleteType` is still in progress. If that happens, just wait a little and try again. + +```php +$mySearchEngine->deleteType('estudio'); // async op +$new_types = $mySearchEngine->addType('estudio'); // ok +$notReadyYet = true; +while($notReadyYet){ + try{ + $mySearchEngine->addItem('estudio', 'AAA', $someItem); // it may not be ready yet!! + $notReadyYet = false; + } catch (IndexingInProgress $ipe) { + sleep(3); // just wait a little + } +} +``` + #### Items Management ##### Single Item diff --git a/src/Management/Errors/IndexingInProgress.php b/src/Management/Errors/IndexingInProgress.php new file mode 100644 index 0000000..50c1da6 --- /dev/null +++ b/src/Management/Errors/IndexingInProgress.php @@ -0,0 +1,5 @@ +client->managementApiCall('DELETE', "{$this->hashid}/types/{$datatype}"); - return $result['statusCode'] == 204; + return $result['statusCode'] == 202; } public function items($datatype) { diff --git a/src/Search/Client.php b/src/Search/Client.php index 98627b5..9ad3b0c 100644 --- a/src/Search/Client.php +++ b/src/Search/Client.php @@ -32,12 +32,10 @@ * Basic client to perform requests to Doofinder search servers. */ class Client { - const URL_SUFFIX = '-search.doofinder.com'; const DEFAULT_TIMEOUT = 10000; const DEFAULT_RPP = 10; const DEFAULT_PARAMS_PREFIX = 'dfParam_'; const DEFAULT_API_VERSION = '5'; - const VERSION = '5.4.3'; private $api_key = null; // Authentication private $hashid = null; // ID of the Search Engine @@ -84,7 +82,6 @@ public function __construct($hashid, $api_key, $fromParams = false, $init_option if (2 === count($zone_key_array)) { $this->api_key = $zone_key_array[1]; $this->zone = $zone_key_array[0]; - $this->url = "https://" . $this->zone . self::URL_SUFFIX; } else { throw new Error("API Key is no properly set."); } @@ -133,6 +130,14 @@ public function __construct($hashid, $api_key, $fromParams = false, $init_option } } + private function getSearchUrl($entryPoint = 'search', $params = array()) { + return sprintf("https://%s-search.doofinder.com/%s/%s?%s", + $this->zone, + $this->apiVersion, + $entryPoint, + http_build_query($this->sanitize($params))); + } + private function addPrefix($value) { return $this->paramsPrefix.$value; } @@ -146,13 +151,10 @@ private function getRequestHeaders(){ return $headers; } - private function apiCall($entry_point = 'search', $params = array()){ + private function apiCall($entryPoint = 'search', $params = array()){ $params['hashid'] = $this->hashid; - $session = curl_init(sprintf("%s/%s/%s?%s", $this->url, - $this->apiVersion, - $entry_point, - http_build_query($this->sanitize($params)))); + $session = curl_init($this->getSearchUrl($entryPoint, $params)); // Configure cURL to return response but not headers curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'GET');