Skip to content

Commit

Permalink
Add route_path as searchfield (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlagler authored Mar 12, 2024
1 parent b0dfa50 commit 9798dd3
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 5 deletions.
17 changes: 14 additions & 3 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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(),
];
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -332,8 +345,6 @@ public function cgetAction(Request $request): Response
);
}

$fieldDescriptors = $this->getFieldDescriptors();

if ($limit) {
$search->setSize($limit);
$search->setFrom(($page - 1) * $limit);
Expand Down
14 changes: 13 additions & 1 deletion ListBuilder/ElasticSearchFieldDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,23 @@ public static function create(string $name, string $translation = null): Elastic
*/
private $sortField;

/**
* @var string
*/
private $searchField;

public function __construct(
string $name,
string $sortField = null,
string $translation = null,
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,
Expand All @@ -55,4 +62,9 @@ public function getSortField(): string
{
return $this->sortField;
}

public function getSearchField(): string
{
return $this->searchField;
}
}
15 changes: 14 additions & 1 deletion ListBuilder/ElasticSearchFieldDescriptorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -94,7 +106,8 @@ public function build(): ElasticSearchFieldDescriptor
$this->visibility,
$this->searchability,
$this->type,
$this->sortable
$this->sortable,
$this->searchField,
);
}
}
5 changes: 5 additions & 0 deletions Resources/config/lists/articles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,10 @@
</params>
</filter>
</property>
<property
name="routePath"
visibility="no"
translation="sulu_article.route"
/>
</properties>
</list>
1 change: 1 addition & 0 deletions Resources/translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions Resources/translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}\"",
Expand Down
22 changes: 22 additions & 0 deletions Tests/Functional/Controller/ArticleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 9798dd3

Please sign in to comment.