@@ -81,6 +81,7 @@ public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder>
81
81
private static final ParseField DISTANCE_TYPE_FIELD = new ParseField ("distance_type" );
82
82
private static final ParseField VALIDATION_METHOD_FIELD = new ParseField ("validation_method" );
83
83
private static final ParseField SORTMODE_FIELD = new ParseField ("mode" , "sort_mode" );
84
+ private static final ParseField IGNORE_UNMAPPED = new ParseField ("ignore_unmapped" );
84
85
85
86
private final String fieldName ;
86
87
private final List <GeoPoint > points = new ArrayList <>();
@@ -97,6 +98,8 @@ public class GeoDistanceSortBuilder extends SortBuilder<GeoDistanceSortBuilder>
97
98
98
99
private GeoValidationMethod validation = DEFAULT_VALIDATION ;
99
100
101
+ private boolean ignoreUnmapped = false ;
102
+
100
103
/**
101
104
* Constructs a new distance based sort on a geo point like field.
102
105
*
@@ -152,6 +155,7 @@ public GeoDistanceSortBuilder(String fieldName, String ... geohashes) {
152
155
this .nestedPath = original .nestedPath ;
153
156
this .validation = original .validation ;
154
157
this .nestedSort = original .nestedSort ;
158
+ this .ignoreUnmapped = original .ignoreUnmapped ;
155
159
}
156
160
157
161
/**
@@ -171,6 +175,9 @@ public GeoDistanceSortBuilder(StreamInput in) throws IOException {
171
175
nestedSort = in .readOptionalWriteable (NestedSortBuilder ::new );
172
176
}
173
177
validation = GeoValidationMethod .readFromStream (in );
178
+ if (in .getVersion ().onOrAfter (Version .V_6_4_0 )) {
179
+ ignoreUnmapped = in .readBoolean ();
180
+ }
174
181
}
175
182
176
183
@ Override
@@ -187,6 +194,9 @@ public void writeTo(StreamOutput out) throws IOException {
187
194
out .writeOptionalWriteable (nestedSort );
188
195
}
189
196
validation .writeTo (out );
197
+ if (out .getVersion ().onOrAfter (Version .V_6_4_0 )) {
198
+ out .writeBoolean (ignoreUnmapped );
199
+ }
190
200
}
191
201
192
202
/**
@@ -374,6 +384,18 @@ public GeoDistanceSortBuilder setNestedSort(final NestedSortBuilder nestedSort)
374
384
return this ;
375
385
}
376
386
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
+
377
399
@ Override
378
400
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
379
401
builder .startObject ();
@@ -403,6 +425,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
403
425
builder .field (NESTED_FIELD .getPreferredName (), nestedSort );
404
426
}
405
427
builder .field (VALIDATION_METHOD_FIELD .getPreferredName (), validation );
428
+ builder .field (IGNORE_UNMAPPED .getPreferredName (), ignoreUnmapped );
406
429
407
430
builder .endObject ();
408
431
builder .endObject ();
@@ -434,14 +457,15 @@ public boolean equals(Object object) {
434
457
Objects .equals (nestedFilter , other .nestedFilter ) &&
435
458
Objects .equals (nestedPath , other .nestedPath ) &&
436
459
Objects .equals (validation , other .validation ) &&
437
- Objects .equals (nestedSort , other .nestedSort );
460
+ Objects .equals (nestedSort , other .nestedSort ) &&
461
+ ignoreUnmapped == other .ignoreUnmapped ;
438
462
}
439
463
440
464
@ Override
441
465
public int hashCode () {
442
466
return Objects .hash (this .fieldName , this .points , this .geoDistance ,
443
467
this .unit , this .sortMode , this .order , this .nestedFilter ,
444
- this .nestedPath , this .validation , this .nestedSort );
468
+ this .nestedPath , this .validation , this .nestedSort , this . ignoreUnmapped );
445
469
}
446
470
447
471
/**
@@ -465,6 +489,7 @@ public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String
465
489
String nestedPath = null ;
466
490
NestedSortBuilder nestedSort = null ;
467
491
GeoValidationMethod validation = null ;
492
+ boolean ignoreUnmapped = false ;
468
493
469
494
XContentParser .Token token ;
470
495
String currentName = parser .currentName ();
@@ -509,6 +534,8 @@ public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String
509
534
} else if (NESTED_PATH_FIELD .match (currentName , parser .getDeprecationHandler ())) {
510
535
DEPRECATION_LOGGER .deprecated ("[nested_path] has been deprecated in favour of the [nested] parameter" );
511
536
nestedPath = parser .text ();
537
+ } else if (IGNORE_UNMAPPED .match (currentName , parser .getDeprecationHandler ())) {
538
+ ignoreUnmapped = parser .booleanValue ();
512
539
} else if (token == Token .VALUE_STRING ){
513
540
if (fieldName != null && fieldName .equals (currentName ) == false ) {
514
541
throw new ParsingException (
@@ -554,6 +581,7 @@ public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String
554
581
if (validation != null ) {
555
582
result .validation (validation );
556
583
}
584
+ result .ignoreUnmapped (ignoreUnmapped );
557
585
return result ;
558
586
}
559
587
@@ -596,8 +624,11 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
596
624
597
625
MappedFieldType fieldType = context .fieldMapper (fieldName );
598
626
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
+ }
601
632
}
602
633
final IndexGeoPointFieldData geoIndexFieldData = context .getForField (fieldType );
603
634
0 commit comments