使用 Elasticsearch 和 Nestjs 索引数据时如何添加映射字段

How to add mapping fields when indexing data with Elasticsearch and Nestjs

我正在使用 ElasticSearch. I've been able to insert some data with the Client wrapped from oficial elasticsearch lib 构建 NestJS 应用程序。由于我的数据将具有特定字段,其中一个将是一个对象(该对象可能具有多个字段,包括对象),因此我想从将添加到该索引的数据中映射整个字段。 此时我的代码如下所示:

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

映射将是:名称:字符串,material:字符串,观察:扁平化。我找不到显示如何使用 Client

映射数据的方法

搜索一些帖子和博客文章,正确的方法是使用: indices.create then, you need to use putMapping。代码将如下所示:

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

                    }
                }
            });