Skip to content
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

Indexing & Querying #3

Merged
merged 17 commits into from
Mar 6, 2024
Merged

Indexing & Querying #3

merged 17 commits into from
Mar 6, 2024

Conversation

fquffio
Copy link
Member

@fquffio fquffio commented Sep 14, 2023

This PR implements indexing and basic querying.

The cakephp/elasticsearch plugin has been used. Two basic indexes are included (one for generic entities, the other specific for BEdita Objects), but it is possible to extend the base functionality by implementing your own Index class both for customising the indexed document and for building your own search query.

Example

class MyAppIndex extends ObjectSearchIndex
{
    protected function getDefaultName(): string
    {
        return 'my_app'; // Name of ElasticSearch index (by default it uses SQL database/schema name).
    }

    protected function prepareData(EntityInterface $entity): array|null
    {
        if (!$entity instanceof ObjectEntity || $entity->deleted) {
            return null; // Do not include deleted objects in our app index.
        }

        return parent::prepareData($entity) + [
            'title_reverse' => strrev($entity->title),
        ];
    }

    public function findQuery(Query $query, array $options): Query
    {
        if (isset($options['type'])) {
            // The `type` finder from `ObjectSearchIndex` filters by `object_type` (exact match).
            $query = $query->find('type', ['type' => $options['type']]);
        }

        // This is the finder invoked by the adapter.
        return $query
            // The `available` finder from `ObjectSearchIndex` filter by `deleted`, `status` and `publish_*` fields, respecting current application settings.
            ->find('available')

            // Full text search and other conditions go here.
            ->queryMust(fn (QueryBuilder $builder): AbstractQuery => $builder->simpleQueryString(
                // Customize fields for full-text search:
                ['title^2', 'description', 'body', 'title_reverse^5'],
                $options['query'],
            ));
    }
}

In config/app.php:

    'Search' => [
        'adapters' => [
            // ...
            'my-app' => [
                'className' => \BEdita\ElasticSearch\Adapter\ElasticSearchAdapter::class,
                'index' => 'MyApp',
            ],
        ],
    ],

Copy link

codecov bot commented Mar 6, 2024

Codecov Report

Attention: Patch coverage is 9.48276% with 210 lines in your changes are missing coverage. Please review.

Project coverage is 22.14%. Comparing base (f52051a) to head (70a0be5).
Report is 24 commits behind head on main.

Files Patch % Lines
src/Model/Index/SearchIndex.php 0.00% 100 Missing ⚠️
src/Model/Index/ObjectSearchIndex.php 0.00% 57 Missing ⚠️
src/Command/CreateIndexCommand.php 0.00% 31 Missing ⚠️
src/Model/Index/IndexTrait.php 0.00% 17 Missing ⚠️
src/Plugin.php 0.00% 3 Missing ⚠️
src/Adapter/ElasticSearchAdapter.php 91.66% 2 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##                main       #3       +/-   ##
==============================================
- Coverage     100.00%   22.14%   -77.86%     
- Complexity         1       75       +74     
==============================================
  Files              1        6        +5     
  Lines              2      289      +287     
==============================================
+ Hits               2       64       +62     
- Misses             0      225      +225     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@le0m le0m added the release:major Major Version label Mar 6, 2024
@le0m le0m merged commit f80c752 into main Mar 6, 2024
19 of 20 checks passed
@le0m le0m deleted the indexing branch March 6, 2024 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:major Major Version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants