I cannot find any documentation for this. I want to perform this query with Go:
{
“from”: 0,
“size”: 10,
“query”: {
“top_left”: {
“lng”: -2.235143,
“lat”: 53.482358
},
“bottom_right”: {
“lng”: 28.955043,
“lat”: 40.991862
},
“field”: “Location”
}
}
Something like this:
params := mux.Vars(r)
indexName := "connection_location"
topLeft := [2]string{params["top_left_lat"], params["top_left_lng"]}
bottomRight := [2]string{params["bottom_right_lat"], params["bottom_right_lng"]}
query := gocb.NewSearchQuery(indexName, topLeft, bottomRight)
result, err := bucketConnections.ExecuteSearchQuery(query)
Thanks
chvck
September 18, 2019, 7:29am
2
Hi @Tim_Cranfield I think that you’re looking for https://godoc.org/gopkg.in/couchbase/gocb.v1/cbft#NewGeoBoundingBoxQuery which I think would look like
boundedQuery := gocb.NewGeoBoundingBoxQuery(
params["top_left_lat"],
params["top_left_lng"],
params["bottom_right_lat"],
params["bottom_right_lng"],
)
query := gocb.NewSearchQuery(indexName, boundedQuery)
Thanks that’s on the right track, but NewGeoBoundingBoxQuery is part of cbft. The arguments need to be floats.
type BoundingBox struct {
TopLeftLat float64 `json:"TopLeftLat"`
TopLeftLng float64 `json:"TopLeftLng"`
BottomRightLat float64 `json:"BottomRightLat"`
BottomRightLng float64 `json:"BottomRightLng"`
}
func PostConnectionsWithinBoundsHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var boundingBoxData BoundingBox
err := decoder.Decode(&boundingBoxData)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
indexName := "idx_location"
boundedQuery := cbft.NewGeoBoundingBoxQuery(
boundingBoxData.TopLeftLat,
boundingBoxData.TopLeftLng,
boundingBoxData.BottomRightLat,
boundingBoxData.BottomRightLng)
boundedQuery.Field("Location")
query := gocb.NewSearchQuery(indexName, boundedQuery)
result, err := bucket.ExecuteSearchQuery(query)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
json.NewEncoder(w).Encode(result)
}
But still not getting any results so something is not right. I can run the same query on the command line and get results. Perhaps the way the field is specified is wrong.
Also, confusingly, the order of lat and lng arguments is different in different documentation.
https://godoc.org/gopkg.in/couchbase/gocb.v1/cbft#NewGeoBoundingBoxQuery
https://godoc.org/github.com/couchbase/gocb#NewGeoBoundingBoxQuery
I am using “gopkg.in/couchbase/gocb.v1/cbft”