@@ -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,10 @@ public GeoDistanceSortBuilder(StreamInput in) throws IOException {
171
175
nestedSort = in .readOptionalWriteable (NestedSortBuilder ::new );
172
176
}
173
177
validation = GeoValidationMethod .readFromStream (in );
178
+ // TODO: Change to 6_4_0 after backport
179
+ if (in .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
180
+ ignoreUnmapped = in .readBoolean ();
181
+ }
174
182
}
175
183
176
184
@ Override
@@ -187,6 +195,10 @@ public void writeTo(StreamOutput out) throws IOException {
187
195
out .writeOptionalWriteable (nestedSort );
188
196
}
189
197
validation .writeTo (out );
198
+ // TODO: Change to 6_4_0 after backport
199
+ if (out .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
200
+ out .writeBoolean (ignoreUnmapped );
201
+ }
190
202
}
191
203
192
204
/**
@@ -374,6 +386,18 @@ public GeoDistanceSortBuilder setNestedSort(final NestedSortBuilder nestedSort)
374
386
return this ;
375
387
}
376
388
389
+ /**
390
+ * Returns true if unmapped geo fields should be treated as located at an infinite distance
391
+ */
392
+ public boolean ignoreUnmapped () {
393
+ return ignoreUnmapped ;
394
+ }
395
+
396
+ public GeoDistanceSortBuilder ignoreUnmapped (boolean ignoreUnmapped ) {
397
+ this .ignoreUnmapped = ignoreUnmapped ;
398
+ return this ;
399
+ }
400
+
377
401
@ Override
378
402
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
379
403
builder .startObject ();
@@ -403,6 +427,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
403
427
builder .field (NESTED_FIELD .getPreferredName (), nestedSort );
404
428
}
405
429
builder .field (VALIDATION_METHOD_FIELD .getPreferredName (), validation );
430
+ builder .field (IGNORE_UNMAPPED .getPreferredName (), ignoreUnmapped );
406
431
407
432
builder .endObject ();
408
433
builder .endObject ();
@@ -434,14 +459,15 @@ public boolean equals(Object object) {
434
459
Objects .equals (nestedFilter , other .nestedFilter ) &&
435
460
Objects .equals (nestedPath , other .nestedPath ) &&
436
461
Objects .equals (validation , other .validation ) &&
437
- Objects .equals (nestedSort , other .nestedSort );
462
+ Objects .equals (nestedSort , other .nestedSort ) &&
463
+ ignoreUnmapped == other .ignoreUnmapped ;
438
464
}
439
465
440
466
@ Override
441
467
public int hashCode () {
442
468
return Objects .hash (this .fieldName , this .points , this .geoDistance ,
443
469
this .unit , this .sortMode , this .order , this .nestedFilter ,
444
- this .nestedPath , this .validation , this .nestedSort );
470
+ this .nestedPath , this .validation , this .nestedSort , this . ignoreUnmapped );
445
471
}
446
472
447
473
/**
@@ -465,6 +491,7 @@ public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String
465
491
String nestedPath = null ;
466
492
NestedSortBuilder nestedSort = null ;
467
493
GeoValidationMethod validation = null ;
494
+ boolean ignoreUnmapped = false ;
468
495
469
496
XContentParser .Token token ;
470
497
String currentName = parser .currentName ();
@@ -509,6 +536,8 @@ public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String
509
536
} else if (NESTED_PATH_FIELD .match (currentName , parser .getDeprecationHandler ())) {
510
537
DEPRECATION_LOGGER .deprecated ("[nested_path] has been deprecated in favour of the [nested] parameter" );
511
538
nestedPath = parser .text ();
539
+ } else if (IGNORE_UNMAPPED .match (currentName , parser .getDeprecationHandler ())) {
540
+ ignoreUnmapped = parser .booleanValue ();
512
541
} else if (token == Token .VALUE_STRING ){
513
542
if (fieldName != null && fieldName .equals (currentName ) == false ) {
514
543
throw new ParsingException (
@@ -554,6 +583,7 @@ public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String
554
583
if (validation != null ) {
555
584
result .validation (validation );
556
585
}
586
+ result .ignoreUnmapped (ignoreUnmapped );
557
587
return result ;
558
588
}
559
589
@@ -596,8 +626,11 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
596
626
597
627
MappedFieldType fieldType = context .fieldMapper (fieldName );
598
628
if (fieldType == null ) {
599
- throw new IllegalArgumentException ("failed to find mapper for [" + fieldName
600
- + "] for geo distance based sort" );
629
+ if (ignoreUnmapped ) {
630
+ fieldType = context .getMapperService ().unmappedFieldType ("geo_point" );
631
+ } else {
632
+ throw new IllegalArgumentException ("failed to find mapper for [" + fieldName + "] for geo distance based sort" );
633
+ }
601
634
}
602
635
final IndexGeoPointFieldData geoIndexFieldData = context .getForField (fieldType );
603
636
0 commit comments