Home > Software design >  How to add mapping fields when indexing data with Elasticsearch and Nestjs
How to add mapping fields when indexing data with Elasticsearch and Nestjs

Time:02-23

I'm building a NestJS app using ElasticSearch. I've been able to insert some data with the Client wrapped from oficial elasticsearch lib. Since my data will have specific fields where one of them will be an object (where this object could have multiples field, object included), I want to map the whole fields from the data that will be added to this index. At this moment my code looks like:

let insertedData = await this.elasticSearch.index({index: 'products', body:{
            name: 'Window',
            material: 'glass',
            observation: {
                type: '1'
            }
     }})

The mapping will be: name: string, material: string, observation: flattened. I couldn't find a way that shows how to map the data using the Client

CodePudding user response:

Searching with some posts and blog articles,the right way to do it is using: indices.create then, you need to use putMapping. The code will look like:

this.elasticSearch.indices.create({index: 'products'});
this.elasticSearch.indices.putMapping({
                index: 'products',
                body:{
                    properties:{
                        name:     { type: 'text' },
                        material:    { type: 'text' },
                        observation:        { type: 'flattened' },

                    }
                }
            });
  • Related