Skip to content

Commit

Permalink
GeoDistanceSort: Add missing ignore_unmapped option
Browse files Browse the repository at this point in the history
Close #1523
  • Loading branch information
olivere committed Jul 30, 2021
1 parent b56abbe commit 1451719
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
33 changes: 23 additions & 10 deletions sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,17 @@ func (s *FieldSort) Source() (interface{}, error) {
// See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-sort.html#_geo_distance_sorting.
type GeoDistanceSort struct {
Sorter
fieldName string
points []*GeoPoint
geohashes []string
distanceType *string
unit string
ascending bool
sortMode *string
nestedFilter Query
nestedPath *string
nestedSort *NestedSort
fieldName string
points []*GeoPoint
geohashes []string
distanceType *string
unit string
ignoreUnmapped *bool
ascending bool
sortMode *string
nestedFilter Query
nestedPath *string
nestedSort *NestedSort
}

// NewGeoDistanceSort creates a new sorter for geo distances.
Expand Down Expand Up @@ -378,6 +379,15 @@ func (s *GeoDistanceSort) Unit(unit string) *GeoDistanceSort {
return s
}

// IgnoreUnmapped indicates whether the unmapped field should be treated as
// a missing value. Setting it to true is equivalent to specifying an
// unmapped_type in the field sort. The default is false (unmapped field
// causes the search to fail).
func (s *GeoDistanceSort) IgnoreUnmapped(ignoreUnmapped bool) *GeoDistanceSort {
s.ignoreUnmapped = &ignoreUnmapped
return s
}

// GeoDistance is an alias for DistanceType.
func (s *GeoDistanceSort) GeoDistance(geoDistance string) *GeoDistanceSort {
return s.DistanceType(geoDistance)
Expand Down Expand Up @@ -439,6 +449,9 @@ func (s *GeoDistanceSort) Source() (interface{}, error) {
if s.unit != "" {
x["unit"] = s.unit
}
if s.ignoreUnmapped != nil {
x["ignore_unmapped"] = *s.ignoreUnmapped
}
if s.distanceType != nil {
x["distance_type"] = *s.distanceType
}
Expand Down
3 changes: 2 additions & 1 deletion sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func TestGeoDistanceSort(t *testing.T) {
Point(-70, 40).
Order(true).
Unit("km").
IgnoreUnmapped(true).
SortMode("min").
GeoDistance("plane")
src, err := builder.Source()
Expand All @@ -176,7 +177,7 @@ func TestGeoDistanceSort(t *testing.T) {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"_geo_distance":{"distance_type":"plane","mode":"min","order":"asc","pin.location":[{"lat":-70,"lon":40}],"unit":"km"}}`
expected := `{"_geo_distance":{"distance_type":"plane","ignore_unmapped":true,"mode":"min","order":"asc","pin.location":[{"lat":-70,"lon":40}],"unit":"km"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
Expand Down

0 comments on commit 1451719

Please sign in to comment.