2020-11-26 18:03:18 +01:00
const { Client } = require ( "@elastic/elasticsearch" )
2021-01-15 14:11:51 +01:00
const { QUERY _TYPES , FIELD _TYPES } = require ( "./Integration" )
2020-11-26 18:03:18 +01:00
2021-01-11 18:18:22 +01:00
const SCHEMA = {
2021-06-15 20:39:40 +02:00
docs : "https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html" ,
2021-02-19 16:07:43 +01:00
description :
"Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents." ,
2021-02-18 17:58:10 +01:00
friendlyName : "ElasticSearch" ,
2021-01-11 18:18:22 +01:00
datasource : {
url : {
type : "string" ,
required : true ,
2021-01-15 14:11:51 +01:00
default : "http://localhost:9200" ,
2021-01-11 18:18:22 +01:00
} ,
2020-11-26 18:03:18 +01:00
} ,
query : {
2021-01-15 14:11:51 +01:00
create : {
2021-01-22 13:22:28 +01:00
type : QUERY _TYPES . FIELDS ,
customisable : true ,
fields : {
index : {
type : FIELD _TYPES . STRING ,
required : true ,
2021-01-15 14:11:51 +01:00
} ,
} ,
} ,
read : {
2021-01-22 13:22:28 +01:00
type : QUERY _TYPES . FIELDS ,
customisable : true ,
fields : {
index : {
type : FIELD _TYPES . STRING ,
required : true ,
2021-01-15 14:11:51 +01:00
} ,
} ,
} ,
update : {
2021-01-22 13:22:28 +01:00
type : QUERY _TYPES . FIELDS ,
customisable : true ,
fields : {
id : {
type : FIELD _TYPES . STRING ,
required : true ,
} ,
index : {
type : FIELD _TYPES . STRING ,
required : true ,
2021-01-15 14:11:51 +01:00
} ,
} ,
} ,
delete : {
2021-01-22 13:22:28 +01:00
type : QUERY _TYPES . FIELDS ,
fields : {
index : {
type : FIELD _TYPES . STRING ,
required : true ,
} ,
id : {
type : FIELD _TYPES . STRING ,
required : true ,
2021-01-15 14:11:51 +01:00
} ,
} ,
2021-01-11 18:18:22 +01:00
} ,
2020-11-26 18:03:18 +01:00
} ,
}
class ElasticSearchIntegration {
constructor ( config ) {
this . config = config
this . client = new Client ( { node : config . url } )
}
2021-01-13 17:39:47 +01:00
async create ( query ) {
2021-01-15 14:11:51 +01:00
const { index , json } = query
2021-01-12 18:45:43 +01:00
try {
const result = await this . client . index ( {
2021-01-15 14:11:51 +01:00
index ,
body : json ,
2021-01-12 18:45:43 +01:00
} )
2021-01-15 14:11:51 +01:00
return result . body
2021-01-12 18:45:43 +01:00
} catch ( err ) {
console . error ( "Error writing to elasticsearch" , err )
throw err
} finally {
await this . client . close ( )
}
}
async read ( query ) {
2021-01-15 14:11:51 +01:00
const { index , json } = query
2020-11-26 18:03:18 +01:00
try {
const result = await this . client . search ( {
2021-01-15 14:11:51 +01:00
index : index ,
body : json ,
2020-11-26 18:03:18 +01:00
} )
return result . body . hits . hits . map ( ( { _source } ) => _source )
2020-11-26 18:34:15 +01:00
} catch ( err ) {
console . error ( "Error querying elasticsearch" , err )
throw err
2020-11-26 18:03:18 +01:00
} finally {
await this . client . close ( )
}
}
2021-01-15 14:11:51 +01:00
async update ( query ) {
const { id , index , json } = query
try {
const result = await this . client . update ( {
id ,
index ,
body : json ,
} )
return result . body
} catch ( err ) {
console . error ( "Error querying elasticsearch" , err )
throw err
} finally {
await this . client . close ( )
}
}
async delete ( query ) {
try {
const result = await this . client . delete ( query )
return result . body
} catch ( err ) {
console . error ( "Error deleting from elasticsearch" , err )
throw err
} finally {
await this . client . close ( )
}
}
2020-11-26 18:03:18 +01:00
}
module . exports = {
2021-01-11 18:18:22 +01:00
schema : SCHEMA ,
2020-11-26 18:03:18 +01:00
integration : ElasticSearchIntegration ,
}