You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

geo_boundingpolygon.go 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2019 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package query
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/blevesearch/bleve/geo"
  19. "github.com/blevesearch/bleve/index"
  20. "github.com/blevesearch/bleve/mapping"
  21. "github.com/blevesearch/bleve/search"
  22. "github.com/blevesearch/bleve/search/searcher"
  23. )
  24. type GeoBoundingPolygonQuery struct {
  25. Points []geo.Point `json:"polygon_points"`
  26. FieldVal string `json:"field,omitempty"`
  27. BoostVal *Boost `json:"boost,omitempty"`
  28. }
  29. func NewGeoBoundingPolygonQuery(points []geo.Point) *GeoBoundingPolygonQuery {
  30. return &GeoBoundingPolygonQuery{
  31. Points: points}
  32. }
  33. func (q *GeoBoundingPolygonQuery) SetBoost(b float64) {
  34. boost := Boost(b)
  35. q.BoostVal = &boost
  36. }
  37. func (q *GeoBoundingPolygonQuery) Boost() float64 {
  38. return q.BoostVal.Value()
  39. }
  40. func (q *GeoBoundingPolygonQuery) SetField(f string) {
  41. q.FieldVal = f
  42. }
  43. func (q *GeoBoundingPolygonQuery) Field() string {
  44. return q.FieldVal
  45. }
  46. func (q *GeoBoundingPolygonQuery) Searcher(i index.IndexReader,
  47. m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
  48. field := q.FieldVal
  49. if q.FieldVal == "" {
  50. field = m.DefaultSearchField()
  51. }
  52. return searcher.NewGeoBoundedPolygonSearcher(i, q.Points, field, q.BoostVal.Value(), options)
  53. }
  54. func (q *GeoBoundingPolygonQuery) Validate() error {
  55. return nil
  56. }
  57. func (q *GeoBoundingPolygonQuery) UnmarshalJSON(data []byte) error {
  58. tmp := struct {
  59. Points []interface{} `json:"polygon_points"`
  60. FieldVal string `json:"field,omitempty"`
  61. BoostVal *Boost `json:"boost,omitempty"`
  62. }{}
  63. err := json.Unmarshal(data, &tmp)
  64. if err != nil {
  65. return err
  66. }
  67. q.Points = make([]geo.Point, 0, len(tmp.Points))
  68. for _, i := range tmp.Points {
  69. // now use our generic point parsing code from the geo package
  70. lon, lat, found := geo.ExtractGeoPoint(i)
  71. if !found {
  72. return fmt.Errorf("geo polygon point: %v is not in a valid format", i)
  73. }
  74. q.Points = append(q.Points, geo.Point{Lon: lon, Lat: lat})
  75. }
  76. q.FieldVal = tmp.FieldVal
  77. q.BoostVal = tmp.BoostVal
  78. return nil
  79. }