@@ -21,9 +21,11 @@ import {
2121 Inject ,
2222 Injectable ,
2323 InternalServerErrorException ,
24+ Logger ,
2425 NotFoundException ,
2526} from '@nestjs/common' ;
2627import { Client , errors } from '@opensearch-project/opensearch' ;
28+ import { Indices_PutMapping_Response } from '@opensearch-project/opensearch/api' ;
2729
2830import type {
2931 CreateDataDto ,
@@ -38,38 +40,55 @@ import { LargeWindowException } from './large-window.exception';
3840
3941@Injectable ( )
4042export class OpensearchRepository {
43+ private logger = new Logger ( OpensearchRepository . name ) ;
4144 private opensearchClient : Client ;
4245 constructor ( @Inject ( 'OPENSEARCH_CLIENT' ) opensearchClient : Client ) {
4346 this . opensearchClient = opensearchClient ;
4447 }
4548
4649 async createIndex ( { index } : CreateIndexDto ) {
4750 const indexName = 'channel_' + index ;
48- await this . opensearchClient . indices . create ( {
49- index : indexName ,
50- body : {
51- settings : {
52- index : { max_ngram_diff : 1 } ,
53- analysis : {
54- analyzer : {
55- ngram_analyzer : {
56- type : 'custom' ,
57- filter : [ 'lowercase' , 'asciifolding' , 'cjk_width' ] ,
58- tokenizer : 'ngram_tokenizer' ,
51+ try {
52+ const response = await this . opensearchClient . indices . create ( {
53+ index : indexName ,
54+ body : {
55+ settings : {
56+ index : { max_ngram_diff : 1 } ,
57+ analysis : {
58+ analyzer : {
59+ ngram_analyzer : {
60+ type : 'custom' ,
61+ filter : [ 'lowercase' , 'asciifolding' , 'cjk_width' ] ,
62+ tokenizer : 'ngram_tokenizer' ,
63+ } ,
5964 } ,
60- } ,
61- tokenizer : {
62- ngram_tokenizer : {
63- type : 'ngram' ,
64- min_gram : 1 ,
65- max_gram : 2 ,
66- token_chars : [ 'letter' , 'digit' , 'punctuation' , 'symbol' ] ,
65+ tokenizer : {
66+ ngram_tokenizer : {
67+ type : 'ngram' ,
68+ min_gram : 1 ,
69+ max_gram : 2 ,
70+ token_chars : [ 'letter' , 'digit' , 'punctuation' , 'symbol' ] ,
71+ } ,
6772 } ,
6873 } ,
6974 } ,
7075 } ,
71- } ,
72- } ) ;
76+ } ) ;
77+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
78+ if ( response ) {
79+ this . logger . log (
80+ `Index created successfully: ${ JSON . stringify ( response . body , null , 2 ) } ` ,
81+ ) ;
82+ }
83+ } catch ( error ) {
84+ this . logger . log ( `Error creating index: ${ error } ` ) ;
85+ if ( error ?. meta ?. body ) {
86+ this . logger . log (
87+ `OpenSearch error details:${ JSON . stringify ( error . meta . body , null , 2 ) } ` ,
88+ ) ;
89+ }
90+ throw error ;
91+ }
7392 await this . opensearchClient . indices . putAlias ( {
7493 index : indexName ,
7594 name : index ,
@@ -82,10 +101,23 @@ export class OpensearchRepository {
82101 } ) ;
83102 if ( statusCode !== 200 ) throw new NotFoundException ( 'index is not found' ) ;
84103
85- return await this . opensearchClient . indices . putMapping ( {
86- index,
87- body : { properties : mappings } ,
88- } ) ;
104+ let response : Indices_PutMapping_Response ;
105+ try {
106+ response = await this . opensearchClient . indices . putMapping ( {
107+ index,
108+ body : { properties : mappings } ,
109+ } ) ;
110+ } catch ( error ) {
111+ this . logger . log ( `Error put mapping: ${ error } ` ) ;
112+ if ( error ?. meta ?. body ) {
113+ this . logger . log (
114+ `OpenSearch error details:${ JSON . stringify ( error . meta . body , null , 2 ) } ` ,
115+ ) ;
116+ }
117+ throw error ;
118+ }
119+
120+ return response ;
89121 }
90122
91123 async createData ( { id, index, data } : CreateDataDto ) {
0 commit comments