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

PHPORM-28 Add Scout engine to index into MongoDB Search #3205

Merged
merged 8 commits into from
Jan 14, 2025

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Nov 13, 2024

Fix PHPORM-28

  • And "index" stands for a MongoDB collection with an Atlas Search index named "scout".
  • The collection name MUST be prefixed to prevent error of indexing in the same MongoDB Collection as the source documents.

Checklist

  • Add tests and ensure they pass

src/Scout/ScoutEngine.php Fixed Show fixed Hide fixed
src/Scout/ScoutEngine.php Fixed Show fixed Hide fixed
@GromNaN GromNaN changed the title PHPORM-28 Add Scout engined to index into MongoDB Search PHPORM-28 Add Scout engine to index into MongoDB Search Nov 14, 2024
@GromNaN GromNaN force-pushed the PHPORM-28 branch 2 times, most recently from fa3e0a9 to 12f3323 Compare November 15, 2024 13:20
src/Scout/ScoutEngine.php Fixed Show fixed Hide fixed
composer.json Outdated Show resolved Hide resolved
tests/Scout/ScoutEngineTest.php Outdated Show resolved Hide resolved
tests/Scout/ScoutEngineTest.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Fixed Show fixed Hide fixed
@GromNaN GromNaN force-pushed the PHPORM-28 branch 2 times, most recently from 13ff6ec to 4647cf0 Compare November 20, 2024 14:44
@GromNaN GromNaN marked this pull request as ready for review November 21, 2024 15:03
@GromNaN GromNaN requested a review from a team as a code owner November 21, 2024 15:03
@GromNaN GromNaN requested a review from jmikola November 21, 2024 15:03
@GromNaN
Copy link
Member Author

GromNaN commented Nov 21, 2024

Integration tests require an atlas or local-atlas cluster to run.
I need to find a way to test the result as the order is not the same for each reindexation.

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.

Leaving an incomplete round of feedback. We'll need to schedule some time to actually go through this PR together if you need a meaningful review, as a lot of it is over my head.

tests/Models/SearchableModel.php Outdated Show resolved Hide resolved
tests/Models/SearchableModel.php Outdated Show resolved Hide resolved
tests/Models/User.php Outdated Show resolved Hide resolved
tests/Scout/ScoutEngineTest.php Show resolved Hide resolved
tests/Scout/ScoutEngineTest.php Outdated Show resolved Hide resolved
tests/Scout/ScoutIntegrationTest.php Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the PHPORM-28 branch 2 times, most recently from 1b39753 to 95343b1 Compare November 23, 2024 09:57
tests/Models/SearchableModel.php Outdated Show resolved Hide resolved
tests/Models/User.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Show resolved Hide resolved
src/Scout/ScoutEngine.php Show resolved Hide resolved
src/Scout/ScoutEngine.php Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the PHPORM-28 branch 2 times, most recently from ee6edd2 to 3d030ba Compare December 2, 2024 11:40
@GromNaN GromNaN force-pushed the PHPORM-28 branch 2 times, most recently from 69fe000 to 831fa21 Compare December 17, 2024 18:55
src/Scout/ScoutEngine.php Fixed Show fixed Hide fixed
@GromNaN GromNaN force-pushed the PHPORM-28 branch 2 times, most recently from 7a0344c to c628969 Compare January 7, 2025 22:03
@github-actions github-actions bot added the github label Jan 8, 2025
@GromNaN GromNaN force-pushed the PHPORM-28 branch 2 times, most recently from 9b74d41 to a5739e0 Compare January 8, 2025 16:17
@GromNaN GromNaN requested a review from alcaeus January 8, 2025 18:50
Copy link
Member Author

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

Update:

  • Existing tests are not touched by the Scout tests. The service provider is registered only for the Scout tests and we use dedicated model classes.
  • Sorting search results by id field as the order is not constant between index creation.
  • CI and compose configuration fixed to make atlas search container more resilient (even if it still randomly crashes).
  • The mapping is dynamic (soft-delete field not added). Index definition to be made configurable in a follow-up ticket: PHPORM-268

@@ -11,6 +11,7 @@ jobs:
name: "PHP ${{ matrix.php }} Laravel ${{ matrix.laravel }} MongoDB ${{ matrix.mongodb }} ${{ matrix.mode }}"

strategy:
fail-fast: false
Copy link
Member Author

Choose a reason for hiding this comment

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

Tests with Atlas fail randomly (more than 1/4 of tries fail). fail-fast interrupt other jobs before they are successful, making it impossible to get all the tests successful simultaneously.

@@ -63,6 +64,9 @@ jobs:
until docker exec --tty mongodb mongosh 127.0.0.1:27017 --eval "db.runCommand({ ping: 1 })"; do
sleep 1
done
until docker exec --tty mongodb mongosh 127.0.0.1:27017 --eval "db.createCollection('connection_test') && db.getCollection('connection_test').createSearchIndex({mappings:{dynamic: true}})"; do
Copy link
Member Author

Choose a reason for hiding this comment

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

Ensure mongot is ready.

@GromNaN GromNaN requested a review from jmikola January 9, 2025 09:42
src/Scout/ScoutEngine.php Show resolved Hide resolved
docker-compose.yml Show resolved Hide resolved
docker-compose.yml Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Outdated Show resolved Hide resolved
src/Scout/ScoutEngine.php Show resolved Hide resolved
tests/Scout/ScoutEngineTest.php Outdated Show resolved Hide resolved
tests/Scout/ScoutEngineTest.php Outdated Show resolved Hide resolved
tests/Scout/ScoutIntegrationTest.php Outdated Show resolved Hide resolved
tests/Scout/ScoutIntegrationTest.php Show resolved Hide resolved
tests/Scout/ScoutIntegrationTest.php Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the PHPORM-28 branch 2 times, most recently from a6e2923 to 5c2c2e8 Compare January 14, 2025 18:43
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.

Noted that most of my previous points were all addressed. Left a few more small things that you can address or ignore as you wish.

ports:
- "27017:27017"
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh mongodb:27017 --quiet
test: mongosh --quiet --eval 'db.runCommand("ping").ok'
Copy link
Member

Choose a reason for hiding this comment

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

I assume this relies on mongosh defaulting `localhost:27017"

@@ -65,6 +65,9 @@ jobs:
until docker exec --tty mongodb mongosh 127.0.0.1:27017 --eval "db.runCommand({ ping: 1 })"; do
sleep 1
done
until docker exec --tty mongodb mongosh 127.0.0.1:27017 --eval "db.createCollection('connection_test') && db.getCollection('connection_test').createSearchIndex({mappings:{dynamic: true}})"; do
Copy link
Member

Choose a reason for hiding this comment

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

In docker-compose.yml you removed the host/port from the mongosh invocation. Should you do so here as well for consistency? I'm OK with either but wanted to ask.

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'll update it as part of PHPORM-287

@@ -17,7 +17,7 @@
</testsuite>
</testsuites>
<php>
<env name="MONGODB_URI" value="mongodb://mongodb/"/>
Copy link
Member

Choose a reason for hiding this comment

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

Is the mongodb hostname a sensible hostname here? In #3205 (comment) you mentioned removing it in favor of the default of localhost:27017. I understand that you'd need to use an explicit value in this context, but I'd expect to see localhost:27017 instead of mongodb in that case.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's the default localhost from the mongodb container, but from the other container, you need to specify the mongodb container name.

return 0;
}

return $results[0]->__count;
Copy link
Member

Choose a reason for hiding this comment

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

This seems tightly coupled to the $addFields stage in performSearch(). Pointing that out in case you'd like to do something about that.

I assume $results[0] can be assumed to be a stdClass instance per the internal type map, but @param mixed $results in the doc block doesn't tell us much. You might be able to address that with an internal assertion even though the method signature cannot be changed.


/**
* Get the total count from a raw result returned by the engine.
* This is an estimate if the count is larger than 1000.
Copy link
Member

Choose a reason for hiding this comment

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

A @see reference to https://www.mongodb.com/docs/atlas/atlas-search/counting/ may be helpful here. You can also mention that this pertains to the pipeline created in performSearch() since that's what ultimately determines this.

// "filter" specifies conditions on exact values to match
// "mustNot" specifies conditions on exact values that must not match
// They don't contribute to the relevance score
// https://www.mongodb.com/docs/atlas/atlas-search/compound/#options
Copy link
Member

Choose a reason for hiding this comment

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

@GromNaN: Pointing this out in case you lost track of this and agreed with leaving a comment above.

@GromNaN GromNaN merged commit 19ed55e into mongodb:5.x Jan 14, 2025
32 checks passed
@GromNaN GromNaN deleted the PHPORM-28 branch January 14, 2025 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants