-
Notifications
You must be signed in to change notification settings - Fork 668
/
search_geoshape.go
129 lines (107 loc) · 3.93 KB
/
search_geoshape.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
// 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 searcher
import (
"bytes"
"context"
"github.com/blevesearch/bleve/v2/geo"
"github.com/blevesearch/bleve/v2/search"
index "github.com/blevesearch/bleve_index_api"
"github.com/blevesearch/geo/geojson"
)
func NewGeoShapeSearcher(ctx context.Context, indexReader index.IndexReader, shape index.GeoJSON,
relation string, field string, boost float64,
options search.SearcherOptions) (search.Searcher, error) {
var err error
var spatialPlugin index.SpatialAnalyzerPlugin
// check for the spatial plugin from the index.
if sr, ok := indexReader.(index.SpatialIndexPlugin); ok {
spatialPlugin, _ = sr.GetSpatialAnalyzerPlugin("s2")
}
if spatialPlugin == nil {
// fallback to the default spatial plugin(s2).
spatialPlugin = geo.GetSpatialAnalyzerPlugin("s2")
}
// obtain the query tokens.
terms := spatialPlugin.GetQueryTokens(shape)
mSearcher, err := NewMultiTermSearcher(ctx, indexReader, terms,
field, boost, options, false)
if err != nil {
return nil, err
}
dvReader, err := indexReader.DocValueReader([]string{field})
if err != nil {
return nil, err
}
return NewFilteringSearcher(ctx, mSearcher,
buildRelationFilterOnShapes(ctx, dvReader, field, relation, shape)), nil
}
// Using the same term splitter slice used in the doc values in zap.
// TODO: This needs to be revisited whenever we change the zap
// implementation of doc values.
var termSeparatorSplitSlice = []byte{0xff}
func buildRelationFilterOnShapes(ctx context.Context, dvReader index.DocValueReader, field string,
relation string, shape index.GeoJSON) FilterFunc {
// this is for accumulating the shape's actual complete value
// spread across multiple docvalue visitor callbacks.
var dvShapeValue []byte
var startReading, finishReading bool
var reader *bytes.Reader
return func(d *search.DocumentMatch) bool {
var found bool
err := dvReader.VisitDocValues(d.IndexInternalID,
func(field string, term []byte) {
// only consider the values which are GlueBytes prefixed or
// if it had already started reading the shape bytes from previous callbacks.
if startReading || len(term) > geo.GlueBytesOffset {
if !startReading && bytes.Equal(geo.GlueBytes, term[:geo.GlueBytesOffset]) {
startReading = true
if bytes.Equal(geo.GlueBytes, term[len(term)-geo.GlueBytesOffset:]) {
term = term[:len(term)-geo.GlueBytesOffset]
finishReading = true
}
dvShapeValue = append(dvShapeValue, term[geo.GlueBytesOffset:]...)
} else if startReading && !finishReading {
if len(term) > geo.GlueBytesOffset &&
bytes.Equal(geo.GlueBytes, term[len(term)-geo.GlueBytesOffset:]) {
term = term[:len(term)-geo.GlueBytesOffset]
finishReading = true
}
term = append(termSeparatorSplitSlice, term...)
dvShapeValue = append(dvShapeValue, term...)
}
// apply the filter once the entire docvalue is finished reading.
if finishReading {
v, err := geojson.FilterGeoShapesOnRelation(shape,
dvShapeValue, relation, &reader)
if err == nil && v {
found = true
}
dvShapeValue = dvShapeValue[:0]
startReading = false
finishReading = false
}
}
})
// use ctx to aggregate the bytes read
if err == nil && found {
bytes := dvReader.BytesRead()
if bytes > 0 {
search.RecordSearchCost(ctx, "add", bytes)
}
return found
}
return false
}
}