diff --git a/composer.json b/composer.json index c29ef08..1032688 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "drupal/search_api_solr": "^3.3", "http-interop/http-factory-guzzle": "^1.0", "symfony/property-access": "^4.3", + "symfony/property-info": "^4.3", "symfony/serializer": "^3.4" }, "require-dev": { diff --git a/lib/src/Api/ApiBase.php b/lib/src/Api/ApiBase.php index 81ef017..725dbd9 100644 --- a/lib/src/Api/ApiBase.php +++ b/lib/src/Api/ApiBase.php @@ -8,7 +8,10 @@ use OpenEuropa\EnterpriseSearchClient\ClientInterface; use Psr\Http\Message\StreamInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; +use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\SerializerInterface; @@ -151,7 +154,8 @@ protected function getMultipartStream(array $elements): StreamInterface { protected function getSerializer(): SerializerInterface { if ($this->serializer === NULL) { $this->serializer = new Serializer([ - new ObjectNormalizer(), + new ObjectNormalizer(null, null, null, new PhpDocExtractor()), + new ArrayDenormalizer(), ], [ new JsonEncoder(), ]); diff --git a/lib/src/Api/SearchApi.php b/lib/src/Api/SearchApi.php index fee8994..e9c407c 100644 --- a/lib/src/Api/SearchApi.php +++ b/lib/src/Api/SearchApi.php @@ -4,11 +4,12 @@ namespace OpenEuropa\EnterpriseSearchClient\Api; +use OpenEuropa\EnterpriseSearchClient\Model\Search; use Symfony\Component\OptionsResolver\OptionsResolver; class SearchApi extends ApiBase { - public function search(array $parameters = []) { + public function search(array $parameters = []): Search { $resolver = $this->getOptionResolver(); $resolver->setRequired('text') @@ -21,6 +22,11 @@ public function search(array $parameters = []) { $queryParameters = array_intersect_key($parameters, $queryKeys); $bodyParameters = array_diff_key($parameters, $queryKeys); $response = $this->send('POST', 'rest/search', $queryParameters, $bodyParameters, true); + + /** @var Search $search */ + $search = $this->getSerializer()->deserialize((string) $response->getBody(), Search::class, 'json'); + + return $search; } /** diff --git a/lib/src/Model/Document.php b/lib/src/Model/Document.php new file mode 100644 index 0000000..b385531 --- /dev/null +++ b/lib/src/Model/Document.php @@ -0,0 +1,420 @@ +accessRestriction; + } + + /** + * Sets if the document has access restrictions. + * + * @param bool $accessRestriction + * A boolean value. + */ + public function setAccessRestriction(bool $accessRestriction): void { + $this->accessRestriction = $accessRestriction; + } + + /** + * Returns the API version. + * + * @return string + * The API version. + */ + public function getApiVersion(): string { + return $this->apiVersion; + } + + /** + * Sets the API version. + * + * @param string $apiVersion + * The API version. + */ + public function setApiVersion(string $apiVersion): void { + $this->apiVersion = $apiVersion; + } + + /** + * Returns the list of children documents. + * + * @return \OpenEuropa\EnterpriseSearchClient\Model\Document[] + * An array of documents. + */ + public function getChildren(): array { + return $this->children; + } + + /** + * Sets the children documents. + * + * @param \OpenEuropa\EnterpriseSearchClient\Model\Document[] $children + * The children documents. + */ + public function setChildren(array $children): void { + $this->children = $children; + } + + /** + * Returns the document content. + * + * @return string + * The document content. + */ + public function getContent(): string { + return $this->content; + } + + /** + * Sets the document content. + * + * @param string $content + * The document content. + */ + public function setContent(string $content): void { + $this->content = $content; + } + + /** + * Returns the document content type. + * + * @return string + * The document content type. + */ + public function getContentType(): string { + return $this->contentType; + } + + /** + * Sets the document content type. + * + * @param string $contentType + * The document content type. + */ + public function setContentType(string $contentType): void { + $this->contentType = $contentType; + } + + /** + * @return string + */ + public function getDatabase(): string { + return $this->database; + } + + /** + * @param string|null $database + */ + public function setDatabase(?string $database): void { + $this->database = $database; + } + + /** + * @return string + */ + public function getDatabaseLabel(): string { + return $this->databaseLabel; + } + + /** + * @param string|null $databaseLabel + */ + public function setDatabaseLabel(?string $databaseLabel): void { + $this->databaseLabel = $databaseLabel; + } + + /** + * @return string + */ + public function getGroupById(): string { + return $this->groupById; + } + + /** + * @param string|null $groupById + */ + public function setGroupById(?string $groupById): void { + $this->groupById = $groupById; + } + + /** + * Returns the document language. + * + * @return string + * The document language. + */ + public function getLanguage(): string { + return $this->language; + } + + /** + * Sets the document language. + * + * @param string $language + * The document language. + */ + public function setLanguage(string $language): void { + $this->language = $language; + } + + /** + * Returns the document metadata. + * + * @return array + * A nested array of field names and values. + */ + public function getMetadata(): array { + return $this->metadata; + } + + /** + * Sets the document metadata. + * + * @param array $metadata + * A nested array of field names and values. + */ + public function setMetadata(array $metadata): void { + $this->metadata = $metadata; + } + + /** + * Returns the number of pages in the document. + * + * @return int + * The number of pages in the document. + */ + public function getPages(): int { + return $this->pages; + } + + /** + * Sets the number of pages in the document. + * + * @param int|null $pages + * The number of pages in the document. + */ + public function setPages(?int $pages): void { + $this->pages = $pages; + } + + /** + * Returns the document unique reference. + * + * @return string + * The document unique reference. + */ + public function getReference(): string { + return $this->reference; + } + + /** + * Sets the document unique reference. + * + * @param string $reference + * The document unique reference. + */ + public function setReference(string $reference): void { + $this->reference = $reference; + } + + /** + * Returns the document summary. + * + * @return string + * The document summary. + */ + public function getSummary(): string { + return $this->summary; + } + + /** + * Sets the document summary. + * + * @param string|null $summary + * The document summary. + */ + public function setSummary(?string $summary): void { + $this->summary = $summary; + } + + /** + * Returns the document title. + * + * @return string + * The document title. + */ + public function getTitle(): ?string { + return $this->title; + } + + /** + * Sets the document title. + * + * @param string|null $title + * The document title. + */ + public function setTitle(?string $title): void { + $this->title = $title; + } + + /** + * Returns the document url. + * + * @return string + * The document url. + */ + public function getUrl(): string { + return $this->url; + } + + /** + * Sets the document url. + * + * @param string $url + * The document url. + */ + public function setUrl(string $url): void { + $this->url = $url; + } + + /** + * Returns the document weight. + * + * @return float + * The document weight. + */ + public function getWeight(): float { + return $this->weight; + } + + /** + * Sets the document weight. + * + * @param float $weight + * The document weight. + */ + public function setWeight(float $weight): void { + $this->weight = $weight; + } + +} diff --git a/lib/src/Model/Ingestion.php b/lib/src/Model/Ingestion.php index e3f29f4..fc16891 100644 --- a/lib/src/Model/Ingestion.php +++ b/lib/src/Model/Ingestion.php @@ -4,7 +4,10 @@ namespace OpenEuropa\EnterpriseSearchClient\Model; -class Ingestion implements ModelInterface { +/** + * A class that represents an ingestion data transfer object. + */ +class Ingestion { /** * The API version. diff --git a/lib/src/Model/ModelInterface.php b/lib/src/Model/ModelInterface.php deleted file mode 100644 index 4dcc6f1..0000000 --- a/lib/src/Model/ModelInterface.php +++ /dev/null @@ -1,9 +0,0 @@ -language; + } + + /** + * Sets the query language code. + * + * @param string $language + * The language code. + */ + public function setLanguage(string $language): void { + $this->language = $language; + } + + /** + * Returns the query probability. + * + * @return float + */ + public function getProbability(): float { + return $this->probability; + } + + /** + * Sets the query probability. + * + * @param float $probability + * The query probability. + */ + public function setProbability(float $probability): void { + $this->probability = $probability; + } + +} diff --git a/lib/src/Model/Search.php b/lib/src/Model/Search.php new file mode 100644 index 0000000..68e2d76 --- /dev/null +++ b/lib/src/Model/Search.php @@ -0,0 +1,363 @@ +apiVersion; + } + + /** + * Sets the API version. + * + * @param string $apiVersion + * The API version. + */ + public function setApiVersion(string $apiVersion): void { + $this->apiVersion = $apiVersion; + } + + /** + * Returns the list of best bets documents. + * + * @return \OpenEuropa\EnterpriseSearchClient\Model\Document[] + * The best bets documents. + */ + public function getBestBets(): array { + return $this->bestBets; + } + + /** + * Sets the list of best bets documents. + * + * @param \OpenEuropa\EnterpriseSearchClient\Model\Document[] $bestBets + * The best bets documents. + */ + public function setBestBets(array $bestBets): void { + $this->bestBets = $bestBets; + } + + /** + * Returns the field used to group the results. + * + * @return string|null + * The field used to group the results. + */ + public function getGroupByField(): ?string { + return $this->groupByField; + } + + /** + * Sets the field used to group the results. + * + * @param string|null $groupByField + * The field used to group the results. + */ + public function setGroupByField(?string $groupByField): void { + $this->groupByField = $groupByField; + } + + /** + * Returns the page number. + * + * @return int + * The page number. + */ + public function getPageNumber(): int { + return $this->pageNumber; + } + + /** + * Sets the page number. + * + * @param int $pageNumber + * The page number. + */ + public function setPageNumber(int $pageNumber): void { + $this->pageNumber = $pageNumber; + } + + /** + * Returns the page size. + * + * @return int + * The page size. + */ + public function getPageSize(): int { + return $this->pageSize; + } + + /** + * Sets the page size. + * + * @param int $pageSize + * The page size. + */ + public function setPageSize(int $pageSize): void { + $this->pageSize = $pageSize; + } + + /** + * Returns the query language. + * + * @return \OpenEuropa\EnterpriseSearchClient\Model\QueryLanguage + * The query language. + */ + public function getQueryLanguage(): QueryLanguage { + return $this->queryLanguage; + } + + /** + * Sets the query language. + * + * @param \OpenEuropa\EnterpriseSearchClient\Model\QueryLanguage $queryLanguage + * The query language. + */ + public function setQueryLanguage(QueryLanguage $queryLanguage): void { + $this->queryLanguage = $queryLanguage; + } + + /** + * Returns the response time. + * + * @return int + * The response time. + */ + public function getResponseTime(): int { + return $this->responseTime; + } + + /** + * Sets the response time. + * + * @param int $responseTime + * The response time. + */ + public function setResponseTime(int $responseTime): void { + $this->responseTime = $responseTime; + } + + /** + * Returns the list of result documents. + * + * @return \OpenEuropa\EnterpriseSearchClient\Model\Document[] + * An array of documents. + */ + public function getResults(): array { + return $this->results; + } + + /** + * Sets the list of result documents. + * + * @param \OpenEuropa\EnterpriseSearchClient\Model\Document[] $results + * An array of documents. + */ + public function setResults(array $results): void { + $this->results = $results; + } + + /** + * Returns the sort used for the search. + * + * @return string + * The field and sort type used for sorting. + */ + public function getSort(): string { + return $this->sort; + } + + /** + * Sets the search sort. + * + * @param string $sort + * The field and sort type used for sorting, separated by a colon. + */ + public function setSort(string $sort): void { + $this->sort = $sort; + } + + /** + * Returns the spelling suggestion. + * + * @return string|null + * The spelling suggestion. + */ + public function getSpellingSuggestion(): ?string { + return $this->spellingSuggestion; + } + + /** + * Sets the spelling suggestion. + * + * @param string|null $spellingSuggestion + * The spelling suggestion. + */ + public function setSpellingSuggestion(?string $spellingSuggestion): void { + $this->spellingSuggestion = $spellingSuggestion; + } + + /** + * Returns the search terms. + * + * @return string + * The search terms. + */ + public function getTerms(): string { + return $this->terms; + } + + /** + * Sets the search terms. + * + * @param string $terms + * The search terms. + */ + public function setTerms(string $terms): void { + $this->terms = $terms; + } + + /** + * Returns the total number of results. + * + * @return int + * The total number of results. + */ + public function getTotalResults(): int { + return $this->totalResults; + } + + /** + * Sets the total number of results. + * + * @param int $totalResults + * The total number of results. + */ + public function setTotalResults(int $totalResults): void { + $this->totalResults = $totalResults; + } + + /** + * Returns the list of warnings. + * + * @return string[] + * An array of strings representing warnings. + */ + public function getWarnings(): array { + return $this->warnings; + } + + /** + * Sets the warning list. + * + * @param string[] $warnings + * An array of strings representing warnings. + */ + public function setWarnings(array $warnings): void { + $this->warnings = $warnings; + } + +} diff --git a/modules/oe_search_enterprise_search/src/Plugin/search_api/backend/EnterpriseSearchBackend.php b/modules/oe_search_enterprise_search/src/Plugin/search_api/backend/EnterpriseSearchBackend.php index fdcf897..3af3e25 100644 --- a/modules/oe_search_enterprise_search/src/Plugin/search_api/backend/EnterpriseSearchBackend.php +++ b/modules/oe_search_enterprise_search/src/Plugin/search_api/backend/EnterpriseSearchBackend.php @@ -18,6 +18,7 @@ use OpenEuropa\EnterpriseSearchClient\Api\SearchApi; use OpenEuropa\EnterpriseSearchClient\Client; use OpenEuropa\EnterpriseSearchClient\ClientInterface; +use OpenEuropa\EnterpriseSearchClient\Model\Document; /** * European Commission Enterprise Search backend for search_api. @@ -127,9 +128,15 @@ public function deleteItems(IndexInterface $index, array $item_ids) { * {@inheritDoc} */ public function deleteAllIndexItems(IndexInterface $index, $datasource_id = NULL) { - \Drupal::messenger()->addWarning($this->t('Mass deletion is not supported yet in %backend backends.', [ - '%backend' => $this->label(), - ])); + $client = $this->getClient(); + $api = new SearchApi($client); + $search = $api->search(); + + $item_ids = array_map(function (Document $document) { + return $document->getReference(); + }, $search->getResults()); + + $this->deleteItems($index, $item_ids); } /** @@ -138,9 +145,12 @@ public function deleteAllIndexItems(IndexInterface $index, $datasource_id = NULL public function search(QueryInterface $query) { $client = $this->getClient(); $api = new SearchApi($client); - $api->search(); + $search = $api->search(); + + $results = $query->getResults(); + $results->setResultCount($search->getTotalResults()); - \Drupal::messenger()->addWarning($this->t('Search is not supported yet in %backend backends.', [ + \Drupal::messenger()->addWarning($this->t('Search is not fully supported yet in %backend backends.', [ '%backend' => $this->label(), ])); }