2021-06-24 15:18:54 +02:00
|
|
|
import { flatMap } from 'lodash'
|
2022-06-03 10:54:30 +02:00
|
|
|
import { MappingProperty, PropertyName } from '@elastic/elasticsearch/lib/api/types'
|
2021-06-24 15:18:54 +02:00
|
|
|
import { elasticSearch } from '../../helpers/elastic-search'
|
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { IndexableDoc } from '../../types/indexable-doc.model'
|
|
|
|
|
2022-06-03 10:54:30 +02:00
|
|
|
function buildIndex (name: string, mapping: Record<PropertyName, MappingProperty>) {
|
2021-06-24 15:18:54 +02:00
|
|
|
logger.info('Initialize %s Elastic Search index.', name)
|
|
|
|
|
|
|
|
return elasticSearch.indices.create({
|
|
|
|
index: name,
|
|
|
|
body: {
|
|
|
|
settings: {
|
|
|
|
number_of_shards: 1,
|
|
|
|
number_of_replicas: 1
|
|
|
|
},
|
|
|
|
mappings: {
|
|
|
|
properties: mapping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
if (err.name === 'ResponseError' && err.meta?.body?.error.root_cause[0]?.type === 'resource_already_exists_exception') return
|
|
|
|
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function indexDocuments <T extends IndexableDoc> (options: {
|
|
|
|
objects: T[]
|
|
|
|
formatter: (o: T) => any
|
|
|
|
replace: boolean
|
|
|
|
index: string
|
|
|
|
}) {
|
|
|
|
const { objects, formatter, replace, index } = options
|
|
|
|
|
|
|
|
const elIdIndex: { [elId: string]: T } = {}
|
|
|
|
|
|
|
|
for (const object of objects) {
|
|
|
|
elIdIndex[object.elasticSearchId] = object
|
|
|
|
}
|
|
|
|
|
|
|
|
const method = replace ? 'index' : 'update'
|
|
|
|
|
|
|
|
const body = flatMap(objects, v => {
|
|
|
|
const doc = formatter(v)
|
|
|
|
|
|
|
|
const options = replace
|
|
|
|
? doc
|
|
|
|
: { doc, doc_as_upsert: true }
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
[method]: {
|
|
|
|
_id: v.elasticSearchId,
|
|
|
|
_index: index
|
|
|
|
}
|
|
|
|
},
|
|
|
|
options
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
const result = await elasticSearch.bulk({
|
|
|
|
index,
|
|
|
|
body
|
|
|
|
})
|
|
|
|
|
2022-06-03 10:54:30 +02:00
|
|
|
if (result.errors === true) {
|
2021-06-24 15:18:54 +02:00
|
|
|
const msg = 'Cannot insert data in elastic search.'
|
2022-06-03 10:54:30 +02:00
|
|
|
logger.error({ err: result }, msg)
|
2021-06-24 15:18:54 +02:00
|
|
|
throw new Error(msg)
|
|
|
|
}
|
|
|
|
|
2022-06-03 10:54:30 +02:00
|
|
|
const created: T[] = result.items
|
|
|
|
.map(i => i[method])
|
|
|
|
.filter(i => i.result === 'created')
|
|
|
|
.map(i => elIdIndex[i._id])
|
2021-06-24 15:18:54 +02:00
|
|
|
|
|
|
|
return { created }
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshIndex (indexName: string) {
|
|
|
|
logger.info('Refreshing %s index.', indexName)
|
|
|
|
|
|
|
|
return elasticSearch.indices.refresh({ index: indexName })
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
buildIndex,
|
|
|
|
indexDocuments,
|
|
|
|
refreshIndex
|
|
|
|
}
|