Browse Source

Avoid using external values in parent-join and percolator mappers (#71834)

We would like to remove the use of 'external values' in document parsing.
This commit simplifies two of the four places it is currently used, by adding
direct indexValue methods to BinaryFieldMapper and ParentIdFieldMapper.

Relates to #56063
Alan Woodward 4 years ago
parent
commit
f2ac4f9953

+ 4 - 4
modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentIdFieldMapper.java

@@ -92,10 +92,10 @@ public final class ParentIdFieldMapper extends FieldMapper {
 
     @Override
     protected void parseCreateField(ParseContext context) {
-        if (context.externalValueSet() == false) {
-            throw new IllegalStateException("external value not set");
-        }
-        String refId = (String) context.externalValue();
+        throw new UnsupportedOperationException("Cannot directly call parse() on a ParentIdFieldMapper");
+    }
+
+    public void indexValue(ParseContext context, String refId) {
         BytesRef binaryValue = new BytesRef(refId);
         Field field = new Field(fieldType().name(), binaryValue, Defaults.FIELD_TYPE);
         context.doc().add(field);

+ 2 - 4
modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java

@@ -254,15 +254,13 @@ public final class ParentJoinFieldMapper extends FieldMapper {
             if (context.sourceToParse().routing() == null) {
                 throw new IllegalArgumentException("[routing] is missing for join field [" + name() + "]");
             }
-            ParseContext externalContext = context.createExternalValueContext(parent);
             String fieldName = fieldType().joiner.parentJoinField(name);
-            parentIdFields.get(fieldName).parse(externalContext);
+            parentIdFields.get(fieldName).indexValue(context, parent);
         }
         if (fieldType().joiner.parentTypeExists(name)) {
             // Index the document as a parent
-            ParseContext externalContext = context.createExternalValueContext(context.sourceToParse().id());
             String fieldName = fieldType().joiner.childJoinField(name);
-            parentIdFields.get(fieldName).parse(externalContext);
+            parentIdFields.get(fieldName).indexValue(context, context.sourceToParse().id());
         }
 
         BytesRef binaryValue = new BytesRef(name);

+ 1 - 2
modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java

@@ -366,8 +366,7 @@ public class PercolatorFieldMapper extends FieldMapper {
             try (OutputStreamStreamOutput out  = new OutputStreamStreamOutput(stream)) {
                 out.setVersion(indexVersion);
                 out.writeNamedWriteable(queryBuilder);
-                byte[] queryBuilderAsBytes = stream.toByteArray();
-                qbField.parse(context.createExternalValueContext(queryBuilderAsBytes));
+                qbField.indexValue(context, stream.toByteArray());
             }
         }
     }

+ 4 - 0
server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java

@@ -149,6 +149,10 @@ public class BinaryFieldMapper extends FieldMapper {
                 value = context.parser().binaryValue();
             }
         }
+        indexValue(context, value);
+    }
+
+    public void indexValue(ParseContext context, byte[] value) {
         if (value == null) {
             return;
         }