Skip to content

Conversation

@GromNaN
Copy link
Member

@GromNaN GromNaN commented Feb 17, 2025

Fix PHPORM-268

The configuration looks like this:

<?php # scout.php

return [
    'driver' => env('SCOUT_DRIVER', 'mongodb'),
    'prefix' => env('SCOUT_PREFIX', 'scout_'),

    'mongodb' => [
        'connection' => env('SCOUT_MONGODB_CONNECTION', 'mongodb'),
        'index-definitions' => [
            'scout_books' => [
                // The definition here
                'mappings' => ['dynamic' => true],
            ],
        ]
    ],
],

@GromNaN GromNaN requested a review from a team as a code owner February 17, 2025 15:25
@GromNaN GromNaN requested a review from jmikola February 17, 2025 15:25

$definition = $this->indexDefinitions[$name] ?? self::DEFAULT_DEFINITION;
if (! isset($definition['mappings'])) {
throw new LogicException(sprintf('The search index definition for collection "scout.mongodb.index-definition.%s" must contain a "mappings" key. Find documentation at https://www.mongodb.com/docs/atlas/atlas-search/create-index/', $name));
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure about adding a link here. Can be useful, maybe update it when the laravel docs is written.

Copy link
Member

Choose a reason for hiding this comment

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

Would InvalidArgumentException be preferable, or is there prior art for using LogicException directly?

https://www.mongodb.com/docs/atlas/atlas-search/create-index/ seems fine if you're in the habit of adding doc links to exception messages.

https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/#search-index-definition-syntax may be a more direct reference, though.

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated the exception class and the message with your link.


$definition = $this->indexDefinitions[$name] ?? self::DEFAULT_DEFINITION;
if (! isset($definition['mappings'])) {
throw new LogicException(sprintf('The search index definition for collection "scout.mongodb.index-definition.%s" must contain a "mappings" key. Find documentation at https://www.mongodb.com/docs/atlas/atlas-search/create-index/', $name));
Copy link
Member

Choose a reason for hiding this comment

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

Would InvalidArgumentException be preferable, or is there prior art for using LogicException directly?

https://www.mongodb.com/docs/atlas/atlas-search/create-index/ seems fine if you're in the habit of adding doc links to exception messages.

https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/#search-index-definition-syntax may be a more direct reference, though.

$engine = new ScoutEngine($database, false, ['collection_invalid' => ['foo' => 'bar']]);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('The search index definition for collection "scout.mongodb.index-definition.collection_invalid" must contain a "mappings" key');
Copy link
Member

Choose a reason for hiding this comment

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

Does this attempt to match the entire message, or is it only a prefix?

Copy link
Member Author

Choose a reason for hiding this comment

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

str_contains is used by PHPUnit for this.

$app['config']->set('scout.driver', 'mongodb');
$app['config']->set('scout.prefix', 'prefix_');
$app['config']->set('scout.mongodb.index-definitions', [
'prefix_scout_users' => ['mappings' => ['dynamic' => true, 'fields' => ['bool_field' => ['type' => 'boolean']]]],
Copy link
Member

Choose a reason for hiding this comment

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

Is it sensible to combine dynamic: true with explicit field mappings? I don't recall seeing anything like this in the server manual or Atlas docs.

Copy link
Member Author

@GromNaN GromNaN Feb 18, 2025

Choose a reason for hiding this comment

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

It's allowed. It forces some field types but enable automatic indexation for others. That's useful for me here as I want to ensure the dynamic mapping works with my search query, but also want to test a custom definition.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Will defer to you on the exception message.

@GromNaN GromNaN merged commit cb3b32c into mongodb:5.x Feb 19, 2025
44 checks passed
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.

2 participants