How to implement and use search tokens for efficient text search in MongoDB
Search tokens are a powerful mechanism in Orionjs that enable efficient and flexible text search capabilities in MongoDB without the overhead of full-text search. They work by preprocessing text fields into normalized tokens that can be indexed and queried efficiently.
First, add a searchTokens
field to your schema:
Add an index on the searchTokens
field in your repository:
Add a method to generate search tokens from relevant fields:
getSearchQueryForTokens
function converts search terms into MongoDB queriesshortenMongoId
to include readable portions of IDsgetSearchTokens
isEqual
to avoid unnecessary updates