Geo Location search not working, pivot is of no use

Hello everyone,

I need to implement text search along with location search (via lat, long). For this, I have implemented
this (https://www.mongodb.com/docs/atlas/atlas-search/near/#examples)

But the expected results are not coming.

Sharing the query,

{
        $search: {
          compound: {
            must: [
              {
                text: {
                  query: searchQuery.source,
                  path: "all_content",
                },
              },
              {
                near: {
                  origin: {
                    type: "Point",
                    coordinates: [Number(long), Number(lat)],
                  },
                  path: "address.point",
                  pivot: 1000,
                },
              },
            ],
          },
        },
      }

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,

whatever I am passing in pivot is not working, every time I got all the documents matching with the given query, Please let me know what’s wrong here

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

1 Like

Thanks @Jason_Tran, will update you on this

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.