Skip to content

Commit

Permalink
[EPM] Handle object type fields with properties in mappings (elastic#…
Browse files Browse the repository at this point in the history
…64745) (elastic#64774)

* Generate correct mapping for object type field with properties

* Add test for fields definition in reverse order
  • Loading branch information
skh authored Apr 29, 2020
1 parent b0255a0 commit da13d07
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,53 @@ test('tests processing object field with dynamic set to strict', () => {
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldDynamicStrictMapping));
});

test('tests processing object field with property', () => {
const objectFieldWithPropertyLiteralYml = `
- name: a
type: object
- name: a.b
type: keyword
`;
const objectFieldWithPropertyMapping = {
properties: {
a: {
properties: {
b: {
ignore_above: 1024,
type: 'keyword',
},
},
},
},
};
const fields: Field[] = safeLoad(objectFieldWithPropertyLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldWithPropertyMapping));
});

test('tests processing object field with property, reverse order', () => {
const objectFieldWithPropertyReversedLiteralYml = `
- name: a.b
type: keyword
- name: a
type: object
`;
const objectFieldWithPropertyReversedMapping = {
properties: {
a: {
properties: {
b: {
ignore_above: 1024,
type: 'keyword',
},
},
},
},
};
const fields: Field[] = safeLoad(objectFieldWithPropertyReversedLiteralYml);
const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
expect(JSON.stringify(mappings)).toEqual(JSON.stringify(objectFieldWithPropertyReversedMapping));
});
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,35 @@ describe('processFields', () => {
JSON.stringify(mixedFieldsExpanded)
);
});

const objectFieldWithProperty = [
{
name: 'a',
type: 'object',
dynamic: true,
},
{
name: 'a.b',
type: 'keyword',
},
];

const objectFieldWithPropertyExpanded = [
{
name: 'a',
type: 'group',
dynamic: true,
fields: [
{
name: 'b',
type: 'keyword',
},
],
},
];
test('correctly handles properties of object type fields', () => {
expect(JSON.stringify(processFields(objectFieldWithProperty))).toEqual(
JSON.stringify(objectFieldWithPropertyExpanded)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@ function dedupFields(fields: Fields): Fields {
return f.name === field.name;
});
if (found) {
if (found.type === 'group' && field.type === 'group' && found.fields && field.fields) {
if (
(found.type === 'group' || found.type === 'object') &&
field.type === 'group' &&
field.fields
) {
if (!found.fields) {
found.fields = [];
}
found.type = 'group';
found.fields = dedupFields(found.fields.concat(field.fields));
} else {
// only 'group' fields can be merged in this way
Expand Down

0 comments on commit da13d07

Please sign in to comment.