I have a query that also checks whether a document has "valid" in a field, or this field does not exist. In both cases, I don't want this query to affect the scoring, so that if I sort by relevance (the default), documents without this field will also appear at the top. The documents looks like:
{
"names": [...],
"stats": [
{
"name": "...",
"status": "valid"
}
]
}
There are some documents with names but without stats, and this is as good as a valid status, however the documents with the stats appear first.
My query:
db.collection.aggregate([
{
$search:
{
index: [index name],
"compound": {
"should": [
[some should conditions that should affect the score]
],
"minimumShouldMatch": 1,
must: [
{
compound: {
should: [
{
phrase: {
path: 'stats.status',
query: "valid"
}
},
{
compound: {
mustNot: [{
exists: {
path: 'stats'
}
}]
}
}
],
"minimumShouldMatch": 1,
},
},
]
},
}
},
{
$addFields: {
score: {$meta: 'searchScore'}
}
},
])