Skip to content

Commit

Permalink
Enhance search customization (#2591)
Browse files Browse the repository at this point in the history
* Merge ElasticSearch and Solr custom criterion, sort clause and aggregation.
Criteria, sort clauses and aggregations don't depend on the search engine, only their visitors and other handlers do.

(cherry picked from commit c765208)

---------

Co-authored-by: adriendupuis <[email protected]>
Co-authored-by: Marek Nocoń <[email protected]>
  • Loading branch information
3 people committed Feb 11, 2025
1 parent 49d1f0d commit e06b15a
Show file tree
Hide file tree
Showing 39 changed files with 337 additions and 352 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,19 @@ jobs:
- name: Convert code_samples usages for comment
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
run: |
echo '# code_samples/ change report' >> code_samples_usage.diff.md
title='# code_samples/ change report'
link='<a href="${{ steps.artifact.outputs.artifact-url }}">Download colorized diff</a>'
echo "$title" > code_samples_usage.diff.md
echo '' >> code_samples_usage.diff.md
php tools/code_samples/code_samples_usage_diff2html.php $HOME/code_samples_usage.diff >> code_samples_usage.diff.md
echo '<a href="${{ steps.artifact.outputs.artifact-url }}">Download colorized diff</a>' >> code_samples_usage.diff.md
echo "$link" >> code_samples_usage.diff.md
if [[ `wc -m < code_samples_usage.diff.md | xargs` -ge $((2**16)) ]]; then
echo "$title" > code_samples_usage.diff.md
echo '' >> code_samples_usage.diff.md
echo "Report's diff is too long to be displayed in a comment." >> code_samples_usage.diff.md
echo '' >> code_samples_usage.diff.md
echo "$link" >> code_samples_usage.diff.md
fi
- name: Find Comment
id: find-comment
uses: peter-evans/find-comment@v3
Expand Down
49 changes: 49 additions & 0 deletions code_samples/search/custom/config/aggregation_services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
services:
app.search.solr.query.aggregation_visitor.priority_range_aggregation:
class: Ibexa\Solr\Query\Common\AggregationVisitor\RangeAggregationVisitor
factory: [ '@Ibexa\Solr\Query\Common\AggregationVisitor\Factory\SearchFieldAggregationVisitorFactory', 'createRangeAggregationVisitor' ]
arguments:
$aggregationClass: 'App\Query\Aggregation\Solr\PriorityRangeAggregation'
$searchIndexFieldName: 'priority_i'
tags:
- { name: ibexa.search.solr.query.location.aggregation.visitor }

app.search.elasticsearch.query.aggregation_visitor.priority_range_aggregation:
class: Ibexa\Elasticsearch\Query\AggregationVisitor\RangeAggregationVisitor
factory: [ '@Ibexa\Elasticsearch\Query\AggregationVisitor\Factory\SearchFieldAggregationVisitorFactory', 'createRangeAggregationVisitor' ]
arguments:
$aggregationClass: 'App\Query\Aggregation\Elasticsearch\PriorityRangeAggregation'
$searchIndexFieldName: 'priority_i'
tags:
- { name: ibexa.search.elasticsearch.query.location.aggregation.visitor }

app.search.solr.query.aggregation_result_extractor.priority_range_aggregation:
class: Ibexa\Solr\ResultExtractor\AggregationResultExtractor\RangeAggregationResultExtractor
arguments:
$aggregationClass: 'App\Query\Aggregation\Solr\PriorityRangeAggregation'
$keyMapper: 'Ibexa\Solr\ResultExtractor\AggregationResultExtractor\RangeAggregationKeyMapper\IntRangeAggregationKeyMapper'
tags:
- { name: ibexa.search.solr.query.location.aggregation.result.extractor }

app.search.elasticsearch.query.aggregation_result_extractor.priority_range_aggregation:
class: Ibexa\Elasticsearch\Query\ResultExtractor\AggregationResultExtractor\RangeAggregationResultExtractor
arguments:
$aggregationClass: 'App\Query\Aggregation\Elasticsearch\PriorityRangeAggregation'
tags:
- { name: ibexa.search.elasticsearch.query.location.aggregation.result.extractor }

App\Query\Aggregation\Solr\PriorityRangeAggregationVisitor:
tags:
- { name: ibexa.search.solr.query.location.aggregation.visitor }

App\Query\Aggregation\Solr\PriorityRangeAggregationResultExtractor:
tags:
- { name: ibexa.search.solr.query.location.aggregation.result.extractor }

App\Query\Aggregation\Elasticsearch\PriorityRangeAggregationVisitor:
tags:
- { name: ibexa.search.elasticsearch.query.location.aggregation.visitor }

App\Query\Aggregation\Elasticsearch\PriorityRangeAggregationResultExtractor:
tags:
- { name: ibexa.search.elasticsearch.query.location.aggregation.result.extractor }
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
services:
App\Query\Criterion\Solr\CameraManufacturerVisitor:
tags:
- { name: ibexa.search.solr.query.content.criterion.visitor }
- { name: ibexa.search.solr.query.location.criterion.visitor }

App\Query\Criterion\Elasticsearch\CameraManufacturerVisitor:
tags:
- { name: ibexa.search.elasticsearch.query.content.criterion.visitor }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
services:
App\Query\SortClause\Solr\ScoreVisitor:
tags:
- { name: ibexa.search.solr.query.content.sort_clause.visitor }
- { name: ibexa.search.solr.query.location.sort_clause.visitor }

App\Query\SortClause\Elasticsearch\ScoreVisitor:
tags:
- { name: ibexa.search.elasticsearch.query.content.sort_clause.visitor }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace App\EventSubscriber;

use Ibexa\Contracts\Core\Search\Field;
use Ibexa\Contracts\Core\Search\FieldType\StringField;
use Ibexa\Contracts\Elasticsearch\Mapping\Event\ContentIndexCreateEvent;
use Ibexa\Contracts\Elasticsearch\Mapping\Event\LocationIndexCreateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class CustomIndexDataSubscriber implements EventSubscriberInterface
{
public function onContentDocumentCreate(ContentIndexCreateEvent $event): void
{
$document = $event->getDocument();
$document->fields[] = new Field(
'custom_field',
'Custom field value',
new StringField()
);
}

public function onLocationDocumentCreate(LocationIndexCreateEvent $event): void
{
$document = $event->getDocument();
$document->fields[] = new Field(
'custom_field',
'Custom field value',
new StringField()
);
}

public static function getSubscribedEvents(): array
{
return [
ContentIndexCreateEvent::class => 'onContentDocumentCreate',
LocationIndexCreateEvent::class => 'onLocationDocumentCreate',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\EventSubscriber;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalAnd;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ObjectStateIdentifier;
use Ibexa\Contracts\ElasticSearch\Query\Event\QueryFilterEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class CustomQueryFilterSubscriber implements EventSubscriberInterface
{
public function onQueryFilter(QueryFilterEvent $event): void

Check failure on line 14 in code_samples/search/custom/src/EventSubscriber/CustomQueryFilterSubscriber.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Parameter $event of method App\EventSubscriber\CustomQueryFilterSubscriber::onQueryFilter() has invalid type Ibexa\Contracts\ElasticSearch\Query\Event\QueryFilterEvent.

Check failure on line 14 in code_samples/search/custom/src/EventSubscriber/CustomQueryFilterSubscriber.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Parameter $event of method App\EventSubscriber\CustomQueryFilterSubscriber::onQueryFilter() has invalid type Ibexa\Contracts\ElasticSearch\Query\Event\QueryFilterEvent.
{
$query = $event->getQuery();

Check failure on line 16 in code_samples/search/custom/src/EventSubscriber/CustomQueryFilterSubscriber.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Call to method getQuery() on an unknown class Ibexa\Contracts\ElasticSearch\Query\Event\QueryFilterEvent.

Check failure on line 16 in code_samples/search/custom/src/EventSubscriber/CustomQueryFilterSubscriber.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Call to method getQuery() on an unknown class Ibexa\Contracts\ElasticSearch\Query\Event\QueryFilterEvent.

$additionalCriteria = new ObjectStateIdentifier('locked');

if ($query->filter !== null) {
$query->filter = $additionalCriteria;
} else {
// Append Criterion to existing filter
$query->filter = new LogicalAnd([

Check failure on line 24 in code_samples/search/custom/src/EventSubscriber/CustomQueryFilterSubscriber.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Parameter #1 $criteria of class Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalAnd constructor expects array<Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion>, array<int, Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ObjectStateIdentifier|null> given.

Check failure on line 24 in code_samples/search/custom/src/EventSubscriber/CustomQueryFilterSubscriber.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Parameter #1 $criteria of class Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalAnd constructor expects array<Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion>, array<int, Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ObjectStateIdentifier|null> given.
$query->filter,
$additionalCriteria,
]);
}
}

public static function getSubscribedEvents(): array
{
return [
QueryFilterEvent::class => 'onQueryFilter',

Check failure on line 34 in code_samples/search/custom/src/EventSubscriber/CustomQueryFilterSubscriber.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Class Ibexa\Contracts\ElasticSearch\Query\Event\QueryFilterEvent not found.

Check failure on line 34 in code_samples/search/custom/src/EventSubscriber/CustomQueryFilterSubscriber.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Class Ibexa\Contracts\ElasticSearch\Query\Event\QueryFilterEvent not found.
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace App\Query\Aggregation\Elasticsearch;

use App\Query\Aggregation\PriorityRangeAggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\AggregationResult;
use Ibexa\Contracts\Elasticsearch\Query\AggregationResultExtractor;
use Ibexa\Contracts\Elasticsearch\Query\LanguageFilter;

final class PriorityAggregationResultExtractor implements AggregationResultExtractor
final class PriorityRangeAggregationResultExtractor implements AggregationResultExtractor
{
public function supports(Aggregation $aggregation, LanguageFilter $languageFilter): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

namespace App\Query\Aggregation\Elasticsearch;

use App\Query\Aggregation\PriorityRangeAggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation;
use Ibexa\Contracts\Elasticsearch\Query\AggregationVisitor;
use Ibexa\Contracts\Elasticsearch\Query\LanguageFilter;

final class PriorityAggregationVisitor implements AggregationVisitor
final class PriorityRangeAggregationVisitor implements AggregationVisitor
{
public function supports(Aggregation $aggregation, LanguageFilter $languageFilter): bool
{
return $aggregation instanceof PriorityRangeAggregation;
}

/**
* @param PriorityRangeAggregation $aggregation
* @param \App\Query\Aggregation\PriorityRangeAggregation $aggregation
*/
public function visit(AggregationVisitor $dispatcher, Aggregation $aggregation, LanguageFilter $languageFilter): array

Check failure on line 22 in code_samples/search/custom/src/Query/Aggregation/Elasticsearch/PriorityRangeAggregationVisitor.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Method App\Query\Aggregation\Elasticsearch\PriorityRangeAggregationVisitor::visit() return type has no value type specified in iterable type array.

Check failure on line 22 in code_samples/search/custom/src/Query/Aggregation/Elasticsearch/PriorityRangeAggregationVisitor.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Method App\Query\Aggregation\Elasticsearch\PriorityRangeAggregationVisitor::visit() return type has no value type specified in iterable type array.
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Query\Aggregation\Solr;
namespace App\Query\Aggregation;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\AbstractRangeAggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\LocationAggregation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Query\Aggregation\Solr;

use App\Query\Aggregation\PriorityRangeAggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Range;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\AggregationResult;
Expand All @@ -12,7 +13,7 @@
use Ibexa\Contracts\Solr\ResultExtractor\AggregationResultExtractor;
use stdClass;

final class PriorityAggregationResultExtractor implements AggregationResultExtractor
final class PriorityRangeAggregationResultExtractor implements AggregationResultExtractor
{
public function canVisit(Aggregation $aggregation, array $languageFilter): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Query\Aggregation\Solr;

use App\Query\Aggregation\PriorityRangeAggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation;
use Ibexa\Contracts\Solr\Query\AggregationVisitor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Query\Criterion\Solr;
namespace App\Query\Criterion;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Query\Criterion\Elasticsearch;

use App\Query\Criterion\CameraManufacturerCriterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Elasticsearch\Query\CriterionVisitor;
use Ibexa\Contracts\Elasticsearch\Query\LanguageFilter;
Expand All @@ -15,6 +16,9 @@ public function supports(Criterion $criterion, LanguageFilter $languageFilter):
return $criterion instanceof CameraManufacturerCriterion;
}

/**
* @param \App\Query\Criterion\Elasticsearch\CameraManufacturerCriterion $criterion
*/
public function visit(CriterionVisitor $dispatcher, Criterion $criterion, LanguageFilter $languageFilter): array

Check failure on line 22 in code_samples/search/custom/src/Query/Criterion/Elasticsearch/CameraManufacturerVisitor.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

PHPDoc tag @param for parameter $criterion with type App\Query\Criterion\Elasticsearch\CameraManufacturerCriterion is not subtype of native type Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion.

Check failure on line 22 in code_samples/search/custom/src/Query/Criterion/Elasticsearch/CameraManufacturerVisitor.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Parameter $criterion of method App\Query\Criterion\Elasticsearch\CameraManufacturerVisitor::visit() has invalid type App\Query\Criterion\Elasticsearch\CameraManufacturerCriterion.

Check failure on line 22 in code_samples/search/custom/src/Query/Criterion/Elasticsearch/CameraManufacturerVisitor.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

PHPDoc tag @param for parameter $criterion with type App\Query\Criterion\Elasticsearch\CameraManufacturerCriterion is not subtype of native type Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion.

Check failure on line 22 in code_samples/search/custom/src/Query/Criterion/Elasticsearch/CameraManufacturerVisitor.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Parameter $criterion of method App\Query\Criterion\Elasticsearch\CameraManufacturerVisitor::visit() has invalid type App\Query\Criterion\Elasticsearch\CameraManufacturerCriterion.
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Query\Criterion\Solr;

use App\Query\Criterion\CameraManufacturerCriterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;

Expand All @@ -14,6 +15,9 @@ public function canVisit(Criterion $criterion)
return $criterion instanceof CameraManufacturerCriterion;
}

/**
* @param \App\Query\Criterion\Solr\CameraManufacturerCriterion $criterion
*/
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null)

Check failure on line 21 in code_samples/search/custom/src/Query/Criterion/Solr/CameraManufacturerVisitor.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

PHPDoc tag @param for parameter $criterion with type App\Query\Criterion\Solr\CameraManufacturerCriterion is not subtype of native type Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion.

Check failure on line 21 in code_samples/search/custom/src/Query/Criterion/Solr/CameraManufacturerVisitor.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Parameter $criterion of method App\Query\Criterion\Solr\CameraManufacturerVisitor::visit() has invalid type App\Query\Criterion\Solr\CameraManufacturerCriterion.

Check failure on line 21 in code_samples/search/custom/src/Query/Criterion/Solr/CameraManufacturerVisitor.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

PHPDoc tag @param for parameter $criterion with type App\Query\Criterion\Solr\CameraManufacturerCriterion is not subtype of native type Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion.

Check failure on line 21 in code_samples/search/custom/src/Query/Criterion/Solr/CameraManufacturerVisitor.php

View workflow job for this annotation

GitHub Actions / Validate code samples (8.3)

Parameter $criterion of method App\Query\Criterion\Solr\CameraManufacturerVisitor::visit() has invalid type App\Query\Criterion\Solr\CameraManufacturerCriterion.
{
$expressions = array_map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Query\SortClause\Elasticsearch;

use App\Query\SortClause\ScoreSortClause;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;
use Ibexa\Contracts\Elasticsearch\Query\LanguageFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Query\SortClause\Solr;
namespace App\Query\SortClause;

use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace App\Query\SortClause\Solr;

use App\Query\SortClause\ScoreSortClause;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;
use Ibexa\Contracts\Solr\Query\SortClauseVisitor;

class ScoreVisitor extends SortClauseVisitor
{
public function canVisit(SortClause $sortClause): bool
{
return $sortClause instanceof SortClause\Score;
return $sortClause instanceof ScoreSortClause;
}

public function visit(SortClause $sortClause): string
Expand Down
10 changes: 0 additions & 10 deletions code_samples/search/elasticsearch/config/aggregation_services.yaml

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit e06b15a

Please sign in to comment.