@@ -81,6 +81,7 @@ public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder>
8181 private static final ParseField DISTANCE_TYPE_FIELD = new ParseField ("distance_type" );
8282 private static final ParseField VALIDATION_METHOD_FIELD = new ParseField ("validation_method" );
8383 private static final ParseField SORTMODE_FIELD = new ParseField ("mode" , "sort_mode" );
84+ private static final ParseField IGNORE_UNMAPPED = new ParseField ("ignore_unmapped" );
8485
8586 private final String fieldName ;
8687 private final List <GeoPoint > points = new ArrayList <>();
@@ -97,6 +98,8 @@ public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder>
9798
9899 private GeoValidationMethod validation = DEFAULT_VALIDATION ;
99100
101+ private boolean ignoreUnmapped = false ;
102+
100103 /**
101104 * Constructs a new distance based sort on a geo point like field.
102105 *
@@ -152,6 +155,7 @@ public GeoDistanceSortBuilder(String fieldName, String ... geohashes) {
152155 this .nestedPath = original .nestedPath ;
153156 this .validation = original .validation ;
154157 this .nestedSort = original .nestedSort ;
158+ this .ignoreUnmapped = original .ignoreUnmapped ;
155159 }
156160
157161 /**
@@ -171,6 +175,9 @@ public GeoDistanceSortBuilder(StreamInput in) throws IOException {
171175 nestedSort = in .readOptionalWriteable (NestedSortBuilder ::new );
172176 }
173177 validation = GeoValidationMethod .readFromStream (in );
178+ if (in .getVersion ().onOrAfter (Version .V_6_4_0 )) {
179+ ignoreUnmapped = in .readBoolean ();
180+ }
174181 }
175182
176183 @ Override
@@ -187,6 +194,9 @@ public void writeTo(StreamOutput out) throws IOException {
187194 out .writeOptionalWriteable (nestedSort );
188195 }
189196 validation .writeTo (out );
197+ if (out .getVersion ().onOrAfter (Version .V_6_4_0 )) {
198+ out .writeBoolean (ignoreUnmapped );
199+ }
190200 }
191201
192202 /**
@@ -374,6 +384,18 @@ public GeoDistanceSortBuilder setNestedSort(final NestedSortBuilder nestedSort)
374384 return this ;
375385 }
376386
387+ /**
388+ * Returns true if unmapped geo fields should be treated as located at an infinite distance
389+ */
390+ public boolean ignoreUnmapped () {
391+ return ignoreUnmapped ;
392+ }
393+
394+ public GeoDistanceSortBuilder ignoreUnmapped (boolean ignoreUnmapped ) {
395+ this .ignoreUnmapped = ignoreUnmapped ;
396+ return this ;
397+ }
398+
377399 @ Override
378400 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
379401 builder .startObject ();
@@ -403,6 +425,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
403425 builder .field (NESTED_FIELD .getPreferredName (), nestedSort );
404426 }
405427 builder .field (VALIDATION_METHOD_FIELD .getPreferredName (), validation );
428+ builder .field (IGNORE_UNMAPPED .getPreferredName (), ignoreUnmapped );
406429
407430 builder .endObject ();
408431 builder .endObject ();
@@ -434,14 +457,15 @@ public boolean equals(Object object) {
434457 Objects .equals (nestedFilter , other .nestedFilter ) &&
435458 Objects .equals (nestedPath , other .nestedPath ) &&
436459 Objects .equals (validation , other .validation ) &&
437- Objects .equals (nestedSort , other .nestedSort );
460+ Objects .equals (nestedSort , other .nestedSort ) &&
461+ ignoreUnmapped == other .ignoreUnmapped ;
438462 }
439463
440464 @ Override
441465 public int hashCode () {
442466 return Objects .hash (this .fieldName , this .points , this .geoDistance ,
443467 this .unit , this .sortMode , this .order , this .nestedFilter ,
444- this .nestedPath , this .validation , this .nestedSort );
468+ this .nestedPath , this .validation , this .nestedSort , this . ignoreUnmapped );
445469 }
446470
447471 /**
@@ -465,6 +489,7 @@ public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String
465489 String nestedPath = null ;
466490 NestedSortBuilder nestedSort = null ;
467491 GeoValidationMethod validation = null ;
492+ boolean ignoreUnmapped = false ;
468493
469494 XContentParser .Token token ;
470495 String currentName = parser .currentName ();
@@ -509,6 +534,8 @@ public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String
509534 } else if (NESTED_PATH_FIELD .match (currentName , parser .getDeprecationHandler ())) {
510535 DEPRECATION_LOGGER .deprecated ("[nested_path] has been deprecated in favour of the [nested] parameter" );
511536 nestedPath = parser .text ();
537+ } else if (IGNORE_UNMAPPED .match (currentName , parser .getDeprecationHandler ())) {
538+ ignoreUnmapped = parser .booleanValue ();
512539 } else if (token == Token .VALUE_STRING ){
513540 if (fieldName != null && fieldName .equals (currentName ) == false ) {
514541 throw new ParsingException (
@@ -554,6 +581,7 @@ public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String
554581 if (validation != null ) {
555582 result .validation (validation );
556583 }
584+ result .ignoreUnmapped (ignoreUnmapped );
557585 return result ;
558586 }
559587
@@ -596,8 +624,11 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
596624
597625 MappedFieldType fieldType = context .fieldMapper (fieldName );
598626 if (fieldType == null ) {
599- throw new IllegalArgumentException ("failed to find mapper for [" + fieldName
600- + "] for geo distance based sort" );
627+ if (ignoreUnmapped ) {
628+ fieldType = context .getMapperService ().unmappedFieldType ("geo_point" );
629+ } else {
630+ throw new IllegalArgumentException ("failed to find mapper for [" + fieldName + "] for geo distance based sort" );
631+ }
601632 }
602633 final IndexGeoPointFieldData geoIndexFieldData = context .getForField (fieldType );
603634
0 commit comments