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

[10.x] Refactor the use of getScoutKeyName #509

Merged
merged 5 commits into from
Aug 24, 2021
Merged

Conversation

driesvints
Copy link
Member

@driesvints driesvints commented Aug 23, 2021

This PR changes the use of getScoutKeyName. The most significant change is the removal of the use of the qualified column name in the getScoutKeyName method. Other changes are using the getScoutKeyName instead of getKeyName so people can modify what column is used to index search records. This was already possible with Algolia but not with Meilisearch.

The reason why the qualified column was removed was that if someone were to override getScoutKeyName (with the other changes in mind from this PR) the toSearchableArray wouldn"t be compatible anymore as it won"t append the correct column name and thus search records in Meilisearch won"t index properly anymore. If we want to make getScoutKeyName adjustable for Meilisearch as well (as currently stated in the docs) then this change is necessary. As pointed out by @mmachatschek in #484 (comment), Algolia doesn"t suffer from this as it"s using its own internal objectID mapping.

The upgrade path for this unfortunately is that people will have to re-index their search records. If that"s not wanted then we should re-consider removing the ability to override the key name.

I"m also not entirely sure what the original reason for the use of the qualified column name was. I do not know if there will be a breaking change because if this.

I"d appreciate a review from you @mmachatschek before I mark this as ready to review for Taylor.

Copy link
Contributor

@mmachatschek mmachatschek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints

please update the \Laravel\Scout\Jobs\RemoveFromSearch::restoreCollection() method and remove the "fix" to get the unqualified key name.

also update the \Laravel\Scout\Tests\Fixtures\SearchableModelWithCustomKey::getScoutKeyName() method.

Copy link
Contributor

@mmachatschek mmachatschek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints

When changing Searchable::getScoutKeyName() the method Searchable::queryScoutModelsByIds() should be updated so that the key is uses a qualified column name? What do you think?

    public function queryScoutModelsByIds(Builder $builder, array $ids)
    {
        $query = static::usesSoftDelete()
            ? $this->withTrashed() : $this->newQuery();

        if ($builder->queryCallback) {
            call_user_func($builder->queryCallback, $query);
        }

        return $query->whereIn(
-            $this->getScoutKeyName(), $ids
+            $this->qualifyColumn($this->getScoutKeyName()), $ids
        );
    }

@driesvints
Copy link
Member Author

@mmachatschek did some of those changes.

When changing Searchable::getScoutKeyName() the method Searchable::queryScoutModelsByIds() should be updated so that the key is uses a qualified column name? What do you think?

Should we also do this for the makeAllSearchable"s orderBy statement?

    public static function makeAllSearchable($chunk = null)
    {
        $self = new static;

        $softDelete = static::usesSoftDelete() && config("scout.soft_delete", false);

        $self->newQuery()
            ->when(true, function ($query) use ($self) {
                $self->makeAllSearchableUsing($query);
            })
            ->when($softDelete, function ($query) {
                $query->withTrashed();
            })
-            ->orderBy($self->getScoutKeyName())
+            ->orderBy($this->qualifyColumn($self->getScoutKeyName()))
            ->searchable($chunk);
    }

Copy link
Contributor

@mmachatschek mmachatschek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints Yes.

Both Searchable::queryScoutModelsByIds() and Searchable::makeAllSearchable() should have the qualifiedColumn call IMO.

Additionally after the changes with the qualifiedColumn call the Algolia driver should have the same behaviour as before the changes.

src/Engines/CollectionEngine.php Outdated Show resolved Hide resolved
src/Searchable.php Outdated Show resolved Hide resolved
@driesvints
Copy link
Member Author

Both Searchable::queryScoutModelsByIds() and Searchable::makeAllSearchable() should have the qualifiedColumn call IMO.

@mmachatschek done, can you check again?

Additionally after the changes with the qualifiedColumn call the Algolia driver should have the same behaviour as before the changes.

How do we practically do this?

Copy link
Contributor

@mmachatschek mmachatschek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we practically do this?

Nothing to do here. Just wanted to point out that for algolia everything should be working as before now that you made the changes.

Edit: LGTM after fixing the test

@driesvints driesvints marked this pull request as ready for review August 23, 2021 18:32
@driesvints driesvints marked this pull request as draft August 23, 2021 18:32
@driesvints driesvints marked this pull request as ready for review August 24, 2021 08:15
@driesvints
Copy link
Member Author

Okay, this is ready for review now. I tried out the changes on both the Laravel.io and blade-ui-kit.com codebases without problems. @taylorotwell feel free to give this a review.

@mmachatschek
Copy link
Contributor

@driesvints MeiliSearch v0.21.0 was released yesterday. It brings breaking changes on the library level.
I"m not sure if there will be the 10.x release after merging this PR, but maybe lets wait for the update of the meilisearch/meilisearch-php library. The release of it should be out this week maybe even today.

@rosszt
Copy link

rosszt commented Mar 3, 2023

Fantastic, this is a great improvement for me. I had this exact issue this week 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants