0

I have the following query

db.hotels.aggregate([
    {
        $search: {
            index:'txtIdx', // this is the index name
            text: {
                query:"sun",
                path:['name','address.landmark','address.city','address.state','address.country'],
                fuzzy: {
                  maxEdits:2,
                  prefixLength: 1,
                },
            },
        },
    },
    {
        $project: {
          _id: 1,
          name: 1,
          address:1,
          score: { $meta: "searchScore" }
        }
    },
    {$limit:100},
])

there is also a field called 'location' in hotels' collection, which has coordinates as follows

"location": {
        "type": "Point",
        "coordinates": [
               72.867804,
               19.076033
        ]
    } 

how can I use geonear with this search query to only return near by hotels from user, with provided latitude, longitude and distance.

I also tried this query

    {
    $search: {
      index:'searchIndex',
      compound: {
        must: {
          text: {
            query:'sun',
            path:['name','address.landmark','address.city','address.state','address.country'],
            fuzzy: {
              maxEdits:2,
              prefixLength: 3,
            },
          },
        },
        should: {
            near:{
             origin: {
                type: 'Point',
                coordinates: [-122.45665489904827,37.75118012951178],
             },
            pivot: 1000,
            path: 'location'
          },
        }
      }
    }
  },

but above query returns results which are not even around that location. It returns same result as 'search' would provide without 'near'.I have created 'geo' index for location, still it doesn't return nearby hotels.

Or is there another way apart from using geonear along with search? I am trying since past 2 days now, and I haven't found anything useful. Also I want to use fuzzy text search only. Please let me know if there is a way to solve this?

Doug
  • 14,387
  • 17
  • 74
  • 104
  • First try and run a query with the `near` operator alone to make sure it returns the results in the order you want. If it works then likely your problem is that the score impact of the `near` clause is not as strong as you would like, you can address it by [boosting](https://docs.atlas.mongodb.com/reference/atlas-search/scoring/#boost) the score of the `near` clause. – Oren Apr 09 '21 at 16:20
  • 2
    i tried with only near.... and the result returns all the documents.. Idk why it didn't filter only nearby hotels... I have created index for location like this for search `"location": [ { "type": "document" }, { "type": "geo" } ], ` i even tried with just {"type":"geo"}, but this throws error while creating index... let me know why am i not getting only nearby hotels.. i even tried boosting it still returns the same result... :/ –  Apr 14 '21 at 12:16
  • `near` doesn't filter any documents just scores them. If you want to also filter by distance use the geoWithin operator (can filter out all documents not within a circle / radius of your origin). You can combine both `near` and `geoWithin` using a `compound` to perform filtering and ranking in the same query/ docs: https://www.mongodb.com/docs/atlas/atlas-search/geoWithin/ – Oren Jul 22 '22 at 22:18

0 Answers0