kunal adhikari
kunal adhikari

Reputation: 13

Custom mapping type in Elasticsearch 7.3.2 using nodejs

I am trying to create custom mapping type in elasticsearch 7.3.2 using nodejs client!.But it's only creating default _doc type mapping.When I provide custom doc type it throws me an error that you should give include_type_name = true but in elasticsearch 7.3.2 mapping type has been deprecated!

I have already tried giving custom mapping type with include_type_name=true.

        var indexName = req.body.indexName;
        var mappingType = req.body.mappingType;
        const mapping = {
            properties: {
                name: {
                    type: "text",
                    analyzer: "autocomplete",
                    search_analyzer: "standard"
                },
                createdate: {
                    type: "date"
                }
            }
        }
        await esClient.indices.putMapping({
            index: indexName,
            type: mappingType,
            include_type_name:true,
            body: mapping
        }, (err: any, resp: any, status: any) => {
            if (err) {
                return res.status(400).json({ status, err });
            }
            res.status(200).json({ resp, status });
        })
    }

Expected:I am trying to create custom mapping type because when I ingest the data from Mongodb by using Transporter it takes the mapping type as the name of the collection and the mapping that I am creating which has a default _doc type mapping. Error:"Rejecting mapping update to [tag] as the final mapping would have more than 1 type: [_doc, tags]" tags(collection name

Upvotes: 1

Views: 1651

Answers (1)

Val
Val

Reputation: 217424

Yes, that's an expected behavior in ES 7 (onwards), only a single mapping type is supported and it's is called _doc by default.

As you've noticed, you can change that behavior in ES7 by adding include_type_name=true to your call. (That won't work in ES8 anymore, though).

The thing is that you can only do it at index creation time, i.e. you cannot modify the mapping type name after the index has been created. So, since you don't control the index creation from MongoDB, all you can do is to create an index template in ES that will kick in once your index will be created.

Run this in Kibana Dev Tools before the index creation:

PUT _template/tags?include_type_name=true
{
  "index_patterns": ["my-index*"],            <--- change this pattern to match yours
  "mappings": {
    "tags": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "autocomplete",
          "search_analyzer": "standard"
        },
        "createdate": {
          "type": "date"
        }
      }
    }
  }
}

That way, when the my-index index gets created, it will automatically get a mapping type called tags instead of the default _doc one.

Upvotes: 2

Related Questions