Skip to content

Commit

Permalink
feat(es): add a script for registering pipelines
Browse files Browse the repository at this point in the history
Includes the pipeline for the metadata quaility score, as well
as the human readable format
  • Loading branch information
jahow committed Oct 20, 2023
1 parent 3c899e0 commit 995035f
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
158 changes: 158 additions & 0 deletions pipelines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
const ES_URL = 'http://localhost:9200/'
const GN_RECORDS_INDEX = 'gn-records'
const VERSION = 100 // increment on changes

const GEONETWORK_UI_PIPELINE = {
description: 'GeoNetwork-UI pipeline',
version: VERSION,
processors: [
{
// compute the metadata quality score
script: {
lang: 'painless',
source: `
int total=8;
int ok=0;
if(ctx.resourceTitleObject != null && ctx.resourceTitleObject.default != null && ctx.resourceTitleObject.default != '') {
ok++
}
if(ctx.resourceAbstractObject != null && ctx.resourceAbstractObject.default != null && ctx.resourceAbstractObject.default != '') {
ok++
}
if(ctx.contact != null && ctx.contact.length > 0 && ctx.contact[0].organisation != null && ctx.contact[0].organisation != '') {
ok++
}
if(ctx.contact != null && ctx.contact.length > 0 && ctx.contact[0].email != null && ctx.contact[0].email != '') {
ok++
}
if(ctx.cl_topic != null && ctx.cl_topic.length > 0) {
ok++
}
if(ctx.tag != null && ctx.tag.length > 0) {
ok++
}
if(ctx.cl_maintenanceAndUpdateFrequency != null && ctx.cl_maintenanceAndUpdateFrequency.length > 0) {
ok++
}
if(ctx.MD_LegalConstraintsUseLimitationObject != null && ctx.MD_LegalConstraintsUseLimitationObject.length > 0) {
ok++
}
ctx.qualityScore = ok * 100 / total;`,
},
},
{
// generate human-readable data formats
script: {
lang: 'painless',
source: `
ctx.originalFormat = ctx.format;
for(int i = 0; i < ctx.format.length; i++) {
String format = ctx.format[i].toLowerCase();
if (format.contains('shp') || format.contains('shapefile')) {
ctx.format[i] = 'ESRI Shapefile'
} else if (format.contains('ogc:w') || format.contains('esri:rest') || format.contains('tms')) {
ctx.format[i] = 'Service'
} else if (format.contains('pdf')) {
ctx.format[i] = 'PDF'
} else if (format.contains('png') || format.contains('jpg') || format.contains('jpeg') || format.contains('bmp')) {
ctx.format[i] = 'Image'
} else if (format.contains('excel') || format.contains('xls') || format.contains('vnd.oasis.opendocument.spreadsheet') || format.contains('vnd.openxmlformats-officedocument.spreadsheetml.sheet')) {
ctx.format[i] = 'Excel'
} else if (format.contains('zip')) {
ctx.format[i] = 'ZIP'
} else if (format.contains('geopackage') || format.contains('gpkg')) {
ctx.format[i] = 'Geopackage'
} else if (format.contains('postgis')) {
ctx.format[i] = 'PostGIS'
} else if (format.contains('gml')) {
ctx.format[i] = 'GML'
} else if (format.contains('kml')) {
ctx.format[i] = 'KML'
} else if (format.contains('xml')) {
ctx.format[i] = 'XML'
} else if (format.contains('html')) {
ctx.format[i] = 'HTML'
} else if (format.contains('geo+json') || format.contains('geojson')) {
ctx.format[i] = 'GeoJSON'
} else if (format.contains('json')) {
ctx.format[i] = 'JSON'
} else if (format.contains('csv')) {
ctx.format[i] = 'CSV'
} else if (format.contains('rtf')) {
ctx.format[i] = 'RTF'
} else if (format.contains('text')) {
ctx.format[i] = 'Text'
} else {
// comment this to hide unrecognized formats
ctx.format[i] = 'unknown: ' + ctx.format[i];
}
}`,
},
},
],
}

async function registerPipeline(name, payload) {
console.log(`adding ${name} pipeline...`)

await fetch(`${ES_URL}_ingest/pipeline/${name}`, {
method: 'PUT',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
},
})
.then((resp) => resp.json())
.then((result) => {
if (result.acknowledged !== true) {
console.error(result)
throw new Error('something went wrong')
}
})

console.log(`${name} pipeline was successfully registered!`)
}

async function setDefaultPipeline(name) {
console.log(`setting ${name} as default pipeline...`)

await fetch(`${ES_URL}${GN_RECORDS_INDEX}/_settings`, {
method: 'PUT',
body: JSON.stringify({ 'index.default_pipeline': name }),
headers: {
'Content-Type': 'application/json',
},
})
.then((resp) => resp.json())
.then((result) => {
if (result.acknowledged !== true) {
console.error(result)
throw new Error('something went wrong')
}
})

console.log(`${name} pipeline was successfully set as default!`)
}

async function registerPipelines() {
console.log('querying currently registered pipelines...')

const pipelines = await fetch('http://localhost:9200/_ingest/pipeline').then(
(resp) => resp.json()
)

const names = Object.keys(pipelines)
names.forEach((name) => {
console.log(`
> ${name}`)
console.log(` ${pipelines[name].description}`)
})

console.log('')
await registerPipeline('geonetwork-ui', GEONETWORK_UI_PIPELINE)

console.log('')
await setDefaultPipeline('geonetwork-ui')
}

registerPipelines()
2 changes: 2 additions & 0 deletions support-services/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ services:
retries: 10
volumes:
- elasticsearch_data:/usr/share/elasticsearch/data
ports:
- "9200:9200"

kibana:
image: kibana:7.11.1
Expand Down

0 comments on commit 995035f

Please sign in to comment.