import { ESClient } from './get_es_client' ; export async function createOrUpdateIndex ( { client , clear , indexName , template , } ) { if ( clear ) { try { await client . indices . delete ( { index : indexName , } ) ; } catch ( err ) { if ( err . body . status !== 404 ) { throw err ; } } } const settings = { ... template . settings } ; delete settings ?. index ?. number_of_shards ; delete settings ?. index ?. sort ; const indexExists = await client . indices . exists ( { index : indexName , } ) ; if ( ! indexExists ) { await client . indices . create ( { index : indexName , body : template , } ) ; } else { await client . indices . close ( { index : indexName } ) ; await Promise . all ( [ template . mappings ? client . indices . putMapping ( { index : indexName , body : template . mappings , } ) : Promise . resolve ( undefined as any ) , settings ? client . indices . putSettings ( { index : indexName , body : settings , } ) : Promise . resolve ( undefined as any ) , ] ) ; await client . indices . open ( { index : indexName } ) ; } }