Hi @Ankit_Arora,
Now What I expect here is, for the given query only those documents should be returned which are in the given lat long range of 1000 m. but this is not how it works I guess,
pivot
is a value which is used to calculate scores of Atlas Search result documents based off a formula. Perhaps if you’re wanting to limit the results within a particular range, the geoWithin
operator may suit your use case better. If you then want to do scoring on these filtered documents based on how close they are to a particular point (based off your example), you could then use the compound
operator to include both near
and geoWithin
.
Hopefully the below examples helps clarify the above.
Using geoWithin
only:
[{
"$search": {
"geoWithin": {
"circle": {
"center": {
"type": "Point",
"coordinates": [-75.2, 13]
},
"radius": 200000
},
"path": "position"
}
}
},
{
$project: {
_id: 0,
position: 1,
score: { $meta: "searchScore" }
}
}]
Output (Note the same score value of 1 for all these documents):
[
{
position: { type: 'Point', coordinates: [ -76.3, 12.2 ] },
score: 1
},
{ position: { type: 'Point', coordinates: [ -75.2, 13 ] }, score: 1 },
{
position: { type: 'Point', coordinates: [ -74.3, 13.7 ] },
score: 1
},
{
position: { type: 'Point', coordinates: [ -75.7, 12.5 ] },
score: 1
},
{
position: { type: 'Point', coordinates: [ -74.2, 13.7 ] },
score: 1
},
{
position: { type: 'Point', coordinates: [ -75.4, 13.1 ] },
score: 1
}
]
Using near
only (limiting to 3 documents for brevity):
[
{
'$search': {
index: 'default',
near: {
path: 'position',
origin: { type: 'Point', coordinates: [ -75.2, 13 ] },
pivot: 1
}
}
},
{
'$project': { _id: 0, position: 1, score: { '$meta': 'searchScore' } }
},
{ '$limit': 3 }
]
Output (Note the pivot
value used and scores):
[
{ position: { type: 'Point', coordinates: [ -75.2, 13 ] }, score: 1 },
{
position: { type: 'Point', coordinates: [ -75.4, 13.1 ] },
score: 0.00004106335836695507
},
{
position: { type: 'Point', coordinates: [ -75.7, 12.5 ] },
score: 0.00001287592385779135
}
]
Using the compound
operator containing both geoWithin
and near
:
[
{
'$search': {
index: 'default',
compound: {
must: [
{
geoWithin: {
circle: {
center: { type: 'Point', coordinates: [ -75.2, 13 ] },
radius: 200000
},
path: 'position'
}
}
],
should: [
{
near: {
path: 'position',
origin: { type: 'Point', coordinates: [ -75.2, 13 ] },
pivot: 1
}
}
]
}
}
},
{
'$project': { _id: 0, position: 1, score: { '$meta': 'searchScore' } }
}
]
Output (Documents only within the specified circle radius
returned sorted by score in which the near
operator is used to assist with for nearest to furthest):
[
{ position: { type: 'Point', coordinates: [ -75.2, 13 ] }, score: 2 },
{
position: { type: 'Point', coordinates: [ -75.4, 13.1 ] },
score: 1.0000410079956055
},
{
position: { type: 'Point', coordinates: [ -75.7, 12.5 ] },
score: 1.0000128746032715
},
{
position: { type: 'Point', coordinates: [ -74.3, 13.7 ] },
score: 1.0000079870224
},
{
position: { type: 'Point', coordinates: [ -74.2, 13.7 ] },
score: 1.0000075101852417
},
{
position: { type: 'Point', coordinates: [ -76.3, 12.2 ] },
score: 1.0000066757202148
}
]
Regards,
Jason