use geonear with fuzzy search text mongodb

311 Views Asked by At

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?

0

There are 0 best solutions below