Hello @Mykhailo_Fomenko ,
Welcome to The MongoDB Community Forums!
MongoDB Atlas search provides a seamless and scalable solution for creating relevance-based application features through full-text search capabilities. It eliminates the necessity of running a separate search system alongside your database.
For your use-case, I added below 3 documents in my collection
[ {
_id: ObjectId("64c759c53f1a52e9fd5053b8"),
email: 'user1@test.com'
},
{
_id: ObjectId("64c759da3f1a52e9fd5053b9"),
email: 'anotheruser2@gmail.com'
},
{
_id: ObjectId("64c759f33f1a52e9fd5053ba"),
email: 'onemoreuser3@gmail.com'
} ]
Created a search index via Atlas UI, kept most settings as default but turned off Dynamic mapping and added a field mapping on my field ‘email’ with Data type as Autocomplete which performs a search for a word or phrase that contains a sequence of characters from an incomplete input string, below is my index definition
{
"mappings": {
"dynamic": false,
"fields": {
"email": {
"tokenization": "nGram",
"type": "autocomplete"
}
}
}
}
Ran below query to check if any email field contains gmail
db.collection.aggregate( [ {
$search: {
autocomplete: {
path: "email",
query: "gmail",
}
}
}])
Output
{
_id: ObjectId("64c759da3f1a52e9fd5053b9"),
email: 'anotheruser2@gmail.com'
}
{
_id: ObjectId("64c759f33f1a52e9fd5053ba"),
email: 'onemoreuser3@gmail.com'
}
Ran below query to check if any email field contains user
db.collection.aggregate( [ {
$search: {
autocomplete: {
path: "email",
query: "user",
}
}
}])
Output
{
_id: ObjectId("64c759da3f1a52e9fd5053b9"),
email: 'anotheruser2@gmail.com'
}
{
_id: ObjectId("64c759f33f1a52e9fd5053ba"),
email: 'onemoreuser3@gmail.com'
}
{
_id: ObjectId("64c759c53f1a52e9fd5053b8"),
email: 'user1@test.com'
}
Please note that the above examples I have posted is based off your provided search terms and sample email strings. If you have any other examples / search terms that you believe the autocomplete
operator may not work for then please let me know.
You may also wish to look at the following topic which may be of use to you too:
Let me know if this helps or if you have any more queries, would be happy to help you!
Regards,
Tarun