-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement TagId and TagKeyword visitors for elasticsearch #157
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0f4859d
FINAWEB-53 add visitors
petarjakopec 361e17f
FINAWEB-53 add custom prefix query
petarjakopec 5fd092d
FINAWEB-53 add configuration
petarjakopec cef539d
FINAWEB-53 remove unecessary doc
petarjakopec f4b8cce
FINAWEB-53 use constructor property promotion
petarjakopec e311175
FINAWEB-53 add ignore for phpstan errors
petarjakopec 2d2e4ed
FINAWEB-53 fix phpstan regular expressions
petarjakopec ebb0e60
FINAWEB-53 hint criterion value as string for phpstan
petarjakopec a238f85
FINAWEB-53 add exclude paths to pass phpstan
petarjakopec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
bundle/Core/Search/Elasticsearch/Query/Common/CriterionVisitor/Tags.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\TagsBundle\Core\Search\Elasticsearch\Query\Common\CriterionVisitor; | ||
|
||
use Ibexa\Contracts\Core\Persistence\Content\Type\Handler; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
use Ibexa\Contracts\Elasticsearch\Query\CriterionVisitor; | ||
use Ibexa\Core\Search\Common\FieldNameResolver; | ||
|
||
use function array_merge; | ||
|
||
abstract class Tags implements CriterionVisitor | ||
{ | ||
public function __construct( | ||
private readonly FieldNameResolver $fieldNameResolver, | ||
private readonly Handler $contentTypeHandler, | ||
private readonly string $fieldTypeIdentifier, | ||
private readonly string $fieldName, | ||
) { | ||
} | ||
|
||
protected function getSearchFields(Criterion $criterion): array | ||
{ | ||
if ($criterion->target !== null) { | ||
return $this->fieldNameResolver->getFieldTypes( | ||
$criterion, | ||
$criterion->target, | ||
$this->fieldTypeIdentifier, | ||
$this->fieldName, | ||
); | ||
} | ||
|
||
$targetFieldTypes = []; | ||
foreach ($this->contentTypeHandler->getSearchableFieldMap() as $fieldDefinitions) { | ||
foreach ($fieldDefinitions as $fieldIdentifier => $fieldDefinition) { | ||
if (!isset($fieldDefinition['field_type_identifier'])) { | ||
continue; | ||
} | ||
|
||
if ($fieldDefinition['field_type_identifier'] !== $this->fieldTypeIdentifier) { | ||
continue; | ||
} | ||
|
||
$fieldTypes = $this->fieldNameResolver->getFieldTypes( | ||
$criterion, | ||
$fieldIdentifier, | ||
$this->fieldTypeIdentifier, | ||
$this->fieldName, | ||
); | ||
|
||
$targetFieldTypes[] = $fieldTypes; | ||
} | ||
} | ||
|
||
return array_merge(...$targetFieldTypes); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
bundle/Core/Search/Elasticsearch/Query/Common/CriterionVisitor/Tags/TagId.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\TagsBundle\Core\Search\Elasticsearch\Query\Common\CriterionVisitor\Tags; | ||
|
||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
use Ibexa\Contracts\Elasticsearch\Query\CriterionVisitor; | ||
use Ibexa\Contracts\Elasticsearch\Query\LanguageFilter; | ||
use Ibexa\Core\Base\Exceptions\InvalidArgumentException; | ||
use Ibexa\Elasticsearch\ElasticSearch\QueryDSL\BoolQuery; | ||
use Ibexa\Elasticsearch\ElasticSearch\QueryDSL\TermQuery; | ||
use Netgen\TagsBundle\API\Repository\Values\Content\Query\Criterion\TagId as APITagId; | ||
use Netgen\TagsBundle\Core\Search\Elasticsearch\Query\Common\CriterionVisitor\Tags; | ||
|
||
use function count; | ||
|
||
final class TagId extends Tags | ||
{ | ||
public function supports(Criterion $criterion, LanguageFilter $languageFilter): bool | ||
{ | ||
return $criterion instanceof APITagId; | ||
} | ||
|
||
public function visit(CriterionVisitor $dispatcher, Criterion $criterion, LanguageFilter $languageFilter): array | ||
{ | ||
$criterion->value = (array) $criterion->value; | ||
$searchFields = $this->getSearchFields($criterion); | ||
|
||
if (count($searchFields) === 0) { | ||
throw new InvalidArgumentException( | ||
'$criterion->target', | ||
"No searchable fields found for the given criterion target '{$criterion->target}'.", | ||
); | ||
} | ||
|
||
$query = new BoolQuery(); | ||
foreach ($searchFields as $name => $fieldType) { | ||
foreach ($criterion->value as $value) { | ||
$query->addShould(new TermQuery($name, $value)); | ||
} | ||
} | ||
|
||
return $query->toArray(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
bundle/Core/Search/Elasticsearch/Query/Common/CriterionVisitor/Tags/TagKeyword.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\TagsBundle\Core\Search\Elasticsearch\Query\Common\CriterionVisitor\Tags; | ||
|
||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator; | ||
use Ibexa\Contracts\Elasticsearch\Query\CriterionVisitor; | ||
use Ibexa\Contracts\Elasticsearch\Query\LanguageFilter; | ||
use Ibexa\Core\Base\Exceptions\InvalidArgumentException; | ||
use Ibexa\Elasticsearch\ElasticSearch\QueryDSL\BoolQuery; | ||
use Ibexa\Elasticsearch\ElasticSearch\QueryDSL\TermQuery; | ||
use Netgen\TagsBundle\API\Repository\Values\Content\Query\Criterion\TagKeyword as APITagKeyword; | ||
use Netgen\TagsBundle\Core\Search\Elasticsearch\Query\Common\CriterionVisitor\Tags; | ||
use Netgen\TagsBundle\Core\Search\Elasticsearch\QueryDSL\PrefixQuery; | ||
|
||
use function count; | ||
|
||
final class TagKeyword extends Tags | ||
{ | ||
public function supports(Criterion $criterion, LanguageFilter $languageFilter): bool | ||
{ | ||
return $criterion instanceof APITagKeyword; | ||
} | ||
|
||
public function visit(CriterionVisitor $dispatcher, Criterion $criterion, LanguageFilter $languageFilter): array | ||
{ | ||
$criterion->value = (array) $criterion->value; | ||
$searchFields = $this->getSearchFields($criterion); | ||
$isLikeOperator = $criterion->operator === Operator::LIKE; | ||
|
||
if (count($searchFields) === 0) { | ||
throw new InvalidArgumentException( | ||
'$criterion->target', | ||
"No searchable fields found for the given criterion target '{$criterion->target}'.", | ||
); | ||
} | ||
|
||
$query = new BoolQuery(); | ||
foreach ($searchFields as $name => $fieldType) { | ||
/** | ||
* @var string $value | ||
*/ | ||
foreach ($criterion->value as $value) { | ||
if ($isLikeOperator) { | ||
$query->addShould(new PrefixQuery($name, $value)); | ||
} else { | ||
$query->addShould(new TermQuery($name, $value)); | ||
} | ||
} | ||
} | ||
|
||
return $query->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\TagsBundle\Core\Search\Elasticsearch\QueryDSL; | ||
|
||
use Ibexa\Elasticsearch\ElasticSearch\QueryDSL\Query; | ||
|
||
final class PrefixQuery implements Query | ||
{ | ||
public function __construct( | ||
private ?string $field = null, | ||
private ?string $value = null, | ||
) { | ||
} | ||
|
||
public function withField(string $field): self | ||
{ | ||
$this->field = $field; | ||
|
||
return $this; | ||
} | ||
|
||
public function withValue(string $value): self | ||
{ | ||
$this->value = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'prefix' => [ | ||
$this->field => $this->value, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
services: | ||
netgen_tags.search.elasticsearch.query.common.criterion_visitor.tag_id: | ||
class: Netgen\TagsBundle\Core\Search\Elasticsearch\Query\Common\CriterionVisitor\Tags\TagId | ||
arguments: | ||
- "@Ibexa\\Core\\Search\\Common\\FieldNameResolver" | ||
- "@Ibexa\\Contracts\\Core\\Persistence\\Content\\Type\\Handler" | ||
- "eztags" | ||
- "tag_ids" | ||
tags: | ||
- { name: ibexa.search.elasticsearch.query.content.criterion.visitor } | ||
- { name: ibexa.search.elasticsearch.query.location.criterion.visitor } | ||
|
||
netgen_tags.search.elasticsearch.query.common.criterion_visitor.tag_keyword: | ||
class: Netgen\TagsBundle\Core\Search\Elasticsearch\Query\Common\CriterionVisitor\Tags\TagKeyword | ||
arguments: | ||
- "@Ibexa\\Core\\Search\\Common\\FieldNameResolver" | ||
- "@Ibexa\\Contracts\\Core\\Persistence\\Content\\Type\\Handler" | ||
- "eztags" | ||
- "tag_keywords" | ||
tags: | ||
- { name: ibexa.search.elasticsearch.query.content.criterion.visitor } | ||
- { name: ibexa.search.elasticsearch.query.location.criterion.visitor } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we cannot obviously add Ibexa Elastic Search to requirements to silence PHPStan here, we need to ignore these errors in
phpstan.neon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just ignore every error containing the string
Ibexa\Contracts\Elasticsearch
orIbexa\Elasticsearch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in a238f85, 2d2e4ed and ebb0e60