Skip to content

Commit

Permalink
Refactor getAliases, add more yaml tests (#105370)
Browse files Browse the repository at this point in the history
* Refactor getAliases, add more yaml tests

* more test updates
  • Loading branch information
kkrik-es authored Feb 12, 2024
1 parent 4eaff75 commit 510c851
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ dynamic templates:
refresh: true
body:
- '{ "create": { "dynamic_templates": { "data": "counter_metric" } } }'
- '{ "@timestamp": "2023-09-01T13:03:08.138Z","data": "10", "attributes.dim": "A", "attributes.another.dim": "C" }'
- '{ "@timestamp": "2023-09-01T13:03:08.138Z", "data": "10", "attributes.dim1": "A", "attributes.dim2": "1", "attributes.another.dim1": "C", "attributes.another.dim2": "10" }'
- '{ "create": { "dynamic_templates": { "data": "counter_metric" } } }'
- '{ "@timestamp": "2023-09-01T13:03:09.138Z","data": "20", "attributes.dim": "A", "attributes.another.dim": "C" }'
- '{ "@timestamp": "2023-09-01T13:03:09.138Z", "data": "20", "attributes.dim1": "A", "attributes.dim2": "1", "attributes.another.dim1": "C", "attributes.another.dim2": "10" }'
- '{ "create": { "dynamic_templates": { "data": "counter_metric" } } }'
- '{ "@timestamp": "2023-09-01T13:03:10.138Z","data": "30", "attributes.dim": "B", "attributes.another.dim": "D" }'
- '{ "@timestamp": "2023-09-01T13:03:10.138Z", "data": "30", "attributes.dim1": "B", "attributes.dim2": "2", "attributes.another.dim1": "D", "attributes.another.dim2": "20" }'
- '{ "create": { "dynamic_templates": { "data": "counter_metric" } } }'
- '{ "@timestamp": "2023-09-01T13:03:10.238Z","data": "40", "attributes.dim": "B", "attributes.another.dim": "D" }'
- '{ "@timestamp": "2023-09-01T13:03:10.238Z", "data": "40", "attributes.dim1": "B", "attributes.dim2": "2", "attributes.another.dim1": "D", "attributes.another.dim2": "20" }'

- do:
search:
Expand All @@ -255,14 +255,14 @@ dynamic templates:
filterA:
filter:
term:
dim: A
dim1: A
aggs:
tsids:
terms:
field: _tsid

- length: { aggregations.filterA.tsids.buckets: 1 }
- match: { aggregations.filterA.tsids.buckets.0.key: "KPLatIF-tbSjI8l9LcbTur4gYpiKF9nXxRUMHnJBiqqP58deEIun8H8" }
- match: { aggregations.filterA.tsids.buckets.0.key: "MD2HE8yse1ZklY-p0-bRcC8gYpiK8yYWLhfZ18WLDvTuBX1YJX1Ll7UMNJqYNES5Eg" }
- match: { aggregations.filterA.tsids.buckets.0.doc_count: 2 }

- do:
Expand All @@ -274,14 +274,52 @@ dynamic templates:
filterA:
filter:
term:
another.dim: C
dim2: 1
aggs:
tsids:
terms:
field: _tsid

- length: { aggregations.filterA.tsids.buckets: 1 }
- match: { aggregations.filterA.tsids.buckets.0.key: "KPLatIF-tbSjI8l9LcbTur4gYpiKF9nXxRUMHnJBiqqP58deEIun8H8" }
- match: { aggregations.filterA.tsids.buckets.0.key: "MD2HE8yse1ZklY-p0-bRcC8gYpiK8yYWLhfZ18WLDvTuBX1YJX1Ll7UMNJqYNES5Eg" }
- match: { aggregations.filterA.tsids.buckets.0.doc_count: 2 }

- do:
search:
index: k9s
body:
size: 0
aggs:
filterA:
filter:
term:
another.dim1: C
aggs:
tsids:
terms:
field: _tsid

- length: { aggregations.filterA.tsids.buckets: 1 }
- match: { aggregations.filterA.tsids.buckets.0.key: "MD2HE8yse1ZklY-p0-bRcC8gYpiK8yYWLhfZ18WLDvTuBX1YJX1Ll7UMNJqYNES5Eg" }
- match: { aggregations.filterA.tsids.buckets.0.doc_count: 2 }

- do:
search:
index: k9s
body:
size: 0
aggs:
filterA:
filter:
term:
another.dim2: 10
aggs:
tsids:
terms:
field: _tsid

- length: { aggregations.filterA.tsids.buckets: 1 }
- match: { aggregations.filterA.tsids.buckets.0.key: "MD2HE8yse1ZklY-p0-bRcC8gYpiK8yYWLhfZ18WLDvTuBX1YJX1Ll7UMNJqYNES5Eg" }
- match: { aggregations.filterA.tsids.buckets.0.doc_count: 2 }

---
Expand Down Expand Up @@ -425,13 +463,13 @@ dynamic templates with nesting:
refresh: true
body:
- '{ "create": { "dynamic_templates": { "data": "counter_metric" } } }'
- '{ "@timestamp": "2023-09-01T13:03:08.138Z","data": "10", "resource.attributes.dim": "A", "resource.attributes.another.dim": "C", "attributes.more.dim": "E" }'
- '{ "@timestamp": "2023-09-01T13:03:08.138Z","data": "10", "resource.attributes.dim1": "A", "resource.attributes.another.dim1": "1", "attributes.dim2": "C", "attributes.another.dim2": "10" }'
- '{ "create": { "dynamic_templates": { "data": "counter_metric" } } }'
- '{ "@timestamp": "2023-09-01T13:03:09.138Z","data": "20", "resource.attributes.dim": "A", "resource.attributes.another.dim": "C", "attributes.more.dim": "E" }'
- '{ "@timestamp": "2023-09-01T13:03:09.138Z","data": "20", "resource.attributes.dim1": "A", "resource.attributes.another.dim1": "1", "attributes.dim2": "C", "attributes.another.dim2": "10" }'
- '{ "create": { "dynamic_templates": { "data": "counter_metric" } } }'
- '{ "@timestamp": "2023-09-01T13:03:10.138Z","data": "30", "resource.attributes.dim": "B", "resource.attributes.another.dim": "D", "attributes.more.dim": "F" }'
- '{ "@timestamp": "2023-09-01T13:03:10.138Z","data": "30", "resource.attributes.dim1": "B", "resource.attributes.another.dim1": "2", "attributes.dim2": "D", "attributes.another.dim2": "20" }'
- '{ "create": { "dynamic_templates": { "data": "counter_metric" } } }'
- '{ "@timestamp": "2023-09-01T13:03:10.238Z","data": "40", "resource.attributes.dim": "B", "resource.attributes.another.dim": "D", "attributes.more.dim": "F" }'
- '{ "@timestamp": "2023-09-01T13:03:10.238Z","data": "40", "resource.attributes.dim1": "B", "resource.attributes.another.dim1": "2", "attributes.dim2": "D", "attributes.another.dim2": "20" }'

- do:
search:
Expand All @@ -450,14 +488,33 @@ dynamic templates with nesting:
filterA:
filter:
term:
dim: A
dim1: A
aggs:
tsids:
terms:
field: _tsid

- length: { aggregations.filterA.tsids.buckets: 1 }
- match: { aggregations.filterA.tsids.buckets.0.key: "MK0AtuFZowY4QPzoYEAZNK7zJhYuIGKYiosO9O4X2dfFtp-JEbk39FSSMEq_vwX7uw" }
- match: { aggregations.filterA.tsids.buckets.0.doc_count: 2 }

- do:
search:
index: k9s
body:
size: 0
aggs:
filterA:
filter:
term:
dim2: C
aggs:
tsids:
terms:
field: _tsid

- length: { aggregations.filterA.tsids.buckets: 1 }
- match: { aggregations.filterA.tsids.buckets.0.key: "LEjiJ4ATCXWlzeFvhGQ9lYlnP-nRIGKYihfZ18WoJ94t9a8OpbsCdwZALomb" }
- match: { aggregations.filterA.tsids.buckets.0.key: "MK0AtuFZowY4QPzoYEAZNK7zJhYuIGKYiosO9O4X2dfFtp-JEbk39FSSMEq_vwX7uw" }
- match: { aggregations.filterA.tsids.buckets.0.doc_count: 2 }

- do:
Expand All @@ -469,14 +526,14 @@ dynamic templates with nesting:
filterA:
filter:
term:
another.dim: C
another.dim1: 1
aggs:
tsids:
terms:
field: _tsid

- length: { aggregations.filterA.tsids.buckets: 1 }
- match: { aggregations.filterA.tsids.buckets.0.key: "LEjiJ4ATCXWlzeFvhGQ9lYlnP-nRIGKYihfZ18WoJ94t9a8OpbsCdwZALomb" }
- match: { aggregations.filterA.tsids.buckets.0.key: "MK0AtuFZowY4QPzoYEAZNK7zJhYuIGKYiosO9O4X2dfFtp-JEbk39FSSMEq_vwX7uw" }
- match: { aggregations.filterA.tsids.buckets.0.doc_count: 2 }

- do:
Expand All @@ -488,14 +545,14 @@ dynamic templates with nesting:
filterA:
filter:
term:
more.dim: E
another.dim2: 10
aggs:
tsids:
terms:
field: _tsid

- length: { aggregations.filterA.tsids.buckets: 1 }
- match: { aggregations.filterA.tsids.buckets.0.key: "LEjiJ4ATCXWlzeFvhGQ9lYlnP-nRIGKYihfZ18WoJ94t9a8OpbsCdwZALomb" }
- match: { aggregations.filterA.tsids.buckets.0.key: "MK0AtuFZowY4QPzoYEAZNK7zJhYuIGKYiosO9O4X2dfFtp-JEbk39FSSMEq_vwX7uw" }
- match: { aggregations.filterA.tsids.buckets.0.doc_count: 2 }

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,7 @@ public RootObjectMapper.Builder addRuntimeFields(Map<String, RuntimeField> runti
@Override
public RootObjectMapper build(MapperBuilderContext context) {
Map<String, Mapper> mappers = buildMappers(context);

Map<String, Mapper> aliasMappers = new HashMap<>();
Map<String, ObjectMapper.Builder> objectIntermediates = new HashMap<>(1);
getAliasMappers(mappers, aliasMappers, objectIntermediates, context, 0);
for (var entry : objectIntermediates.entrySet()) {
aliasMappers.put(entry.getKey(), entry.getValue().build(context));
}
mappers.putAll(aliasMappers);

mappers.putAll(getAliasMappers(mappers, context));
return new RootObjectMapper(
name,
enabled,
Expand All @@ -135,6 +127,16 @@ public RootObjectMapper build(MapperBuilderContext context) {
);
}

Map<String, Mapper> getAliasMappers(Map<String, Mapper> mappers, MapperBuilderContext context) {
Map<String, Mapper> aliasMappers = new HashMap<>();
Map<String, ObjectMapper.Builder> objectIntermediates = new HashMap<>(1);
getAliasMappers(mappers, aliasMappers, objectIntermediates, context, 0);
for (var entry : objectIntermediates.entrySet()) {
aliasMappers.put(entry.getKey(), entry.getValue().build(context));
}
return aliasMappers;
}

void getAliasMappers(
Map<String, Mapper> mappers,
Map<String, Mapper> aliasMappers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,7 @@ public void testPassThroughObjectNested() throws IOException {

public void testAliasMappersCreatesAlias() throws Exception {
var context = MapperBuilderContext.root(false, false);
Map<String, Mapper> aliases = new HashMap<>();
var objectIntermediates = new HashMap<String, ObjectMapper.Builder>(1);
new RootObjectMapper.Builder("root", Explicit.EXPLICIT_FALSE).getAliasMappers(
Map<String, Mapper> aliases = new RootObjectMapper.Builder("root", Explicit.EXPLICIT_FALSE).getAliasMappers(
Map.of(
"labels",
new PassThroughObjectMapper(
Expand All @@ -407,20 +405,15 @@ public void testAliasMappersCreatesAlias() throws Exception {
Explicit.EXPLICIT_FALSE
)
),
aliases,
objectIntermediates,
context,
0
context
);
assertEquals(1, aliases.size());
assertThat(aliases.get("host"), instanceOf(FieldAliasMapper.class));
}

public void testAliasMappersCreatesAliasNested() throws Exception {
var context = MapperBuilderContext.root(false, false);
Map<String, Mapper> aliases = new HashMap<>();
var objectIntermediates = new HashMap<String, ObjectMapper.Builder>(1);
new RootObjectMapper.Builder("root", Explicit.EXPLICIT_FALSE).getAliasMappers(
Map<String, Mapper> aliases = new RootObjectMapper.Builder("root", Explicit.EXPLICIT_FALSE).getAliasMappers(
Map.of(
"outer",
new ObjectMapper(
Expand All @@ -442,10 +435,7 @@ public void testAliasMappersCreatesAliasNested() throws Exception {
)
)
),
aliases,
objectIntermediates,
context,
0
context
);
assertEquals(1, aliases.size());
assertThat(aliases.get("host"), instanceOf(FieldAliasMapper.class));
Expand Down Expand Up @@ -477,9 +467,7 @@ public void testAliasMappersExitsInDeepNesting() throws Exception {

public void testAliasMappersCreatesNoAliasForRegularObject() throws Exception {
var context = MapperBuilderContext.root(false, false);
Map<String, Mapper> aliases = new HashMap<>();
var objectIntermediates = new HashMap<String, ObjectMapper.Builder>(1);
new RootObjectMapper.Builder("root", Explicit.EXPLICIT_FALSE).getAliasMappers(
Map<String, Mapper> aliases = new RootObjectMapper.Builder("root", Explicit.EXPLICIT_FALSE).getAliasMappers(
Map.of(
"labels",
new ObjectMapper(
Expand All @@ -491,19 +479,14 @@ public void testAliasMappersCreatesNoAliasForRegularObject() throws Exception {
Map.of("host", new KeywordFieldMapper.Builder("host", IndexVersion.current()).build(context))
)
),
aliases,
objectIntermediates,
context,
0
context
);
assertTrue(aliases.isEmpty());
}

public void testAliasMappersConflictingField() throws Exception {
var context = MapperBuilderContext.root(false, false);
Map<String, Mapper> aliases = new HashMap<>();
var objectIntermediates = new HashMap<String, ObjectMapper.Builder>(1);
new RootObjectMapper.Builder("root", Explicit.EXPLICIT_FALSE).getAliasMappers(
Map<String, Mapper> aliases = new RootObjectMapper.Builder("root", Explicit.EXPLICIT_FALSE).getAliasMappers(
Map.of(
"labels",
new PassThroughObjectMapper(
Expand All @@ -517,10 +500,7 @@ public void testAliasMappersConflictingField() throws Exception {
"host",
new KeywordFieldMapper.Builder("host", IndexVersion.current()).build(context)
),
aliases,
objectIntermediates,
context,
0
context
);
assertTrue(aliases.isEmpty());
}
Expand Down

0 comments on commit 510c851

Please sign in to comment.