Any example for combining geo location search query with distance filter/sorting?

154 Views Asked by At

I am using gocb to query documents based on geo location . So far NewGeoDistanceQuery works well with the index. Now i need to sort the result based on geo location distance . According to the doc in here , it says i need to pass sort parameter inside the query! but the NewGeoDistanceQuery doesnt have that. So searched the sdk and found NewSearchSortGeoDistance that is exactly what i was looking for but I am confused on how to combine them.

    location := cbft.NewGeoDistanceQuery(in.Lat, in.Lon, fmt.Sprintf("%skm", in.Distance))
    sort := cbft.NewSearchSortGeoDistance("address", in.Lat, in.Lon).Unit("km")
    conjunctionQuery = cbft.NewConjunctionQuery(location, sort)

I tried the solution above but got this error

{"error":"rest_index: Query, indexName: restaurant-geo-search, err: bleve: QueryBleve parsing searchRequest, err: unknown query type","request":{"ctl":{"timeout":75000},"query":{"conjuncts":[{"distance":"2km","location":[90.404272,23.793993]},{"by":"geo_distance","field":"address","location":[90.404272,23.793993],"unit":"km"}]},"size":100},"status":"fail"}

I have also tried to use the NewSearchSortGeoDistance alone but same error. Any help on this ?

1

There are 1 best solutions below

0
On

I've answered at https://forums.couchbase.com/t/any-example-for-combining-geo-location-search-query-with-distance-filter-sorting/21155 but just to put all the info here too:

I think that the issue here is that you’re trying to use sort order as a query, rather than as sorting criteria. I think that what you want is probably gocb.NewSearchQuery(indexName, location).Sort(sort) . This will take the sort criteria and put it into the JSON request under the sort field, as you can see in our documentation.