Allow $search to be a later stage in the aggregation pipeline
Currently $search is required to be the first stage in the aggregation pipeline. It would be more intuitive if we could use after other operators, such as $match.
-
Jan Sporek commented
It would also be great (even better :) ) to be able to call $search after $unwind, because it is currently impossible to score individual embeddedDocuments.
-
Sukhmeet commented
This would be great if the $search stage following a $match can be made as performant as when it is the first stage. My suspicion is that some of the id lookup will be required after the $match stage while handing off the the $search stage, and that might not be as performant.
-
Charlie commented
Could this include executing the search separately, then merging the results with the previous stage(s)?
-
AdminMarcus Eagan (Admin, MongoDB) commented
Tyler, multi-tenancy can be accomplished easily with the filter clause within the compound operator for search and others.
I'm attaching an example query with the same message that I am posting here. Of course, the comment is not valid JSON so you would need to run the query if you want to.
This query uses the sample_restaurants.restaurants data set in Atlas and the default index. Assume that the customers here are the boroughs of New York. Below is a query by the Queens tenant for a deli specializing in Sandwiches. It returns a number of documents but only those in Queens.
Let me know if you have any questions.
/** This query uses the sample_restaurants.restaurants dataset in Atlas and the default index.
Assume that the customers here are the boroughs of New York.
Below is a query by the Queens tenant for a deli specializing in Sandwiches. It reutrns a number of documents but only those in Queens.
**/{
"index": "default",
"compound": {
"must": [
{
"text": {
"query": "Deli",
"path": "name"
}
}
],
"should": [
{
"text": {
"query": "Sandwiches",
"path": "cuisine"
}
}
],
"filter": [
{
"text": {
"query": "Queens",
"path": "borough"
}
}
]
}
} -
Tyler Bell commented
Are there any updates to this?
Like Cassio, my company can't use Atlas Search for our multi-tenant application until this is possible.
Thanks!
-
Justin commented
@Cassio Why can't you $match after the $search?
-
Yurii commented
Hopefully once this feature is released I will be able to apply additional stages before $search with regular indexes since now any stages after don't use indexes e.g. to do $sort of results
-
Cassio commented
Intuitiveness is not really the problem here. Not being able to perform a $search after a $match means that I can't use this on a multi-tenant database.
-
Alex commented
Thanks for creating this feature request Doug. I just wanted to say that our team strongly agrees here - adding the $search after a $match would be very intuitive.