-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PHPORM-268 Add configuration for scout search indexes #3281
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
Conversation
src/Scout/ScoutEngine.php
Outdated
@@ -435,14 +437,16 @@ public function createIndex($name, array $options = []): void | |||
{ | |||
assert(is_string($name), new TypeError(sprintf('Argument #1 ($name) must be of type string, %s given', get_debug_type($name)))); | |||
|
|||
$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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
src/Scout/ScoutEngine.php
Outdated
@@ -435,14 +437,16 @@ public function createIndex($name, array $options = []): void | |||
{ | |||
assert(is_string($name), new TypeError(sprintf('Argument #1 ($name) must be of type string, %s given', get_debug_type($name)))); | |||
|
|||
$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)); |
There was a problem hiding this comment.
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.
tests/Scout/ScoutEngineTest.php
Outdated
$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'); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@@ -38,6 +39,9 @@ protected function getEnvironmentSetUp($app): void | |||
|
|||
$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']]]], |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
Fix PHPORM-268
The configuration looks like this: