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.

---------

Co-authored-by: adriendupuis <[email protected]>
Co-authored-by: Marek Nocoń <[email protected]>
  • Loading branch information
3 people authored Feb 11, 2025
1 parent eb96ce9 commit c765208
Show file tree
Hide file tree
Showing 39 changed files with 333 additions and 354 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
{
$query = $event->getQuery();

$additionalCriteria = new ObjectStateIdentifier('locked');

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

public static function getSubscribedEvents(): array
{
return [
QueryFilterEvent::class => 'onQueryFilter',
];
}
}
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
{
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\CriterionInterface;
use Ibexa\Contracts\Elasticsearch\Query\CriterionVisitor;
use Ibexa\Contracts\Elasticsearch\Query\LanguageFilter;
Expand All @@ -16,7 +17,7 @@ public function supports(CriterionInterface $criterion, LanguageFilter $language
}

/**
* @param \App\Query\Criterion\Elasticsearch\CameraManufacturerCriterion $criterion
* @param \App\Query\Criterion\CameraManufacturerCriterion $criterion
*/
public function visit(CriterionVisitor $dispatcher, CriterionInterface $criterion, LanguageFilter $languageFilter): array
{
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\CriterionInterface;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;

Expand All @@ -15,7 +16,7 @@ public function canVisit(CriterionInterface $criterion)
}

/**
* @param \App\Query\Criterion\Solr\CameraManufacturerCriterion $criterion
* @param \App\Query\Criterion\CameraManufacturerCriterion $criterion
*/
public function visit(CriterionInterface $criterion, CriterionVisitor $subVisitor = null)
{
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.

This file was deleted.

Loading

0 comments on commit c765208

Please sign in to comment.