Add full-text search to your application
You now know everything you need to add full-text search capabilities to your application.
Open up the code from the server file server/index.mjs
once more, and edit the /search/:name
endpoint to use full text search.
tip
You'll need to use the aggregate
method instead of the find
method you used earlier. Check out the documentation for more details.
Answer
app.get("/search/:name", async (req, res) => {
const pipeline = [
{
$search: {
index: "default",
text: {
query: "Mesi",
path: "short_name",
fuzzy: {
maxEdits: 1,
},
score: {
function: {
add: [{ path: "overall" }, { score: "relevance" }],
},
},
},
},
},
];
const players = await collection.aggregate(pipeline).toArray();
res.json(players).status(200);
});