From 9798dd38f24459f3f5e32c508fa381bc2843e6df Mon Sep 17 00:00:00 2001 From: martinlagler Date: Tue, 12 Mar 2024 15:45:46 +0100 Subject: [PATCH] Add route_path as searchfield (#665) --- Controller/ArticleController.php | 17 +++++++++++--- ListBuilder/ElasticSearchFieldDescriptor.php | 14 +++++++++++- .../ElasticSearchFieldDescriptorBuilder.php | 15 ++++++++++++- Resources/config/lists/articles.xml | 5 +++++ Resources/translations/admin.de.json | 1 + Resources/translations/admin.en.json | 1 + .../Controller/ArticleControllerTest.php | 22 +++++++++++++++++++ 7 files changed, 70 insertions(+), 5 deletions(-) diff --git a/Controller/ArticleController.php b/Controller/ArticleController.php index 3516a17c4..eda130365 100644 --- a/Controller/ArticleController.php +++ b/Controller/ArticleController.php @@ -41,6 +41,7 @@ use Sulu\Component\Rest\AbstractRestController; use Sulu\Component\Rest\Exception\MissingParameterException; use Sulu\Component\Rest\Exception\RestException; +use Sulu\Component\Rest\ListBuilder\FieldDescriptor; use Sulu\Component\Rest\ListBuilder\FieldDescriptorInterface; use Sulu\Component\Rest\ListBuilder\ListRepresentation; use Sulu\Component\Rest\ListBuilder\ListRestHelperInterface; @@ -173,6 +174,8 @@ protected function getFieldDescriptors(): array ->build(), 'title' => ElasticSearchFieldDescriptor::create('title', 'public.title') ->setSortField('title.raw') + ->setSearchField('title') + ->setSearchability(FieldDescriptor::SEARCHABILITY_YES) ->build(), 'creatorFullName' => ElasticSearchFieldDescriptor::create('creatorFullName', 'sulu_article.list.creator') ->setSortField('creatorFullName.raw') @@ -208,6 +211,8 @@ protected function getFieldDescriptors(): array ->build(), 'routePath' => ElasticSearchFieldDescriptor::create('routePath') ->setVisibility(FieldDescriptorInterface::VISIBILITY_NO) + ->setSearchField('route_path.value') + ->setSearchability(FieldDescriptor::SEARCHABILITY_YES) ->build(), ]; } @@ -248,9 +253,17 @@ public function cgetAction(Request $request): Response $limit = \count($ids); } + $fieldDescriptors = $this->getFieldDescriptors(); + $searchFields = $this->restHelper->getSearchFields(); if (0 === \count($searchFields)) { - $searchFields = ['title']; + foreach ($fieldDescriptors as $fieldDescriptor) { + if (FieldDescriptorInterface::SEARCHABILITY_YES !== $fieldDescriptor->getSearchability()) { + continue; + } + + $searchFields[] = $fieldDescriptor->getSearchField(); + } } $searchPattern = $this->restHelper->getSearchPattern(); @@ -332,8 +345,6 @@ public function cgetAction(Request $request): Response ); } - $fieldDescriptors = $this->getFieldDescriptors(); - if ($limit) { $search->setSize($limit); $search->setFrom(($page - 1) * $limit); diff --git a/ListBuilder/ElasticSearchFieldDescriptor.php b/ListBuilder/ElasticSearchFieldDescriptor.php index 92dcfae27..33b5b4be6 100644 --- a/ListBuilder/ElasticSearchFieldDescriptor.php +++ b/ListBuilder/ElasticSearchFieldDescriptor.php @@ -30,6 +30,11 @@ public static function create(string $name, string $translation = null): Elastic */ private $sortField; + /** + * @var string + */ + private $searchField; + public function __construct( string $name, string $sortField = null, @@ -37,9 +42,11 @@ public function __construct( string $visibility = FieldDescriptorInterface::VISIBILITY_YES, string $searchability = FieldDescriptorInterface::SEARCHABILITY_NEVER, string $type = '', - bool $sortable = true + bool $sortable = true, + string $searchField = '' ) { $this->sortField = $sortField ? $sortField : $name; + $this->searchField = $searchField; parent::__construct( $name, @@ -55,4 +62,9 @@ public function getSortField(): string { return $this->sortField; } + + public function getSearchField(): string + { + return $this->searchField; + } } diff --git a/ListBuilder/ElasticSearchFieldDescriptorBuilder.php b/ListBuilder/ElasticSearchFieldDescriptorBuilder.php index eff328d36..33b4dbfc8 100644 --- a/ListBuilder/ElasticSearchFieldDescriptorBuilder.php +++ b/ListBuilder/ElasticSearchFieldDescriptorBuilder.php @@ -50,6 +50,11 @@ final class ElasticSearchFieldDescriptorBuilder */ private $sortable = false; + /** + * @var string + */ + private $searchField = ''; + public function __construct(string $name, string $translation = null) { $this->name = $name; @@ -85,6 +90,13 @@ public function setType(string $type) return $this; } + public function setSearchField(string $searchField): self + { + $this->searchField = $searchField; + + return $this; + } + public function build(): ElasticSearchFieldDescriptor { return new ElasticSearchFieldDescriptor( @@ -94,7 +106,8 @@ public function build(): ElasticSearchFieldDescriptor $this->visibility, $this->searchability, $this->type, - $this->sortable + $this->sortable, + $this->searchField, ); } } diff --git a/Resources/config/lists/articles.xml b/Resources/config/lists/articles.xml index ce8fd1288..8ff02ef04 100644 --- a/Resources/config/lists/articles.xml +++ b/Resources/config/lists/articles.xml @@ -111,5 +111,10 @@ + diff --git a/Resources/translations/admin.de.json b/Resources/translations/admin.de.json index fc756ab3b..932a5e724 100644 --- a/Resources/translations/admin.de.json +++ b/Resources/translations/admin.de.json @@ -14,6 +14,7 @@ "sulu_article.published_state": "Status der Veröffentlichung", "sulu_article.published": "Veröffentlicht", "sulu_article.not_published": "Nicht Veröffentlicht", + "sulu_article.route": "Route", "sulu_activity.resource.articles": "Artikel", "sulu_activity.resource.articles.translation": "Übersetzung", "sulu_activity.description.articles.created": "{userFullName} hat den Artikel \"{resourceTitle}\" erstellt", diff --git a/Resources/translations/admin.en.json b/Resources/translations/admin.en.json index 5fb8e0d0b..560ffa387 100644 --- a/Resources/translations/admin.en.json +++ b/Resources/translations/admin.en.json @@ -14,6 +14,7 @@ "sulu_article.published_state": "Published State", "sulu_article.published": "Published", "sulu_article.not_published": "Not Published", + "sulu_article.route": "Route", "sulu_activity.resource.articles": "Article", "sulu_activity.resource.articles.translation": "Translation", "sulu_activity.description.articles.created": "{userFullName} has created the article \"{resourceTitle}\"", diff --git a/Tests/Functional/Controller/ArticleControllerTest.php b/Tests/Functional/Controller/ArticleControllerTest.php index 3e8a2946c..388c24184 100644 --- a/Tests/Functional/Controller/ArticleControllerTest.php +++ b/Tests/Functional/Controller/ArticleControllerTest.php @@ -880,6 +880,28 @@ public function testCGetSearchRoutePath() $this->assertEquals($articles[1]['title'], $response['_embedded']['articles'][1]['title']); } + public function testCGetSearchFieldRoutePath() + { + $routePathData = [ + 'suffix' => '/test-route', + ]; + $article = $this->postPageTreeRoute($routePathData, 'Article Name'); + + $this->client->jsonRequest( + 'GET', + '/api/articles?locale=de&page=1&limit=10&fields=id,title,creatorFullName,changerFullName,authorFullName,routePath&search=test-route&flat=true' + ); + + $this->assertHttpStatusCode(200, $this->client->getResponse()); + + $response = \json_decode($this->client->getResponse()->getContent(), true); + + $this->assertEquals(1, $response['total']); + $this->assertCount(1, $response['_embedded']['articles']); + $this->assertEquals($article['id'], $response['_embedded']['articles'][0]['id']); + $this->assertEquals($article['title'], $response['_embedded']['articles'][0]['title']); + } + public function testCGetSearchCaseInsensitive() { $this->testPost('Sulu');