Skip to content

Commit

Permalink
Merge pull request #140 from social-native/fix/mappings710
Browse files Browse the repository at this point in the history
Fix/mappings710
  • Loading branch information
markrsocialnative authored May 6, 2021
2 parents 559e176 + dda4ce5 commit 31b3db6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elastic-composer",
"version": "4.5.0",
"version": "4.6.0",
"description": "",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/mapping_parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import MappingParser from '../mapping_parser';

test('it should work when mappings have a "dynamic" field', () => {
const fakeMapping = {
crm_index: {
mappings: {
dynamic: 'false',
properties: {
agg_average_image_submission_score: {
type: 'long' as 'long'
},
agg_historic_percent_approved_invite_to_submitted: {
properties: {
all_time: {
type: 'double' as 'double'
}
}
}
}
}
}
};
const actual = MappingParser.flattenMappings710(fakeMapping);

expect(actual).toEqual({
agg_average_image_submission_score: 'long',
'agg_historic_percent_approved_invite_to_submitted.all_time': 'double'
});
});
19 changes: 18 additions & 1 deletion src/mapping_parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {objKeys} from './utils';
import {ESMappingType, ESMappingPropertyType, ESMappingProperties, ESMapping} from './types';
import {
ESMappingType,
ESMappingPropertyType,
ESMappingProperties,
ESMapping,
ESMapping710
} from './types';

export function isPropertyType(
prop: ESMappingPropertyType | {properties: ESMappingProperties}
Expand Down Expand Up @@ -27,6 +33,17 @@ export default class MappingParser {
}, {});
};

public static flattenMappings710(rawMappings: ESMapping710): Record<string, ESMappingType> {
let flattenedMappings = {};
Object.values(rawMappings).forEach(({mappings}) => {
flattenedMappings = {
...flattenedMappings,
...MappingParser.flattenMappingProperty(mappings.properties)
};
});
return flattenedMappings;
}

public static flattenMappingProperty = (
mappingProperties: ESMappingProperties,
parentFieldName: string | undefined = undefined
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,12 @@ export type ESMapping<Alias extends string> = {
};
};
};

export type ESMapping710 = {
[index: string]: {
mappings: {
dynamic?: string;
properties: ESMappingProperties
}
}
}

0 comments on commit 31b3db6

Please sign in to comment.