Skip to content

Commit

Permalink
v0.1.21 RE of specialized datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
pdesmarets committed May 8, 2019
1 parent fe09546 commit 0a4cbe4
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 126 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Elasticsearch",
"version": "0.1.20",
"versionDate": "2019-05-07",
"version": "0.1.21",
"versionDate": "2019-05-08",
"author": "hackolade",
"engines": {
"hackolade": "1.12.7",
Expand Down
1 change: 1 addition & 0 deletions properties_pane/field_level/fieldLevelConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ making sure that you maintain a proper JSON format.
"propertyName": "relations",
"propertyKeyword": "relations",
"propertyType": "group",
"isTargetProperty": true,
"structure": [
{
"propertyName": "parent",
Expand Down
114 changes: 107 additions & 7 deletions reverse_engineering/SchemaCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const snippets = {
"geometrycollection": require(snippetsPath + "geoshape-geometrycollection.json"),
"multilinestring": require(snippetsPath + "geoshape-multilinestring.json"),
"multipolygon": require(snippetsPath + "geoshape-multipolygon.json"),
"polygon": require(snippetsPath + "geoshape-polygon.json")
"polygon": require(snippetsPath + "geoshape-polygon.json"),
"completionArray": require(snippetsPath + "completionArray.json"),
"completionObject": require(snippetsPath + "completionObject.json")
};

const helper = require('../helper/helper');
Expand Down Expand Up @@ -78,7 +80,7 @@ module.exports = {
sample = sample || {};

schema.properties = this.getServiceFields(sample);
schema.properties._source.properties = this.getFields(elasticMapping.properties, sample._source);
schema.properties._source.properties = this.getFields(elasticMapping.properties, sample._source, elasticMapping.properties);

if (elasticMapping.dynamic) {
schema.dynamic = elasticMapping.dynamic;
Expand All @@ -87,19 +89,19 @@ module.exports = {
return schema;
},

getFields(properties, sample) {
getFields(properties, sample, mapping) {
let schema = {};

for (let fieldName in properties) {
const currentSample = sample && sample[fieldName];

schema[fieldName] = this.getField(properties[fieldName], currentSample);
schema[fieldName] = this.getField(properties[fieldName], currentSample, mapping);
}

return schema;
},

getField(fieldData, sample) {
getField(fieldData, sample, mapping) {
let schema = {};

if (!fieldData) {
Expand All @@ -116,7 +118,7 @@ module.exports = {
].indexOf(schema.type) !== -1;

if (hasProperties) {
let properties = this.getFields(fieldData.properties, sample);
let properties = this.getFields(fieldData.properties, sample, mapping);

if (isArrayType) {
schema.items = [{
Expand All @@ -128,17 +130,27 @@ module.exports = {
}
}

if (fieldData.copy_to) {
fieldData.copy_to = this.getCopyToPath(fieldData.copy_to, mapping);
}

if (Array.isArray(sample) && !isArrayType) {
schema = {
type: 'array',
items: [schema]
};
}

if (schema.type === 'geo-shape' || schema.type === 'geo-point') {
if ([
'geo-shape', 'geo-point'
].includes(schema.type)) {
schema = this.handleSnippet(schema);
}

if (schema.type === 'completion') {
schema = this.handleCompletionSnippet(schema);
}

schema = this.setProperties(schema, fieldData);

return schema;
Expand Down Expand Up @@ -178,7 +190,23 @@ module.exports = {
case "binary":
case "nested":
case "date":
case "token_count":
case "murmur3":
case "annotated_text":
case "percolator":
case "ip":
case "dense_vector":
case "sparse_vector":
case "alias":
case "rank_feature":
case "rank_features":
case "join":
return { type };
case "completion":
return {
type,
subType: this.getCompletionSubtype(value)
};
case "geo_point":
return {
type: "geo-point",
Expand Down Expand Up @@ -310,6 +338,22 @@ module.exports = {
}
},

getCompletionSubtype(value) {
if (Array.isArray(value)) {
return "array";
} else {
return "object";
}
},

handleCompletionSnippet(schema) {
return Object.assign({}, this.handleSnippet(Object.assign({}, schema, {
subType: schema.subType === 'array' ? 'completionArray' : 'completionObject'
})), {
subType: schema.subType
});
},

handleSnippet(schema) {
const snippet = snippets[schema.subType];
if (snippet) {
Expand Down Expand Up @@ -363,11 +407,67 @@ module.exports = {
for (let propName in properties) {
if (propName === 'fields') {
schema["stringfields"] = JSON.stringify(properties[propName], null, 4);
} else if (propName === 'relations') {
schema[propName] = getRelations(properties[propName]);
} else {
schema[propName] = properties[propName];
}
}

return schema;
},

getCopyToPath(copyToValue, mapping) {
const copyTo = Array.isArray(copyToValue) ? copyToValue : [ copyToValue ];

const result = copyTo.reduce((result, propertyName) => {
return [
...result,
...findPropertiesInMapping(propertyName, mapping)
];
}, []);

return result;
}
};

const getRelations = (relations) => {
if (typeof relations !== 'object') {
return [];
}

return Object.keys(relations).map((parentName) => {
const children = Array.isArray(relations[parentName]) ? relations[parentName] : [ relations[parentName] ];

return {
parent: parentName,
children: children.map(item => ({ name: item }))
};
}, {});
};

const findPropertiesInMapping = (propertyName, mapping) => {
const getPaths = (propertyName, properties, path) => {
return Object.keys(properties).reduce((result, name) => {
const property = properties[name];

if (property.properties) {
result = [
...result,
...getPaths(propertyName, property.properties, [...path, name])
];
}

if (propertyName === name) {
result = [
...result,
[...path, name].join('.')
];
}

return result;
}, []);
};

return getPaths(propertyName, mapping, []);
};
Loading

0 comments on commit 0a4cbe4

Please sign in to comment.