I’ve provided further details below which may help but if not and in future, can you provide the JSON format of the index definition? The behaviour may differ if I use the default index definition in my own environment compared to your environment so testing would not be as effective.
In future could, you provide documents in JSON format so we can easily copy and paste them into our test environment if we need to test? This makes it easier for the people assisting so that it would be easier for them to help you.
In saying so, i’ve used the default index definition for my test environment. Please see sample documents below:
dodge> db.collection.find({},{_id:0})
[
{ name: 'dodge' },
{ name: 'dodge ram' },
{ name: 'ram dodge 2016' },
{ name: 'dodge ram 2500' },
{ name: 'floor mats for dodge ram' }
]
I then utilised the compound operator with the should clause (my index is called nameindex for this test):
db.collection.aggregate([
{
$search: {
index: 'nameindex',
compound: {
should: [{
text: {
query : 'dodge ram floor mats',
path: 'name'
}
}]
}
}
},
{
$project: {
_id: 0,
'name': 1,
'score': {$meta: 'searchScore'}
}
}
])
I’ve projected the score for your reference as well. The output is below:
[
{ name: 'floor mats for dodge ram', score: 1.082603096961975 },
{ name: 'dodge ram', score: 0.19285692274570465 },
{ name: 'ram dodge 2016', score: 0.16547974944114685 },
{ name: 'dodge ram 2500', score: 0.16547974944114685 },
{ name: 'dodge', score: 0.05366339907050133 }
]
You might also be able to use queryString to achieve what you’re after.
With any of these examples, please test thoroughly in a test environment to ensure it suits all your use case(s) and requirement(s).
Regards,
Jason