Explorar o código

Ensure that field aliases cannot be used in multi-fields. (#32219)

Julie Tibshirani %!s(int64=7) %!d(string=hai) anos
pai
achega
0f0068b91c

+ 1 - 1
docs/reference/mapping/types/alias.asciidoc

@@ -74,7 +74,7 @@ field alias to query over multiple target fields in a single clause.
 ==== Unsupported APIs
 ==== Unsupported APIs
 
 
 Writes to field aliases are not supported: attempting to use an alias in an index or update request
 Writes to field aliases are not supported: attempting to use an alias in an index or update request
-will result in a failure. Likewise, aliases cannot be used as the target of `copy_to`.
+will result in a failure. Likewise, aliases cannot be used as the target of `copy_to` or in multi-fields.
 
 
 Because alias names are not present in the document source, aliases cannot be used when performing
 Because alias names are not present in the document source, aliases cannot be used when performing
 source filtering. For example, the following request will return an empty result for `_source`:
 source filtering. For example, the following request will return an empty result for `_source`:

+ 3 - 1
server/src/main/java/org/elasticsearch/index/mapper/TypeParsers.java

@@ -230,7 +230,9 @@ public class TypeParsers {
                 } else {
                 } else {
                     throw new MapperParsingException("no type specified for property [" + multiFieldName + "]");
                     throw new MapperParsingException("no type specified for property [" + multiFieldName + "]");
                 }
                 }
-                if (type.equals(ObjectMapper.CONTENT_TYPE) || type.equals(ObjectMapper.NESTED_CONTENT_TYPE)) {
+                if (type.equals(ObjectMapper.CONTENT_TYPE)
+                        || type.equals(ObjectMapper.NESTED_CONTENT_TYPE)
+                        || type.equals(FieldAliasMapper.CONTENT_TYPE)) {
                     throw new MapperParsingException("Type [" + type + "] cannot be used in multi field");
                     throw new MapperParsingException("Type [" + type + "] cannot be used in multi field");
                 }
                 }
 
 

+ 24 - 0
server/src/test/java/org/elasticsearch/index/mapper/DocumentMapperParserTests.java

@@ -76,4 +76,28 @@ public class DocumentMapperParserTests extends ESSingleNodeTestCase {
             mapperParser.parse("type", new CompressedXContent(mapping)));
             mapperParser.parse("type", new CompressedXContent(mapping)));
         assertTrue(e.getMessage(), e.getMessage().contains("mapper [foo] of different type"));
         assertTrue(e.getMessage(), e.getMessage().contains("mapper [foo] of different type"));
     }
     }
+
+    public void testMultiFieldsWithFieldAlias() throws Exception {
+        IndexService indexService = createIndex("test");
+        DocumentMapperParser mapperParser = indexService.mapperService().documentMapperParser();
+        String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
+            .startObject("properties")
+                .startObject("field")
+                    .field("type", "text")
+                    .startObject("fields")
+                        .startObject("alias")
+                            .field("type", "alias")
+                            .field("path", "other-field")
+                        .endObject()
+                    .endObject()
+                .endObject()
+                .startObject("other-field")
+                    .field("type", "keyword")
+                .endObject()
+            .endObject()
+        .endObject().endObject());
+        MapperParsingException e = expectThrows(MapperParsingException.class, () ->
+            mapperParser.parse("type", new CompressedXContent(mapping)));
+        assertEquals("Type [alias] cannot be used in multi field", e.getMessage());
+    }
 }
 }