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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) 2017 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 GeoDistanceQuery struct {
  25. Location []float64 `json:"location,omitempty"`
  26. Distance string `json:"distance,omitempty"`
  27. FieldVal string `json:"field,omitempty"`
  28. BoostVal *Boost `json:"boost,omitempty"`
  29. }
  30. func NewGeoDistanceQuery(lon, lat float64, distance string) *GeoDistanceQuery {
  31. return &GeoDistanceQuery{
  32. Location: []float64{lon, lat},
  33. Distance: distance,
  34. }
  35. }
  36. func (q *GeoDistanceQuery) SetBoost(b float64) {
  37. boost := Boost(b)
  38. q.BoostVal = &boost
  39. }
  40. func (q *GeoDistanceQuery) Boost() float64 {
  41. return q.BoostVal.Value()
  42. }
  43. func (q *GeoDistanceQuery) SetField(f string) {
  44. q.FieldVal = f
  45. }
  46. func (q *GeoDistanceQuery) Field() string {
  47. return q.FieldVal
  48. }
  49. func (q *GeoDistanceQuery) Searcher(i index.IndexReader, m mapping.IndexMapping,
  50. options search.SearcherOptions) (search.Searcher, error) {
  51. field := q.FieldVal
  52. if q.FieldVal == "" {
  53. field = m.DefaultSearchField()
  54. }
  55. dist, err := geo.ParseDistance(q.Distance)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return searcher.NewGeoPointDistanceSearcher(i, q.Location[0], q.Location[1],
  60. dist, field, q.BoostVal.Value(), options)
  61. }
  62. func (q *GeoDistanceQuery) Validate() error {
  63. return nil
  64. }
  65. func (q *GeoDistanceQuery) UnmarshalJSON(data []byte) error {
  66. tmp := struct {
  67. Location interface{} `json:"location,omitempty"`
  68. Distance string `json:"distance,omitempty"`
  69. FieldVal string `json:"field,omitempty"`
  70. BoostVal *Boost `json:"boost,omitempty"`
  71. }{}
  72. err := json.Unmarshal(data, &tmp)
  73. if err != nil {
  74. return err
  75. }
  76. // now use our generic point parsing code from the geo package
  77. lon, lat, found := geo.ExtractGeoPoint(tmp.Location)
  78. if !found {
  79. return fmt.Errorf("geo location not in a valid format")
  80. }
  81. q.Location = []float64{lon, lat}
  82. q.Distance = tmp.Distance
  83. q.FieldVal = tmp.FieldVal
  84. q.BoostVal = tmp.BoostVal
  85. return nil
  86. }