I have a Node/NestJS backend application that uses MongoDB (with the Mongoose driver). For a "Get" function, I have set up an aggregation pipeline where first some "hard" filters can be applied, which filters out content entirely - and now I want some soft filters, which ranks search results and filters them out if they are irrelevant. This algorithm should use three fields on the document: title, description and tags. The title and the tags should have the most weight of those. Results would be excluded if the total relevance score falls below a certain threshold. Now, I have checked several other StackOverflow posts for this, for example this one, but they all seem to be about the "tags" field alone. A piece of doc I found suggested to use indexes for this, but I preferably want to do it through the aggregation framework, if I knew approximately how to do it.
Below is code from another application that demonstrates the functionality;
do {
let reg
if (Array.isArray(searchString)) {
reg = new RegExp(searchString[i], 'gi')
} else {
reg = new RegExp(searchString, 'gi')
}
for (const note of this.notes) {
const countTitle = (note.title.match(reg) || []).length
note.searchScore += countTitle
let countTags = 0
for (const tag of note.tags) {
const tagLength = (tag.match(reg) || []).length
countTags += tagLength
}
note.searchScore += countTags * 0.5
const countContent = (note.content.match(reg) || []).length
note.searchScore += countContent * 0.3
}
i++
} while (!Array.isArray(searchString) && i < searchString.length)
this.toDisplay = this.notes.filter(
f => f.searchScore > 0 + searchString.length / 4
)
this.showNew = false
this.sortUp = false
this.sortItems('relevance')
} else {
this.updateUI()
}
}
The algorithm above takes a string or array of strings. Title, tags and description/content have weights of 1, 0.5 and 0.3 respective. A threshold is set where items are filtered out entirely when score is lower or equal to 0 + the amount of search terms divided by 4. The values can be adjusted, but in essence, this is the algorithm I want to implement within the aggregate framework. How would it kind of look like? Thanks in advance.