-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e27a935
commit 51d6efb
Showing
19 changed files
with
1,141 additions
and
424 deletions.
There are no files selected for viewing
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 @@ | ||
node_modules |
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,127 @@ | ||
const helper = require('../helper/helper.js'); | ||
|
||
module.exports = { | ||
generateScript(data, logger, cb) { | ||
const { jsonSchema, modelData, containerData, entityData, isUpdateScript } = data; | ||
let result = ""; | ||
let mappingScript = { | ||
mappings: { | ||
[entityData.collectionName.toLowerCase()]: { | ||
properties: this.getMappingScript(JSON.parse(jsonSchema)) | ||
} | ||
} | ||
}; | ||
|
||
if (isUpdateScript) { | ||
result = this.getCurlScript(mappingScript, modelData, containerData); | ||
} else { | ||
result += this.getKibanaScript(mappingScript, containerData); | ||
} | ||
|
||
cb(null, result); | ||
}, | ||
|
||
getCurlScript(mapping, modelData, indexData) { | ||
const host = modelData.host || 'localhost'; | ||
const port = modelData.port || 9200; | ||
const indexName = indexData.name || ""; | ||
|
||
return `curl -XPUT '${host}:${port}/${indexName.toLowerCase()}?pretty' -H 'Content-Type: application/json' -d '\n${JSON.stringify(mapping, null, 4)}\n'`; | ||
}, | ||
|
||
getKibanaScript(mapping, indexData) { | ||
const indexName = indexData.name || ""; | ||
|
||
return `PUT /${indexName.toLowerCase()}\n${JSON.stringify(mapping, null, 4)}`; | ||
}, | ||
|
||
getMappingScript(jsonSchema) { | ||
let schema = {}; | ||
|
||
if (!(jsonSchema.properties && jsonSchema.properties._source && jsonSchema.properties._source.properties)) { | ||
return schema; | ||
} | ||
|
||
schema = this.getSchemaByItem(jsonSchema.properties._source.properties) | ||
|
||
return schema; | ||
}, | ||
|
||
getSchemaByItem(properties) { | ||
let schema = {}; | ||
|
||
for (let fieldName in properties) { | ||
let field = properties[fieldName]; | ||
|
||
schema[fieldName] = this.getField(field); | ||
} | ||
|
||
return schema; | ||
}, | ||
|
||
getField(field) { | ||
let schema = {}; | ||
const fieldProperties = helper.getFieldProperties(field.type, field, {}); | ||
let type = this.getFieldType(field); | ||
|
||
if (type !== 'object' && type !== 'array') { | ||
schema.type = type; | ||
} | ||
|
||
if (type === 'object') { | ||
schema.properties = {}; | ||
} | ||
|
||
this.setProperties(schema, fieldProperties); | ||
|
||
if (type === 'geo_shape' || type === 'geo_point') { | ||
return schema; | ||
} else if (field.properties) { | ||
schema.properties = this.getSchemaByItem(field.properties); | ||
} else if (field.items) { | ||
let arrData = field.items; | ||
|
||
if (Array.isArray(field.items)) { | ||
arrData = field.items[0]; | ||
} | ||
|
||
schema = Object.assign(schema, this.getField(arrData)); | ||
} | ||
|
||
return schema; | ||
}, | ||
|
||
getFieldType(field) { | ||
switch(field.type) { | ||
case 'geo-shape': | ||
return 'geo_shape'; | ||
case 'geo-point': | ||
return 'geo_point'; | ||
case 'number': | ||
return field.mode || 'long'; | ||
case 'string': | ||
return field.mode || 'text'; | ||
case 'range': | ||
return field.mode || 'integer_range'; | ||
case 'null': | ||
return 'long'; | ||
default: | ||
return field.type; | ||
} | ||
}, | ||
|
||
setProperties(schema, properties) { | ||
for (let propName in properties) { | ||
if (propName === 'stringfields') { | ||
try { | ||
schema['fields'] = JSON.parse(properties[propName]); | ||
} catch (e) { | ||
} | ||
} else { | ||
schema[propName] = properties[propName]; | ||
} | ||
} | ||
|
||
return schema; | ||
} | ||
}; |
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,6 @@ | ||
{ | ||
"extension": "txt", | ||
"filterName": "Plain text", | ||
"namePrefix": "Elasticsearch Mapping", | ||
"hasUpdateScript": true | ||
} |
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,8 @@ | ||
{ | ||
"name": "elasticsearch", | ||
"version": "1.0.0", | ||
"description": "", | ||
"author": "Hackolade", | ||
"dependencies": { | ||
} | ||
} |
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,37 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const fieldLevelConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '../properties_pane/field_level/fieldLevelConfig.json')).toString().replace(/\/\*[.\s\S]*\*\//ig, "")); | ||
|
||
module.exports = { | ||
getTargetFieldLevelPropertyNames(type, data) { | ||
if (!fieldLevelConfig.structure[type]) { | ||
return []; | ||
} | ||
|
||
return fieldLevelConfig.structure[type].filter(property => { | ||
if (typeof property === 'object' && property.isTargetProperty) { | ||
if (property.dependency) { | ||
return (data[property.dependency.key] == property.dependency.value); | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}).map(property => property.propertyKeyword); | ||
}, | ||
|
||
getFieldProperties(type, data, pseudonyms) { | ||
const propertyNames = this.getTargetFieldLevelPropertyNames(type, data); | ||
|
||
return propertyNames.reduce((result, propertyName) => { | ||
if (Object.prototype.hasOwnProperty.call(data, propertyName)) { | ||
result[propertyName] = data[propertyName]; | ||
} else if (Object.prototype.hasOwnProperty.call(data, pseudonyms[propertyName])) { | ||
result[pseudonyms[propertyName]] = data[pseudonyms[propertyName]]; | ||
} | ||
|
||
return result; | ||
}, {}); | ||
} | ||
}; |
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
Oops, something went wrong.