2121import org .apache .lucene .index .IndexOptions ;
2222import org .apache .lucene .index .IndexableField ;
2323import org .apache .lucene .index .Term ;
24- import org .apache .lucene .search .DocValuesFieldExistsQuery ;
2524import org .apache .lucene .search .Query ;
2625import org .apache .lucene .search .TermQuery ;
2726import org .apache .lucene .spatial .prefix .PrefixTreeStrategy ;
5453import java .util .Map ;
5554import java .util .Objects ;
5655
56+ import static org .elasticsearch .index .mapper .GeoPointFieldMapper .Names .IGNORE_MALFORMED ;
57+
5758/**
5859 * FieldMapper for indexing {@link org.locationtech.spatial4j.shape.Shape}s.
5960 * <p>
@@ -96,6 +97,7 @@ public static class Defaults {
9697 public static final Orientation ORIENTATION = Orientation .RIGHT ;
9798 public static final double LEGACY_DISTANCE_ERROR_PCT = 0.025d ;
9899 public static final Explicit <Boolean > COERCE = new Explicit <>(false , false );
100+ public static final Explicit <Boolean > IGNORE_MALFORMED = new Explicit <>(false , false );
99101
100102 public static final MappedFieldType FIELD_TYPE = new GeoShapeFieldType ();
101103
@@ -115,6 +117,7 @@ public static class Defaults {
115117 public static class Builder extends FieldMapper .Builder <Builder , GeoShapeFieldMapper > {
116118
117119 private Boolean coerce ;
120+ private Boolean ignoreMalformed ;
118121
119122 public Builder (String name ) {
120123 super (name , Defaults .FIELD_TYPE , Defaults .FIELD_TYPE );
@@ -145,6 +148,21 @@ protected Explicit<Boolean> coerce(BuilderContext context) {
145148 return Defaults .COERCE ;
146149 }
147150
151+ public Builder ignoreMalformed (boolean ignoreMalformed ) {
152+ this .ignoreMalformed = ignoreMalformed ;
153+ return builder ;
154+ }
155+
156+ protected Explicit <Boolean > ignoreMalformed (BuilderContext context ) {
157+ if (ignoreMalformed != null ) {
158+ return new Explicit <>(ignoreMalformed , true );
159+ }
160+ if (context .indexSettings () != null ) {
161+ return new Explicit <>(IGNORE_MALFORMED_SETTING .get (context .indexSettings ()), false );
162+ }
163+ return Defaults .IGNORE_MALFORMED ;
164+ }
165+
148166 @ Override
149167 public GeoShapeFieldMapper build (BuilderContext context ) {
150168 GeoShapeFieldType geoShapeFieldType = (GeoShapeFieldType )fieldType ;
@@ -154,8 +172,8 @@ public GeoShapeFieldMapper build(BuilderContext context) {
154172 }
155173 setupFieldType (context );
156174
157- return new GeoShapeFieldMapper (name , fieldType , coerce (context ), context . indexSettings ( ), multiFieldsBuilder . build ( this ,
158- context ), copyTo );
175+ return new GeoShapeFieldMapper (name , fieldType , ignoreMalformed (context ), coerce ( context ), context . indexSettings () ,
176+ multiFieldsBuilder . build ( this , context ), copyTo );
159177 }
160178 }
161179
@@ -186,6 +204,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
186204 } else if (Names .STRATEGY .equals (fieldName )) {
187205 builder .fieldType ().setStrategyName (fieldNode .toString ());
188206 iterator .remove ();
207+ } else if (IGNORE_MALFORMED .equals (fieldName )) {
208+ builder .ignoreMalformed (TypeParsers .nodeBooleanValue (fieldName , "ignore_malformed" , fieldNode , parserContext ));
209+ iterator .remove ();
189210 } else if (Names .COERCE .equals (fieldName )) {
190211 builder .coerce (TypeParsers .nodeBooleanValue (fieldName , Names .COERCE , fieldNode , parserContext ));
191212 iterator .remove ();
@@ -428,11 +449,13 @@ public Query termQuery(Object value, QueryShardContext context) {
428449 }
429450
430451 protected Explicit <Boolean > coerce ;
452+ protected Explicit <Boolean > ignoreMalformed ;
431453
432- public GeoShapeFieldMapper (String simpleName , MappedFieldType fieldType , Explicit <Boolean > coerce , Settings indexSettings ,
433- MultiFields multiFields , CopyTo copyTo ) {
454+ public GeoShapeFieldMapper (String simpleName , MappedFieldType fieldType , Explicit <Boolean > ignoreMalformed ,
455+ Explicit < Boolean > coerce , Settings indexSettings , MultiFields multiFields , CopyTo copyTo ) {
434456 super (simpleName , fieldType , Defaults .FIELD_TYPE , indexSettings , multiFields , copyTo );
435457 this .coerce = coerce ;
458+ this .ignoreMalformed = ignoreMalformed ;
436459 }
437460
438461 @ Override
@@ -461,7 +484,9 @@ public Mapper parse(ParseContext context) throws IOException {
461484 context .doc ().add (field );
462485 }
463486 } catch (Exception e ) {
464- throw new MapperParsingException ("failed to parse [" + fieldType ().name () + "]" , e );
487+ if (ignoreMalformed .value () == false ) {
488+ throw new MapperParsingException ("failed to parse [" + fieldType ().name () + "]" , e );
489+ }
465490 }
466491 return null ;
467492 }
@@ -478,6 +503,9 @@ protected void doMerge(Mapper mergeWith, boolean updateAllTypes) {
478503 if (gsfm .coerce .explicit ()) {
479504 this .coerce = gsfm .coerce ;
480505 }
506+ if (gsfm .ignoreMalformed .explicit ()) {
507+ this .ignoreMalformed = gsfm .ignoreMalformed ;
508+ }
481509 }
482510
483511 @ Override
@@ -506,14 +534,21 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults,
506534 builder .field (Names .STRATEGY_POINTS_ONLY , fieldType ().pointsOnly ());
507535 }
508536 if (includeDefaults || coerce .explicit ()) {
509- builder .field ("coerce" , coerce .value ());
537+ builder .field (Names .COERCE , coerce .value ());
538+ }
539+ if (includeDefaults || ignoreMalformed .explicit ()) {
540+ builder .field (IGNORE_MALFORMED , ignoreMalformed .value ());
510541 }
511542 }
512543
513544 public Explicit <Boolean > coerce () {
514545 return coerce ;
515546 }
516547
548+ public Explicit <Boolean > ignoreMalformed () {
549+ return ignoreMalformed ;
550+ }
551+
517552 @ Override
518553 protected String contentType () {
519554 return CONTENT_TYPE ;
0 commit comments