Suray_T
(Suray Tej)
December 13, 2022, 2:23am
1
Suppose I have a collection with documents that are structured like this.
{
"_id": {
"$oid": "6397dc537457f02b0ca50a6c"
},
"route": "nonstop",
"cities": [
{
"name": "Ontario",
"layover": {
"duration": "long"
}
}
]
}
I created the following Atlas Search index for this collection.
{
"mappings": {
"dynamic": false,
"fields": {
"cities": {
"dynamic": true,
"fields": {
"layover.duration": {
"type": "autocomplete"
},
"name": {
"type": "autocomplete"
}
},
"type": "embeddedDocuments"
},
"route": {
"type": "autocomplete"
}
}
}
}
I am able to search based on the “name” attribute that is in each element of the “cities” array.
Is it possible to search based on the “layover.duration” attribute?
I tried doing so, but my pipeline is not returning any results.
Please see my pipeline below.
[{
$search: {
index: 'TravelSearch',
embeddedDocument: {
path: 'cities',
operator: {
autocomplete: {
query: 'long',
path: 'cities.layover.duration'
}
}
}
}
}, {
$addFields: {
score: {
$meta: 'searchScore'
}
}
}]
Aasawari
(Aasawari Sahasrabuddhe)
January 3, 2023, 4:24am
3
Hi @Suray_T and welcome to the MongoDB community forum!!
Based on the sample data shared above, I tried to reproduce this in my own atlas environment and below is the index definition and the query used for the data.
Index definition:
"mappings": {
"dynamic": false,
"fields": {
"cities": {
"dynamic": true,
"fields": {
"layover": {
"fields": {
"duration": {
"type": "autocomplete"
}
},
"type": "document"
},
"name": {
"type": "autocomplete"
}
},
"type": "embeddedDocuments"
},
"route": {
"type": "autocomplete"
}
}
}
}
and the search query :
[
{
'$search': {
'index': 'travel',
'embeddedDocument': {
'path': 'cities',
'operator': {
'compound': {
'must': [
{
'autocomplete': {
'path': 'cities.layover.duration',
'query': 'long'
}
}
],
'should': [
{
'autocomplete': {
'path': 'cities.layover.duration',
'query': 'long'
}
}
]
}
}
}
}
}
]
Please note that, the above query is based on the sample data shared, hence I would recommend through testing for the complete dataset.
If you believe this index definition and query do not fit your use case, please provide further information regarding the use case.
As of the time of this message, the Atlas Search embeddedDocuments index option, embeddedDocument operator, and embedded
scoring option are in preview.
Let us know if you have any further queries.
Best Regards
Aasawari
3 Likes
system
(system)
Closed
January 10, 2023, 3:28am
5
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.