setIndexTemplate to be directly responsible for calling putIndexTemplate

Signed-off-by: Emma Indal <emma.indahl@gmail.com>
This commit is contained in:
Emma Indal
2022-06-10 13:31:46 +02:00
parent ddce23d080
commit 0b8c1b93e2
2 changed files with 14 additions and 53 deletions
@@ -32,9 +32,6 @@ class ElasticSearchSearchEngineForTranslatorTests extends ElasticSearchSearchEng
getTranslator() {
return this.translator;
}
getIndexTemplate() {
return this.indexTemplate;
}
}
const mock = new Mock();
@@ -93,16 +90,16 @@ describe('ElasticSearchSearchEngine', () => {
});
describe('custom index template', () => {
it('should not have custom template set as default', async () => {
expect(inspectableSearchEngine.getIndexTemplate()).toBeUndefined();
});
it('should set custom index template', async () => {
const indexTemplateSpy = jest.fn().mockReturnValue(customIndexTemplate);
mock.add(
{ method: 'PUT', path: '/_index_template/custom-index-template' },
indexTemplateSpy,
);
await inspectableSearchEngine.setIndexTemplate(customIndexTemplate);
expect(inspectableSearchEngine.getIndexTemplate()).toMatchObject(
customIndexTemplate,
);
expect(indexTemplateSpy).toHaveBeenCalled();
expect(indexTemplateSpy).toHaveBeenCalledTimes(1);
});
});
@@ -811,38 +808,12 @@ describe('ElasticSearchSearchEngine', () => {
elasticSearchClient: client,
}),
);
expect(indexerMock.on).toHaveBeenCalledWith(
'finish',
expect.any(Function),
);
expect(indexerMock.on).toHaveBeenCalledWith(
'error',
expect.any(Function),
);
});
describe('onFinish', () => {
let callback: Function;
beforeEach(async () => {
mock.clearAll();
await testSearchEngine.getIndexer('test-index');
callback = indexerMock.on.mock.calls[1][1];
});
it('should set provided index template on finish', async () => {
const indexTemplateSpy = jest.fn().mockReturnValue(customIndexTemplate);
mock.add(
{ method: 'PUT', path: '/_index_template/custom-index-template' },
indexTemplateSpy,
);
await callback();
expect(indexTemplateSpy).toHaveBeenCalled();
});
});
describe('onError', () => {
let errorHandler: Function;
const error = new Error('some error');
@@ -145,7 +145,6 @@ function isBlank(str: string) {
export class ElasticSearchSearchEngine implements SearchEngine {
private readonly elasticSearchClient: Client;
private readonly highlightOptions: ElasticSearchHighlightConfig;
protected indexTemplate?: ElasticSearchCustomIndexTemplate;
constructor(
private readonly elasticSearchClientOptions: ElasticSearchClientOptions,
@@ -268,8 +267,13 @@ export class ElasticSearchSearchEngine implements SearchEngine {
this.translator = translator;
}
setIndexTemplate(template: ElasticSearchCustomIndexTemplate) {
this.indexTemplate = template;
async setIndexTemplate(template: ElasticSearchCustomIndexTemplate) {
try {
await this.elasticSearchClient.indices.putIndexTemplate(template);
this.logger.info('Custom index template set');
} catch (error) {
this.logger.error(`Unable to set custom index template: ${error}`);
}
}
async getIndexer(type: string) {
@@ -303,20 +307,6 @@ export class ElasticSearchSearchEngine implements SearchEngine {
}
});
indexer.on('finish', async () => {
// Set custom index template if set
if (this.indexTemplate) {
try {
await this.elasticSearchClient.indices.putIndexTemplate(
this.indexTemplate,
);
this.logger.info('Custom index template set');
} catch (error) {
this.logger.error(`Unable to set custom index template: ${error}`);
}
}
});
return indexer;
}