snapshot of https://github.com/blevesearch/bleve.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
Coverage / coverage (push) Failing after 6s
Tests / test (1.24.x, ubuntu-latest) (push) Failing after 42s
Tests / test (1.26.x, ubuntu-latest) (push) Failing after 38s
Tests / test (1.25.x, ubuntu-latest) (push) Failing after 35s
Tests / test (1.25.x, windows-latest) (push) Has been cancelled
Tests / test (1.26.x, macos-latest) (push) Has been cancelled
Tests / test (1.26.x, windows-latest) (push) Has been cancelled
Tests / test (1.24.x, macos-latest) (push) Has been cancelled
Tests / test (1.24.x, windows-latest) (push) Has been cancelled
Tests / test (1.25.x, macos-latest) (push) Has been cancelled

This commit is contained in:
upstream-mirror
2026-09-15 22:27:16 +05:00
commit f8d3d9f0cf
797 changed files with 159570 additions and 0 deletions

141
geov2/query_contains.go Normal file
View File

@@ -0,0 +1,141 @@
// Copyright (c) 2026 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 geov2
import (
"bytes"
"github.com/blevesearch/bleve/v2/util"
index "github.com/blevesearch/bleve_index_api"
"github.com/blevesearch/geo/geojson"
segment "github.com/blevesearch/scorch_segment_api/v2"
)
type containsQuery struct {
innerCells []uint64
crossCells []uint64
shape index.GeoJSON
bBox index.GeoJSON
score uint64
}
func NewContainsQuery(shape index.GeoJSON) Query {
inner, cross := shape.QueryCells()
score := CalcCellsScore(inner) + CalcCellsScore(cross)
return &containsQuery{
innerCells: inner,
crossCells: cross,
shape: shape,
bBox: shape.BoundingBox(),
score: score,
}
}
func (cq *containsQuery) Evaluate(geoData segment.GeoShapeV2Data) *util.Bitset {
numDocs := int(geoData.NumDocs())
exclude := geoData.Excluded()
// create bitsets for hits and maybeHits providing exclude to the bitset
// which will make it impossible to set those bits
hits := util.NewBitset(numDocs, exclude)
maybeHits := util.NewBitset(numDocs, exclude)
// failsafe for a degenerate query shape that produced no cells
if cq.score == 0 {
return hits
}
// obtain zeroed score arrays from the segment-level pool and return
// them once the evaluation is done
innerScores := geoData.GetScoreMap()
crossScores := geoData.GetScoreMap()
defer geoData.PutScoreMap(innerScores)
defer geoData.PutScoreMap(crossScores)
// create an evaluator instance to scan the query cells against the index cells
evaluator := NewQueryEvaluator(cq, geoData)
// scan and score the overlap of all query cells with all index cells
evaluator.rangeScanInner(innerScores, crossScores)
evaluator.rangeScanCross(innerScores, crossScores)
// if all of the query cells are contained within the inner index cells
// then we have a guaranteed hit, if they are contained within both the inner
// and cross index cells then we have a maybe hit, otherwise we have no hit
forEachScoredDoc(innerScores, crossScores, func(id uint32, inner, cross uint64) {
docNum := int(id)
if inner == cq.score {
hits.Add(docNum)
} else if inner+cross == cq.score {
maybeHits.Add(docNum)
}
})
var reader *bytes.Reader
// filter out any maybeHits that do not have a bounding box that
// contains the query bounding box
boxFilter := func(docNum int) {
docBBoxBytes, err := geoData.BoundingBox(uint32(docNum))
if docBBoxBytes == nil || err != nil {
return
}
docBBox, err := geojson.ExtractShapesFromBytes(docBBoxBytes, &reader, nil)
if err != nil {
return
}
if ok, err := docBBox.Contains(cq.bBox); err == nil && !ok {
maybeHits.Remove(docNum)
}
}
maybeHits.Iterate(boxFilter)
// filter out any maybeHits that do not have a shape that
// contains the query shape
shapeFilter := func(docNum int) {
docShapeBytes, err := geoData.Shape(uint32(docNum))
if docShapeBytes == nil || err != nil {
return
}
docShape, err := geojson.ExtractShapesFromBytes(docShapeBytes, &reader, nil)
if err != nil {
return
}
if ok, err := docShape.Contains(cq.shape); err == nil && ok {
hits.Add(docNum)
}
}
maybeHits.Iterate(shapeFilter)
return hits
}
func (cq *containsQuery) InnerCells() []uint64 {
return cq.innerCells
}
func (cq *containsQuery) CrossCells() []uint64 {
return cq.crossCells
}