forked from blevesearch/bleve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo_shape.go
137 lines (114 loc) · 3.8 KB
/
geo_shape.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2022 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package query
import (
"context"
"encoding/json"
"github.com/blevesearch/bleve/v2/geo"
"github.com/blevesearch/bleve/v2/mapping"
"github.com/blevesearch/bleve/v2/search"
"github.com/blevesearch/bleve/v2/search/searcher"
index "github.com/blevesearch/bleve_index_api"
)
type Geometry struct {
Shape index.GeoJSON `json:"shape"`
Relation string `json:"relation"`
}
type GeoShapeQuery struct {
Geometry Geometry `json:"geometry"`
FieldVal string `json:"field,omitempty"`
BoostVal *Boost `json:"boost,omitempty"`
}
// NewGeoShapeQuery creates a geoshape query for the
// given shape type. This method can be used for
// creating geoshape queries for shape types like: point,
// linestring, polygon, multipoint, multilinestring,
// multipolygon and envelope.
func NewGeoShapeQuery(coordinates [][][][]float64, typ,
relation string) (*GeoShapeQuery, error) {
s, _, err := geo.NewGeoJsonShape(coordinates, typ)
if err != nil {
return nil, err
}
return &GeoShapeQuery{Geometry: Geometry{Shape: s,
Relation: relation}}, nil
}
// NewGeoShapeCircleQuery creates a geoshape query for the
// given center point and the radius. Radius formats supported:
// "5in" "5inch" "7yd" "7yards" "9ft" "9feet" "11km" "11kilometers"
// "3nm" "3nauticalmiles" "13mm" "13millimeters" "15cm" "15centimeters"
// "17mi" "17miles" "19m" "19meters" If the unit cannot be determined,
// the entire string is parsed and the unit of meters is assumed.
func NewGeoShapeCircleQuery(coordinates []float64, radius,
relation string) (*GeoShapeQuery, error) {
s, _, err := geo.NewGeoCircleShape(coordinates, radius)
if err != nil {
return nil, err
}
return &GeoShapeQuery{Geometry: Geometry{Shape: s,
Relation: relation}}, nil
}
// NewGeometryCollectionQuery creates a geoshape query for the
// given geometrycollection coordinates and types.
func NewGeometryCollectionQuery(coordinates [][][][][]float64, types []string,
relation string) (*GeoShapeQuery, error) {
s, _, err := geo.NewGeometryCollection(coordinates, types)
if err != nil {
return nil, err
}
return &GeoShapeQuery{Geometry: Geometry{Shape: s,
Relation: relation}}, nil
}
func (q *GeoShapeQuery) SetBoost(b float64) {
boost := Boost(b)
q.BoostVal = &boost
}
func (q *GeoShapeQuery) Boost() float64 {
return q.BoostVal.Value()
}
func (q *GeoShapeQuery) SetField(f string) {
q.FieldVal = f
}
func (q *GeoShapeQuery) Field() string {
return q.FieldVal
}
func (q *GeoShapeQuery) Searcher(ctx context.Context, i index.IndexReader,
m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
field := q.FieldVal
if q.FieldVal == "" {
field = m.DefaultSearchField()
}
ctx = context.WithValue(ctx, search.QueryTypeKey, search.Geo)
return searcher.NewGeoShapeSearcher(ctx, i, q.Geometry.Shape, q.Geometry.Relation, field,
q.BoostVal.Value(), options)
}
func (q *GeoShapeQuery) Validate() error {
return nil
}
func (q *Geometry) UnmarshalJSON(data []byte) error {
tmp := struct {
Shape json.RawMessage `json:"shape"`
Relation string `json:"relation"`
}{}
err := json.Unmarshal(data, &tmp)
if err != nil {
return err
}
q.Shape, err = geo.ParseGeoJSONShape(tmp.Shape)
if err != nil {
return err
}
q.Relation = tmp.Relation
return nil
}