Let’s say I have 2 documents:
Document 1:
title: "elephant is an elephant"
description: "this elephant is an elephant"
Document 2:
title: "duck"
description: "duck is not an elephant"
How can I make Atlas search give both these results the same search score for “elephant”? I want it to only look for a keyword once and not weight the result higher if the keyword appears more often.
Note: Matches of different words should still rank higher than matching a single word.
When the user searches for “duck elephant”, document 2 should be listed higher because it matches both words.
My current idea is to use a constant search score and add entries to the search aggregation step dynamically for each word in the search query (which I turn into an array beforehand).
Is there a better way to handle this?
What do you think about this approach? It seems to work. Is there a better way to do it?
const shouldQueries = searchTerms.map(searchTerm: string) => ({
wildcard: {
query: searchTerm,
path: ['title', 'description'],
allowAnalyzedField: true,
score: { constant: { value: 1 } }
}
}));
let aggregation = Resource.aggregate()
.search({
compound: {
must: [...],
should: [...shouldQueries]
}
})