Search code examples
elasticsearchnestjs

add analyzer for index with elasticsearch and nestjs


I'm using @nestjs/elasticsearch and want to use bulgarian analyzer, for search it's too easy

    const result = await this.elasticsearchService.search<Question>({
      index: this.index,
      query: {
        multi_match: {
          query: text,
          fields: ['body', 'tags.name'],
          analyzer: 'bulgarian',
        },
      },
    });

but there is nothing similar available for index, this is the current code

    this.elasticsearchService.index<Question>({
      index: this.index,
      id: question.id.toString(),
      document: question,
    });

I read from here https://stackoverflow.com/a/33219447/2748984 it should be done through mapping, but index method doesn't take mapping as well, here is what index interface is

export interface IndexRequest<TDocument = unknown> extends RequestBase {
    id?: Id;
    index: IndexName;
    if_primary_term?: long;
    if_seq_no?: SequenceNumber;
    op_type?: OpType;
    pipeline?: string;
    refresh?: Refresh;
    routing?: Routing;
    timeout?: Duration;
    version?: VersionNumber;
    version_type?: VersionType;
    wait_for_active_shards?: WaitForActiveShards;
    require_alias?: boolean;
    document?: TDocument;
}

I don't see any of the options is related to mapping or analyzer


Solution

  • There must be a this.elasticsearchService.create(...) function that allows you to create your index with a specific mapping as in the question you've linked to, something similar to this:

    this.elasticsearchService.indices.create<Question>({
      index: this.index,
      mapping: ...,
    });