`I am working on following Events Collection:
{
title: "Birthday party",
location: {
address: "main street, Užupis Pizza, Užupis, Vilnius, Lithuania",
locationDetails: {
locality: "Vilnius",
country: "Lithuania",
administrative_area_level_2: "Vilnius city municipality",
administrative_area_level_1: "Vilnius County",
},
},
{
title: "Wedding",
location: {
address: "Prie angelo, Uzupis, Vilnius, Lithuania",
locationDetails: {
locality: "Vilnius",
country: "Lithuania",
administrative_area_level_2: "Uzupis",
administrative_area_level_1: "Vilnius",
},
},
},
{
title: "engagement",
location: {
address: "Plaza, NYC, USA",
locationDetails: {
locality: "New your city",
country: "USA",
administrative_area_level_1: "New York City",
},
},
}
I am looking for an efficient query which can find the sorted documents by most relevant location (nearest location) to less relevant location (Farthest location).
Example:
Case I: If user Search event near "Uzupis pizza", or "main street Užupis" it should return
[ {
title: "Birthday party",
location: {
address: "Užupis Pizza, main street, Užupis, Vilnius, Lithuania",
locationDetails: {
locality: "Vilnius",
country: "Lithuania",
administrative_area_level_2: "Vilnius city municipality",
administrative_area_level_1: "Vilnius County",
},
},
{
_id: {
$oid: "2",
},
title: "Wedding",
location: {
address: "Prie angelo, Uzupis, Vilnius, Lithuania",
locationDetails: {
locality: "Vilnius",
country: "Lithuania",
administrative_area_level_2: "Uzupis",
administrative_area_level_1: "Vilnius",
},
},
}]
Case 2: If user search "Prie angelo Vilnius" It should return both documents but change in order.
[{
_id: {
$oid: "2",
},
title: "Wedding",
location: {
address: "Prie angelo, Uzupis, Vilnius, Lithuania",
locationDetails: {
locality: "Vilnius",
country: "Lithuania",
administrative_area_level_2: "Uzupis",
administrative_area_level_1: "Vilnius",
},
},
},
{
_id: {
$oid: "1",
},
title: "Birthday party",
location: {
address: "Užupis Pizza, Užupis, Vilnius, Lithuania",
locationDetails: {
locality: "Vilnius",
country: "Lithuania",
administrative_area_level_2: "Vilnius city municipality",
administrative_area_level_1: "Vilnius County",
},
}]
I tried solution like regex, text search but I am not sure if they are efficient enough as we are expecting 2 million event data in next couple of year. I can change in schema also or add location co-ordinates if required. Thanks.
CodePudding user response:
This might be use full.
In your case your are expecting to perform full text search.
Reference from https://www.mongodb.com/docs/manual/core/link-text-indexes/
Step 1
: Create a Text index to location.address
db.event_col.createIndex({"location.address": "text"})
Step 2
: Aggregate the query
//This might be use full.
db.collection.aggregate([
{
$match: {
$text: {
$search: "Prie angelo Vilnius"
}
}
}
])
If you need to added geo-coordinate you can add additional pipelines to this query.
Text search for relevant data after you can go for near location search via geosearch.
I checked the above cases it's works on.