-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Transform] avoid mapping problems with index templates (#51368)
insert explict mappings for objects in nested output to avoid clashes with index templates fixes #51321
- Loading branch information
Hendrik Muhs
authored
Jan 28, 2020
1 parent
83a819a
commit 4d11e1a
Showing
4 changed files
with
259 additions
and
47 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
.../java/org/elasticsearch/xpack/transform/integration/TransformPivotRestSpecialCasesIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.transform.integration; | ||
|
||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class TransformPivotRestSpecialCasesIT extends TransformRestTestCase { | ||
private static boolean indicesCreated = false; | ||
|
||
// preserve indices in order to reuse source indices in several test cases | ||
@Override | ||
protected boolean preserveIndicesUponCompletion() { | ||
return true; | ||
} | ||
|
||
@Before | ||
public void createIndexes() throws IOException { | ||
|
||
// it's not possible to run it as @BeforeClass as clients aren't initialized then, so we need this little hack | ||
if (indicesCreated) { | ||
return; | ||
} | ||
|
||
createReviewsIndex(); | ||
indicesCreated = true; | ||
} | ||
|
||
public void testIndexTemplateMappingClash() throws Exception { | ||
String transformId = "special_pivot_template_mappings_clash"; | ||
String transformIndex = "special_pivot_template_mappings_clash"; | ||
|
||
// create a template that defines a field "rating" with a type "float" which will clash later with | ||
// output field "rating.avg" in the pivot config | ||
final Request createIndexTemplateRequest = new Request("PUT", "_template/special_pivot_template"); | ||
|
||
String template = "{" | ||
+ "\"index_patterns\" : [\"special_pivot_template*\"]," | ||
+ " \"mappings\" : {" | ||
+ " \"properties\": {" | ||
+ " \"rating\":{" | ||
+ " \"type\": \"float\"\n" | ||
+ " }" | ||
+ " }" | ||
+ " }" | ||
+ "}"; | ||
|
||
createIndexTemplateRequest.setJsonEntity(template); | ||
Map<String, Object> createIndexTemplateResponse = entityAsMap(client().performRequest(createIndexTemplateRequest)); | ||
assertThat(createIndexTemplateResponse.get("acknowledged"), equalTo(Boolean.TRUE)); | ||
|
||
final Request createTransformRequest = new Request("PUT", getTransformEndpoint() + transformId); | ||
|
||
String config = "{" | ||
+ " \"source\": {\"index\":\"" | ||
+ REVIEWS_INDEX_NAME | ||
+ "\"}," | ||
+ " \"dest\": {\"index\":\"" | ||
+ transformIndex | ||
+ "\"},"; | ||
|
||
config += " \"pivot\": {" | ||
+ " \"group_by\": {" | ||
+ " \"reviewer\": {" | ||
+ " \"terms\": {" | ||
+ " \"field\": \"user_id\"" | ||
+ " } } }," | ||
+ " \"aggregations\": {" | ||
+ " \"rating.avg\": {" | ||
+ " \"avg\": {" | ||
+ " \"field\": \"stars\"" | ||
+ " } }" | ||
+ " } }" | ||
+ "}"; | ||
|
||
createTransformRequest.setJsonEntity(config); | ||
Map<String, Object> createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); | ||
assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); | ||
|
||
startAndWaitForTransform(transformId, transformIndex); | ||
assertTrue(indexExists(transformIndex)); | ||
|
||
// we expect 27 documents as there shall be 27 user_id's | ||
Map<String, Object> indexStats = getAsMap(transformIndex + "/_stats"); | ||
assertEquals(27, XContentMapValues.extractValue("_all.total.docs.count", indexStats)); | ||
|
||
// get and check some users | ||
Map<String, Object> searchResult = getAsMap(transformIndex + "/_search?q=reviewer:user_4"); | ||
|
||
assertEquals(1, XContentMapValues.extractValue("hits.total.value", searchResult)); | ||
Number actual = (Number) ((List<?>) XContentMapValues.extractValue("hits.hits._source.rating.avg", searchResult)).get(0); | ||
assertEquals(3.878048780, actual.doubleValue(), 0.000001); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...orm/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/SchemaUtilTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.transform.transforms.pivot; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SchemaUtilTests extends ESTestCase { | ||
|
||
public void testInsertNestedObjectMappings() { | ||
Map<String, String> fieldMappings = new HashMap<>() { | ||
{ | ||
// creates: a.b, a | ||
put("a.b.c", "long"); | ||
put("a.b.d", "double"); | ||
// creates: c.b, c | ||
put("c.b.a", "double"); | ||
// creates: c.d | ||
put("c.d.e", "object"); | ||
put("d", "long"); | ||
put("e.f.g", "long"); | ||
// cc: already there | ||
put("e.f", "object"); | ||
// cc: already there but different type (should not be possible) | ||
put("e", "long"); | ||
// cc: start with . (should not be possible) | ||
put(".x", "long"); | ||
// cc: start and ends with . (should not be possible), creates: .y | ||
put(".y.", "long"); | ||
// cc: ends with . (should not be possible), creates: .z | ||
put(".z.", "long"); | ||
} | ||
}; | ||
|
||
SchemaUtil.insertNestedObjectMappings(fieldMappings); | ||
|
||
assertEquals(18, fieldMappings.size()); | ||
assertEquals("long", fieldMappings.get("a.b.c")); | ||
assertEquals("object", fieldMappings.get("a.b")); | ||
assertEquals("double", fieldMappings.get("a.b.d")); | ||
assertEquals("object", fieldMappings.get("a")); | ||
assertEquals("object", fieldMappings.get("c.d")); | ||
assertEquals("object", fieldMappings.get("e.f")); | ||
assertEquals("long", fieldMappings.get("e")); | ||
assertEquals("object", fieldMappings.get(".y")); | ||
assertEquals("object", fieldMappings.get(".z")); | ||
assertFalse(fieldMappings.containsKey(".")); | ||
assertFalse(fieldMappings.containsKey("")); | ||
} | ||
|
||
} |