Skip to content

Commit

Permalink
GOCBC-184: Implemented new FTS Geo & TermRange queries and sorting.
Browse files Browse the repository at this point in the history
Change-Id: I6ee13a1ee6cbd2450a32868d0d0c89507ee83731
Reviewed-on: http://review.couchbase.org/78072
Reviewed-by: Mike Goldsmith <goldsmith.mike@gmail.com>
Tested-by: Brett Lawson <brett19@gmail.com>
  • Loading branch information
brett19 committed May 12, 2017
1 parent f6b4cda commit 818028e
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 2 deletions.
88 changes: 88 additions & 0 deletions cbft/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,91 @@ type MatchNoneQuery struct {
func NewMatchNoneQuery(prefix string) *MatchNoneQuery {
return &MatchNoneQuery{}
}

// TermRangeQuery represents a FTS term range query.
type TermRangeQuery struct {
ftsQueryBase
}

// NewTermRangeQuery creates a new TermRangeQuery.
func NewTermRangeQuery(term string) *TermRangeQuery {
q := &TermRangeQuery{newFtsQueryBase()}
q.options["term"] = term
return q
}

// Field specifies the field for this query.
func (q *TermRangeQuery) Field(field string) *TermRangeQuery {
q.options["field"] = field
return q
}

// Min specifies the minimum value and inclusiveness for this range query.
func (q *TermRangeQuery) Min(min string, inclusive bool) *TermRangeQuery {
q.options["min"] = min
q.options["inclusive_min"] = inclusive
return q
}

// Max specifies the maximum value and inclusiveness for this range query.
func (q *TermRangeQuery) Max(max string, inclusive bool) *TermRangeQuery {
q.options["max"] = max
q.options["inclusive_max"] = inclusive
return q
}

// Boost specifies the boost for this query.
func (q *TermRangeQuery) Boost(boost float32) *TermRangeQuery {
q.options["boost"] = boost
return q
}

// GeoDistanceQuery represents a FTS geographical distance query.
type GeoDistanceQuery struct {
ftsQueryBase
}

// NewGeoDistanceQuery creates a new GeoDistanceQuery.
func NewGeoDistanceQuery(lat, lon float64, distance string) *GeoDistanceQuery {
q := &GeoDistanceQuery{newFtsQueryBase()}
q.options["location"] = []float64{lon, lat}
q.options["distance"] = distance
return q
}

// Field specifies the field for this query.
func (q *GeoDistanceQuery) Field(field string) *GeoDistanceQuery {
q.options["field"] = field
return q
}

// Boost specifies the boost for this query.
func (q *GeoDistanceQuery) Boost(boost float32) *GeoDistanceQuery {
q.options["boost"] = boost
return q
}

// GeoBoundingBoxQuery represents a FTS geographical bounding box query.
type GeoBoundingBoxQuery struct {
ftsQueryBase
}

// NewGeoBoundingBoxQuery creates a new GeoBoundingBoxQuery.
func NewGeoBoundingBoxQuery(tl_lat, tl_lon, br_lat, br_lon float64) *GeoBoundingBoxQuery {
q := &GeoDistanceQuery{newFtsQueryBase()}
q.options["top_left"] = []float64{tl_lon, tl_lat}
q.options["bottom_right"] = []float64{br_lon, br_lat}
return q
}

// Field specifies the field for this query.
func (q *GeoBoundingBoxQuery) Field(field string) *GeoBoundingBoxQuery {
q.options["field"] = field
return q
}

// Boost specifies the boost for this query.
func (q *GeoBoundingBoxQuery) Boost(boost float32) *GeoBoundingBoxQuery {
q.options["boost"] = boost
return q
}
121 changes: 121 additions & 0 deletions cbft/sorting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package cbft

import (
"encoding/json"
)

// *VOLATILE*
// FtsQuery represents an FTS query for a search query.
type FtsSort interface {
}

type ftsSortBase struct {
options map[string]interface{}
}

func newFtsSortBase() ftsSortBase {
return ftsSortBase{
options: make(map[string]interface{}),
}
}

// MarshalJSON marshal's this query to JSON for the FTS REST API.
func (q ftsSortBase) MarshalJSON() ([]byte, error) {
return json.Marshal(q.options)
}

// SearchSortScore represents a FTS score sort.
type SearchSortScore struct {
ftsSortBase
}

// NewSearchSortScore creates a new SearchSortScore.
func NewSearchSortScore() *SearchSortScore {
q := &SearchSortScore{newFtsSortBase()}
q.options["by"] = "score"
return q
}

// Descending specifies the ordering of the results.
func (q *SearchSortScore) Descending(descending bool) *SearchSortScore {
q.options["descending"] = descending
return q
}

// SearchSortScore represents a FTS Document ID sort.
type SearchSortId struct {
ftsSortBase
}

// NewSearchSortScore creates a new SearchSortScore.
func NewSearchSortId() *SearchSortId {
q := &SearchSortId{newFtsSortBase()}
q.options["by"] = "id"
return q
}

// Descending specifies the ordering of the results.
func (q *SearchSortId) Descending(descending bool) *SearchSortId {
q.options["descending"] = descending
return q
}

// SearchSortField represents a FTS field sort.
type SearchSortField struct {
ftsSortBase
}

// NewSearchSortField creates a new SearchSortField.
func NewSearchSortField(field string) *SearchSortField {
q := &SearchSortField{newFtsSortBase()}
q.options["by"] = "field"
q.options["field"] = field
return q
}

func (q *SearchSortField) Type(value string) *SearchSortField {
q.options["type"] = value
return q
}

func (q *SearchSortField) Mode(mode string) *SearchSortField {
q.options["mode"] = mode
return q
}

func (q *SearchSortField) Missing(missing string) *SearchSortField {
q.options["missing"] = missing
return q
}

// Descending specifies the ordering of the results.
func (q *SearchSortField) Descending(descending bool) *SearchSortField {
q.options["descending"] = descending
return q
}

// SearchSortGeoDistance represents a FTS geo sort.
type SearchSortGeoDistance struct {
ftsSortBase
}

// NewSearchSortGeoDistance creates a new SearchSortGeoDistance.
func NewSearchSortGeoDistance(field string, lat, lon float64) *SearchSortGeoDistance {
q := &SearchSortGeoDistance{newFtsSortBase()}
q.options["by"] = "geo_distance"
q.options["field"] = field
q.options["location"] = []float64{lon, lat}
return q
}

// Unit specifies the unit used for sorting
func (q *SearchSortGeoDistance) Unit(unit string) *SearchSortGeoDistance {
q.options["unit"] = unit
return q
}

// Descending specifies the ordering of the results.
func (q *SearchSortGeoDistance) Descending(descending bool) *SearchSortGeoDistance {
q.options["descending"] = descending
return q
}
4 changes: 2 additions & 2 deletions searchquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type searchQueryData struct {
Explain bool `json:"explain,omitempty"`
Highlight *searchQueryHighlightData `json:"highlight,omitempty"`
Fields []string `json:"fields,omitempty"`
Sort []string `json:"sort,omitempty"`
Sort []interface{} `json:"sort,omitempty"`
Facets map[string]interface{} `json:"facets,omitempty"`
Ctl *searchQueryCtlData `json:"ctl,omitempty"`
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func (sq *SearchQuery) Fields(fields ...string) *SearchQuery {
}

// Sort specifies a sorting order for the results. Only available in Couchbase Server 4.6+.
func (sq *SearchQuery) Sort(fields ...string) *SearchQuery {
func (sq *SearchQuery) Sort(fields ...interface{}) *SearchQuery {
sq.data.Sort = fields
return sq
}
Expand Down

0 comments on commit 818028e

Please sign in to comment.