Hello MongoDB,
I would like to run a compound search query which returns relevant results, but at the moment my query is returning every result and I don’t understand why. My issue/problem is that the search-term may be contained in a number of fields, and therefore I can’t stipulate a ‘must’ contain in the query.
Let’s say my data looks like this:
{
restaurantName: String, //SAMPLE 'The Serenity'
restaurantTitle: String, //SAMPLE 'Gorgeous vegan restaurant with river views'
restaurantDesc: String, //SAMPLE 'The Serenity is a three-michelin-starred restaurant in the heart of Soho...'
restaurantType: String //SAMPLE 'Chinese'
area: String //SAMPLE 'Mayfair'
city: String // SAMPLE 'London'
country: String // SAMPLE 'UK"
}
At the moment my query is as follows (once this bit works I will obviously build it out). I’m just trying with two fields and a ‘should’ condition, I thought meaning return a result if the searchTerm is in either:
$search: {
index: 'defaultRestaurantSearch',
compound: {
should: [
{
text: {
query: req.query.searchTerm,
path: 'restaurantTitle',
fuzzy: {},
score: {
boost: {
value: 9,
},
},
},
},
{
text: {
query: req.query.searchTerm,
path: 'restaurantDescription',
fuzzy: {},
score: {
boost: {
value: 8,
},
},
},
},
],
},
},
In the app there’s already a filtering system to allow users to filter restaurants. However, we would like to have an open text field search where we can basically expect users to type anything, from the restaurant name to something such as ‘chinese in Soho’.
At the moment, if I just enter ‘West End’ into the field, all records are returned, even those with no mention of Soho in the name, title, description, area etc…
Is there an obvious error in the code? Or is this even possible? I have been studying the sample Restaurant app at https://www.atlassearchrestaurants.com/ to get a feel for the compound query (even though the use-cases are slightly different) but I can’t get it to work.
I thought the problem may be with the Index Definition, and have tried both
{
"mappings": {
"dynamic": true
}
}
and
{
"mappings": {
"dynamic": false,
"fields": {
"restaurantName": [
{
"dynamic": true,
"type": "document"
},
{
"type": "string"
}
],
"restaurantTitle": [
{
"dynamic": true,
"type": "document"
},
{
"type": "string"
}
],
...//OTHER FIELDS HERE
}
}
}
But neither have made a difference.
I’ve used Atlas search a number of times before, but more simple searches on a single name field. Any help would be very much appreciated.
Cheers,
Matt