Skip to content
162 changes: 162 additions & 0 deletions document/field_geoshape_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// 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 document

import (
"reflect"

"github.com/blevesearch/bleve/v2/geo"
"github.com/blevesearch/bleve/v2/geov2"
"github.com/blevesearch/bleve/v2/size"
index "github.com/blevesearch/bleve_index_api"
"github.com/blevesearch/geo/geojson"
)

var reflectStaticSizeGeoShapeV2Field int

func init() {
var f GeoShapeV2Field
reflectStaticSizeGeoShapeV2Field = int(reflect.TypeOf(f).Size())
}

type GeoShapeV2Field struct {
name string

shape index.GeoJSON
inner []uint64
cross []uint64
scoreInner uint64
scoreCross uint64
bBoxBytes []byte
shapeBytes []byte

options index.FieldIndexingOptions
}

func (f *GeoShapeV2Field) Name() string {
return f.name
}

func (f *GeoShapeV2Field) ArrayPositions() []uint64 {
return nil
}

func (f *GeoShapeV2Field) Options() index.FieldIndexingOptions {
return f.options
}

func (f *GeoShapeV2Field) Analyze() {
f.inner, f.cross = f.shape.IndexCells()
f.scoreInner = geov2.CalcCellsScore(f.inner)
f.scoreCross = geov2.CalcCellsScore(f.cross)

if bBox, ok := f.shape.BoundingBox().(*geojson.Envelope); ok {
bBoxBytes, err := bBox.Marshal()
if err != nil {
return
}
f.bBoxBytes = bBoxBytes
}
}

func (f *GeoShapeV2Field) Value() []byte {
return []byte{}
}

func (f *GeoShapeV2Field) NumPlainTextBytes() uint64 {
return 0
}

func (f *GeoShapeV2Field) Size() int {
return reflectStaticSizeGeoShapeV2Field + size.SizeOfPtr +
len(f.name) +
len(f.inner)*size.SizeOfUint64 +
len(f.cross)*size.SizeOfUint64 +
len(f.bBoxBytes) +
len(f.shapeBytes)
}

func (f *GeoShapeV2Field) EncodedFieldType() byte {
return 'o'
}

func (f *GeoShapeV2Field) AnalyzedLength() int {
return 0
}

func (f *GeoShapeV2Field) AnalyzedTokenFrequencies() index.TokenFrequencies {
return nil
}

func (f *GeoShapeV2Field) InnerCells() []uint64 {
return f.inner
}

func (f *GeoShapeV2Field) CrossCells() []uint64 {
return f.cross
}

func (f *GeoShapeV2Field) EncodedBoundingBox() []byte {
return f.bBoxBytes
}

func (f *GeoShapeV2Field) EncodedShape() []byte {
return f.shapeBytes
}

func (f *GeoShapeV2Field) Scores() (uint64, uint64) {
return f.scoreInner, f.scoreCross
}

func NewGeoShapeV2FieldFromShapeWithIndexingOptions(name string, geoShape *geojson.GeoShape,
options index.FieldIndexingOptions) *GeoShapeV2Field {

var shape index.GeoJSON
var shapeBytes []byte
var err error

if geoShape.Type == geo.CircleType {
shape, shapeBytes, err = geo.NewGeoCircleShape(geoShape.Center,
geoShape.Radius)
} else {
shape, shapeBytes, err = geo.NewGeoJsonShape(geoShape.Coordinates,
geoShape.Type)
}
if err != nil {
return nil
}

return &GeoShapeV2Field{
name: name,
shape: shape,
options: options,
shapeBytes: shapeBytes,
}
}

func NewGeometryCollectionV2FieldFromShapesWithIndexingOptions(name string,
geoShapes []*geojson.GeoShape, options index.FieldIndexingOptions) *GeoShapeV2Field {
shape, shapeBytes, err := geo.NewGeometryCollectionFromShapes(geoShapes)
if err != nil {
return nil
}

return &GeoShapeV2Field{
name: name,
shape: shape,
options: options,
shapeBytes: shapeBytes,
}
}
60 changes: 60 additions & 0 deletions geo/geo_s2plugin_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,21 @@ func (p *Point) QueryTokens(s *S2SpatialAnalyzerPlugin) []string {
return nil
}

func (p *Point) IndexCells() ([]uint64, []uint64) {
// placeholder implementation
return nil, nil
}

func (p *Point) QueryCells() ([]uint64, []uint64) {
// placeholder implementation
return nil, nil
}

func (p *Point) BoundingBox() index.GeoJSON {
// placeholder implementation
return nil
}

//----------------------------------------------------------------------------------

type boundedRectangle struct {
Expand Down Expand Up @@ -293,6 +308,21 @@ func (br *boundedRectangle) QueryTokens(s *S2SpatialAnalyzerPlugin) []string {
return geojson.StripCoveringTerms(terms)
}

func (br *boundedRectangle) IndexCells() ([]uint64, []uint64) {
// placeholder implementation
return nil, nil
}

func (br *boundedRectangle) QueryCells() ([]uint64, []uint64) {
// placeholder implementation
return nil, nil
}

func (br *boundedRectangle) BoundingBox() index.GeoJSON {
// placeholder implementation
return nil
}

//----------------------------------------------------------------------------------

type boundedPolygon struct {
Expand Down Expand Up @@ -341,6 +371,21 @@ func (bp *boundedPolygon) QueryTokens(s *S2SpatialAnalyzerPlugin) []string {
return geojson.StripCoveringTerms(terms)
}

func (bp *boundedPolygon) IndexCells() ([]uint64, []uint64) {
// placeholder implementation
return nil, nil
}

func (bp *boundedPolygon) QueryCells() ([]uint64, []uint64) {
// placeholder implementation
return nil, nil
}

func (bp *boundedPolygon) BoundingBox() index.GeoJSON {
// placeholder implementation
return nil
}

//----------------------------------------------------------------------------------

type pointDistance struct {
Expand Down Expand Up @@ -389,6 +434,21 @@ func (pd *pointDistance) QueryTokens(s *S2SpatialAnalyzerPlugin) []string {
return geojson.StripCoveringTerms(terms)
}

func (pd *pointDistance) IndexCells() ([]uint64, []uint64) {
// placeholder implementation
return nil, nil
}

func (pd *pointDistance) QueryCells() ([]uint64, []uint64) {
// placeholder implementation
return nil, nil
}

func (pd *pointDistance) BoundingBox() index.GeoJSON {
// placeholder implementation
return nil
}

// ------------------------------------------------------------------------

// NewGeometryCollection instantiate a geometrycollection
Expand Down
49 changes: 49 additions & 0 deletions geov2/cells.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 "github.com/blevesearch/geo/s2"

// getCellSearchBounds takes a raw uint64 S2 cell ID and returns the
// minimum and maximum uint64 values defining its absolute spatial range.
func getCellSearchBounds(cellUint uint64) (min uint64, max uint64) {
cellID := s2.CellID(cellUint)

rangeMin := cellID.RangeMin()
rangeMax := cellID.RangeMax()

return uint64(rangeMin), uint64(rangeMax)
}

// Returns the level of the given S2 cell ID
func getCellLevel(cell uint64) uint64 {
return uint64(s2.CellID(cell).Level())
}

// Returns the parent cell ID of the given S2 cell ID at the specified level
func getParentCell(cell uint64, level int) uint64 {
return uint64(s2.CellID(cell).Parent(level))
}

// CalcCellsScore returns the total area of the given S2 cells
// in level 16 cell units (maxCellLevel in the geo repo's region coverer
// configuration - see geo/geojson/geojson_v2.go)
func CalcCellsScore(cells []uint64) uint64 {
var score uint64
for _, cell := range cells {
score += calcScore(0, getCellLevel(cell))
}
return score
}
Loading
Loading