improved geo searcher unit tests
also added flag for bounding box searcher to optionally not check boundaries. this is useful when other searchers are going to check every point anyway by some other criteria.
This commit is contained in:
parent
f44630a205
commit
6507e31787
|
@ -72,11 +72,11 @@ func (q *GeoBoundingBoxQuery) Searcher(i index.IndexReader, m mapping.IndexMappi
|
|||
if q.BottomRight.Lon < q.TopLeft.Lon {
|
||||
// cross date line, rewrite as two parts
|
||||
|
||||
leftSearcher, err := searcher.NewGeoBoundingBoxSearcher(i, -180, q.BottomRight.Lat, q.BottomRight.Lon, q.TopLeft.Lat, field, q.BoostVal.Value(), options)
|
||||
leftSearcher, err := searcher.NewGeoBoundingBoxSearcher(i, -180, q.BottomRight.Lat, q.BottomRight.Lon, q.TopLeft.Lat, field, q.BoostVal.Value(), options, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rightSearcher, err := searcher.NewGeoBoundingBoxSearcher(i, q.TopLeft.Lon, q.BottomRight.Lat, 180, q.TopLeft.Lat, field, q.BoostVal.Value(), options)
|
||||
rightSearcher, err := searcher.NewGeoBoundingBoxSearcher(i, q.TopLeft.Lon, q.BottomRight.Lat, 180, q.TopLeft.Lat, field, q.BoostVal.Value(), options, true)
|
||||
if err != nil {
|
||||
_ = leftSearcher.Close()
|
||||
return nil, err
|
||||
|
@ -85,7 +85,7 @@ func (q *GeoBoundingBoxQuery) Searcher(i index.IndexReader, m mapping.IndexMappi
|
|||
return searcher.NewDisjunctionSearcher(i, []search.Searcher{leftSearcher, rightSearcher}, 0, options)
|
||||
}
|
||||
|
||||
return searcher.NewGeoBoundingBoxSearcher(i, q.TopLeft.Lon, q.BottomRight.Lat, q.BottomRight.Lon, q.TopLeft.Lat, field, q.BoostVal.Value(), options)
|
||||
return searcher.NewGeoBoundingBoxSearcher(i, q.TopLeft.Lon, q.BottomRight.Lat, q.BottomRight.Lon, q.TopLeft.Lat, field, q.BoostVal.Value(), options, true)
|
||||
}
|
||||
|
||||
func (q *GeoBoundingBoxQuery) Validate() error {
|
||||
|
|
|
@ -15,8 +15,6 @@
|
|||
package searcher
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/blevesearch/bleve/document"
|
||||
"github.com/blevesearch/bleve/geo"
|
||||
"github.com/blevesearch/bleve/index"
|
||||
|
@ -38,7 +36,7 @@ type GeoBoundingBoxSearcher struct {
|
|||
searcher *DisjunctionSearcher
|
||||
}
|
||||
|
||||
func NewGeoBoundingBoxSearcher(indexReader index.IndexReader, minLon, minLat, maxLon, maxLat float64, field string, boost float64, options search.SearcherOptions) (*GeoBoundingBoxSearcher, error) {
|
||||
func NewGeoBoundingBoxSearcher(indexReader index.IndexReader, minLon, minLat, maxLon, maxLat float64, field string, boost float64, options search.SearcherOptions, checkBoundaries bool) (*GeoBoundingBoxSearcher, error) {
|
||||
var openedSearchers []search.Searcher
|
||||
cleanupOpenedSearchers := func() {
|
||||
for _, s := range openedSearchers {
|
||||
|
@ -64,42 +62,46 @@ func NewGeoBoundingBoxSearcher(indexReader index.IndexReader, minLon, minLat, ma
|
|||
cleanupOpenedSearchers()
|
||||
return nil, err
|
||||
}
|
||||
if r.boundary {
|
||||
if r.boundary && checkBoundaries {
|
||||
termsOnBoundary = append(termsOnBoundary, ts)
|
||||
} else {
|
||||
termsNotOnBoundary = append(termsNotOnBoundary, ts)
|
||||
}
|
||||
openedSearchers = append(openedSearchers)
|
||||
}
|
||||
onBoundarySearcher, err := NewDisjunctionSearcher(indexReader, termsOnBoundary, 0, options)
|
||||
if err != nil {
|
||||
cleanupOpenedSearchers()
|
||||
return nil, err
|
||||
}
|
||||
filterOnBoundarySearcher := NewFilteringSearcher(onBoundarySearcher, func(d *search.DocumentMatch) bool {
|
||||
var lon, lat float64
|
||||
var found bool
|
||||
err = indexReader.DocumentVisitFieldTerms(d.IndexInternalID, []string{field}, func(field string, term []byte) {
|
||||
// only consider the values which are shifted 0
|
||||
prefixCoded := numeric.PrefixCoded(term)
|
||||
var shift uint
|
||||
shift, err = prefixCoded.Shift()
|
||||
if err == nil && shift == 0 {
|
||||
var i64 int64
|
||||
i64, err = prefixCoded.Int64()
|
||||
if err == nil {
|
||||
lon = geo.MortonUnhashLon(uint64(i64))
|
||||
lat = geo.MortonUnhashLat(uint64(i64))
|
||||
found = true
|
||||
}
|
||||
}
|
||||
})
|
||||
if err == nil && found {
|
||||
return geo.BoundingBoxContains(lon, lat, minLon, minLat, maxLon, maxLat)
|
||||
|
||||
var filterOnBoundarySearcher search.Searcher
|
||||
if len(termsOnBoundary) > 0 {
|
||||
onBoundarySearcher, err := NewDisjunctionSearcher(indexReader, termsOnBoundary, 0, options)
|
||||
if err != nil {
|
||||
cleanupOpenedSearchers()
|
||||
return nil, err
|
||||
}
|
||||
return false
|
||||
})
|
||||
openedSearchers = append(openedSearchers, filterOnBoundarySearcher)
|
||||
filterOnBoundarySearcher = NewFilteringSearcher(onBoundarySearcher, func(d *search.DocumentMatch) bool {
|
||||
var lon, lat float64
|
||||
var found bool
|
||||
err = indexReader.DocumentVisitFieldTerms(d.IndexInternalID, []string{field}, func(field string, term []byte) {
|
||||
// only consider the values which are shifted 0
|
||||
prefixCoded := numeric.PrefixCoded(term)
|
||||
var shift uint
|
||||
shift, err = prefixCoded.Shift()
|
||||
if err == nil && shift == 0 {
|
||||
var i64 int64
|
||||
i64, err = prefixCoded.Int64()
|
||||
if err == nil {
|
||||
lon = geo.MortonUnhashLon(uint64(i64))
|
||||
lat = geo.MortonUnhashLat(uint64(i64))
|
||||
found = true
|
||||
}
|
||||
}
|
||||
})
|
||||
if err == nil && found {
|
||||
return geo.BoundingBoxContains(lon, lat, minLon, minLat, maxLon, maxLat)
|
||||
}
|
||||
return false
|
||||
})
|
||||
openedSearchers = append(openedSearchers, filterOnBoundarySearcher)
|
||||
}
|
||||
notOnBoundarySearcher, err := NewDisjunctionSearcher(indexReader, termsNotOnBoundary, 0, options)
|
||||
if err != nil {
|
||||
cleanupOpenedSearchers()
|
||||
|
@ -107,6 +109,11 @@ func NewGeoBoundingBoxSearcher(indexReader index.IndexReader, minLon, minLat, ma
|
|||
}
|
||||
openedSearchers = append(openedSearchers, notOnBoundarySearcher)
|
||||
|
||||
// if there is no filterOnBoundary searcher, just return the notOnBoundarySearcher
|
||||
if filterOnBoundarySearcher == nil {
|
||||
rv.searcher = notOnBoundarySearcher
|
||||
return rv, nil
|
||||
}
|
||||
rv.searcher, err = NewDisjunctionSearcher(indexReader, []search.Searcher{filterOnBoundarySearcher, notOnBoundarySearcher}, 0, options)
|
||||
if err != nil {
|
||||
cleanupOpenedSearchers()
|
||||
|
@ -192,7 +199,3 @@ func newGeoRange(lower uint64, res uint, level uint, boundary bool) *geoRange {
|
|||
cell: numeric.MustNewPrefixCodedInt64(int64(lower), res),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *geoRange) Compare(other *geoRange) int {
|
||||
return bytes.Compare(r.cell, other.cell)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,21 @@
|
|||
// Copyright (c) 2017 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 (
|
||||
"log"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/blevesearch/bleve/document"
|
||||
|
@ -12,7 +26,22 @@ import (
|
|||
)
|
||||
|
||||
func TestGeoBoundingBox(t *testing.T) {
|
||||
i := setup(t)
|
||||
|
||||
tests := []struct {
|
||||
minLon float64
|
||||
minLat float64
|
||||
maxLon float64
|
||||
maxLat float64
|
||||
field string
|
||||
want []string
|
||||
}{
|
||||
{10.001, 10.001, 20.002, 20.002, "loc", nil},
|
||||
{0.001, 0.001, 0.002, 0.002, "loc", []string{"a"}},
|
||||
{0.001, 0.001, 1.002, 1.002, "loc", []string{"a", "b"}},
|
||||
{0.001, 0.001, 9.002, 9.002, "loc", []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}},
|
||||
}
|
||||
|
||||
i := setupGeo(t)
|
||||
indexReader, err := i.Reader()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
|
@ -24,28 +53,39 @@ func TestGeoBoundingBox(t *testing.T) {
|
|||
}
|
||||
}()
|
||||
|
||||
gbs, err := NewGeoBoundingBoxSearcher(indexReader, 0.001, 0.001, 0.002, 0.002, "loc", 1.0, search.SearcherOptions{})
|
||||
for _, test := range tests {
|
||||
got, err := testGeoBoundingBoxSearch(indexReader, test.minLon, test.minLat, test.maxLon, test.maxLat, test.field)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, test.want) {
|
||||
t.Errorf("expected %v, got %v for %f %f %f %f %s", test.want, got, test.minLon, test.minLat, test.maxLon, test.maxLat, test.field)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func testGeoBoundingBoxSearch(i index.IndexReader, minLon, minLat, maxLon, maxLat float64, field string) ([]string, error) {
|
||||
var rv []string
|
||||
gbs, err := NewGeoBoundingBoxSearcher(i, minLon, minLat, maxLon, maxLat, field, 1.0, search.SearcherOptions{}, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
ctx := &search.SearchContext{
|
||||
DocumentMatchPool: search.NewDocumentMatchPool(gbs.DocumentMatchPoolSize(), 0),
|
||||
}
|
||||
docMatch, err := gbs.Next(ctx)
|
||||
for docMatch != nil && err == nil {
|
||||
if docMatch == nil {
|
||||
log.Printf("nil docmatch")
|
||||
} else {
|
||||
log.Printf("got doc match: %s", docMatch.IndexInternalID)
|
||||
}
|
||||
rv = append(rv, string(docMatch.IndexInternalID))
|
||||
docMatch, err = gbs.Next(ctx)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
func setup(t *testing.T) index.Index {
|
||||
func setupGeo(t *testing.T) index.Index {
|
||||
|
||||
analysisQueue := index.NewAnalysisQueue(1)
|
||||
i, err := upsidedown.NewUpsideDownCouch(
|
||||
|
|
|
@ -51,11 +51,11 @@ func NewGeoPointDistanceSearcher(indexReader index.IndexReader, centerLon, cente
|
|||
if bottomRightLon < topLeftLon {
|
||||
// cross date line, rewrite as two parts
|
||||
|
||||
leftSearcher, err := NewGeoBoundingBoxSearcher(indexReader, -180, bottomRightLat, bottomRightLon, topLeftLat, field, boost, options)
|
||||
leftSearcher, err := NewGeoBoundingBoxSearcher(indexReader, -180, bottomRightLat, bottomRightLon, topLeftLat, field, boost, options, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rightSearcher, err := NewGeoBoundingBoxSearcher(indexReader, topLeftLon, bottomRightLat, 180, topLeftLat, field, boost, options)
|
||||
rightSearcher, err := NewGeoBoundingBoxSearcher(indexReader, topLeftLon, bottomRightLat, 180, topLeftLat, field, boost, options, false)
|
||||
if err != nil {
|
||||
_ = leftSearcher.Close()
|
||||
return nil, err
|
||||
|
@ -71,7 +71,7 @@ func NewGeoPointDistanceSearcher(indexReader index.IndexReader, centerLon, cente
|
|||
|
||||
// build geoboundinggox searcher for that bounding box
|
||||
var err error
|
||||
boxSearcher, err = NewGeoBoundingBoxSearcher(indexReader, topLeftLon, bottomRightLat, bottomRightLon, topLeftLat, field, boost, options)
|
||||
boxSearcher, err = NewGeoBoundingBoxSearcher(indexReader, topLeftLon, bottomRightLat, bottomRightLon, topLeftLat, field, boost, options, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
// Copyright (c) 2017 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 (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/blevesearch/bleve/index"
|
||||
"github.com/blevesearch/bleve/search"
|
||||
)
|
||||
|
||||
func TestGeoPointDistanceSearcher(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
centerLon float64
|
||||
centerLat float64
|
||||
dist float64
|
||||
field string
|
||||
want []string
|
||||
}{
|
||||
// approx 110567m per degree at equator
|
||||
{0.0, 0.0, 0, "loc", nil},
|
||||
{0.0, 0.0, 110567, "loc", []string{"a"}},
|
||||
{0.0, 0.0, 2 * 110567, "loc", []string{"a", "b"}},
|
||||
// stretching our approximation here
|
||||
{0.0, 0.0, 15 * 110567, "loc", []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}},
|
||||
}
|
||||
|
||||
i := setupGeo(t)
|
||||
indexReader, err := i.Reader()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
defer func() {
|
||||
err = indexReader.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
for _, test := range tests {
|
||||
got, err := testGeoPointDistanceSearch(indexReader, test.centerLon, test.centerLat, test.dist, test.field)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, test.want) {
|
||||
t.Errorf("expected %v, got %v for %f %f %f %s", test.want, got, test.centerLon, test.centerLat, test.dist, test.field)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func testGeoPointDistanceSearch(i index.IndexReader, centerLon, centerLat, dist float64, field string) ([]string, error) {
|
||||
var rv []string
|
||||
gds, err := NewGeoPointDistanceSearcher(i, centerLon, centerLat, dist, field, 1.0, search.SearcherOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx := &search.SearchContext{
|
||||
DocumentMatchPool: search.NewDocumentMatchPool(gds.DocumentMatchPoolSize(), 0),
|
||||
}
|
||||
docMatch, err := gds.Next(ctx)
|
||||
for docMatch != nil && err == nil {
|
||||
rv = append(rv, string(docMatch.IndexInternalID))
|
||||
docMatch, err = gds.Next(ctx)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rv, nil
|
||||
}
|
Loading…
Reference in New Issue