Please see the example below which I believe matches what you have described above. 5 sample documents in which 4 match the above description:
[
{
headline: 'News of the day!',
subtitle: '',
fulltext: 'sony will partnership with Honda in making cars'
},
{
headline: 'News of the day!',
subtitle: 'mobile iPhones test',
fulltext: 'this is just random text '
},
/// the one document below this contains only honda and mobile
{
headline: 'nothing',
subtitle: 'mobile only',
fulltext: 'honda only'
},
{
headline: 'cars here',
subtitle: 'honda here',
fulltext: 'test only'
},
{
headline: 'iphones here',
subtitle: 'mobile here',
fulltext: 'test 2 only'
}
]
The $search
stage used:
{
'$search': {
index: 'compoundindex',
compound: {
should: [
{
compound: {
must: [
{ text: { query: 'honda', path: { wildcard: '*' } } },
{ text: { query: 'cars', path: { wildcard: '*' } } }
]
}
},
{
compound: {
must: [
{ text: { query: 'iphones', path: { wildcard: '*' } } },
{ text: { query: 'mobile', path: { wildcard: '*' } } }
]
}
}
]
}
}
}
Output:
[
/// The document contains "car" AND "honda"
{
headline: 'cars here',
subtitle: 'honda here',
fulltext: 'test only'
},
/// The document contains "iphone" AND "mobile"
{
headline: 'iphones here',
subtitle: 'mobile here',
fulltext: 'test 2 only'
},
/// The document contains "car" AND "honda"
{
headline: 'News of the day!',
subtitle: '',
fulltext: 'sony will partnership with Honda in making cars'
},
/// The document contains "iphone" AND "mobile"
{
headline: 'News of the day!',
subtitle: 'mobile iPhones test',
fulltext: 'this is just random text '
}
]
I have only tested this on the 5 sample documents mentioned above and if you believe it suits your use case please test thoroughly and and adjust the search query accordingly to verify it meets your requirements.
You can refer to the nested example as well for the compound
operator.
Regards,
Jason