# How to use custom field type in mappings to achieve multiple type mappings within an index?

**URL:** <https://discuss.elastic.co/t/how-to-use-custom-field-type-in-mappings-to-achieve-multiple-type-mappings-within-an-index/219530>\
**Category:** Elasticsearch\
**Created:** [February 16, 2020, 5:02pm UTC](https://discuss.elastic.co/t/how-to-use-custom-field-type-in-mappings-to-achieve-multiple-type-mappings-within-an-index/219530 "2020-02-16T17:02:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prasad98](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prasad98/32/64837_2.png) [@prasad98](https://discuss.elastic.co/u/prasad98)\
**Post date:** [February 16, 2020, 5:02pm UTC](https://discuss.elastic.co/t/how-to-use-custom-field-type-in-mappings-to-achieve-multiple-type-mappings-within-an-index/219530/1 "2020-02-16T17:02:42Z")

</div>

Hi, I'm trying to migrate my elasticsearch from 2.4 to 7.5

I was working on fixing breaking changes in the mappings. This example here (for custom field type): [https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html) seems to be not working as it gives error for the `doc` field.

Here they ES blog has mentioned best use of custom type field: [https://www.elastic.co/blog/kibana-6-removal-of-mapping-types](https://www.elastic.co/blog/kibana-6-removal-of-mapping-types)

But I'm not able to figure it out, how should I create it ?

Based on this question - consider there are two types of mapping like `product` and `category`. I want them within 1 index only. How should I create it ?

Reference doubt on discussion forum: [How to avoid duplicate fields copied during bulk indexing?](https://discuss.elastic.co/t/how-to-avoid-duplicate-fields-copied-during-bulk-indexing/219528)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 16, 2020, 5:47pm UTC](https://discuss.elastic.co/t/how-to-use-custom-field-type-in-mappings-to-achieve-multiple-type-mappings-within-an-index/219530/2 "2020-02-16T17:47:42Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

So provide a typical example of your 2 types.

---

<div class="post-metadata">

**Author:** ![prasad98](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prasad98/32/64837_2.png) [@prasad98](https://discuss.elastic.co/u/prasad98)\
**Post date:** [February 16, 2020, 6:39pm UTC](https://discuss.elastic.co/t/how-to-use-custom-field-type-in-mappings-to-achieve-multiple-type-mappings-within-an-index/219530/3 "2020-02-16T18:39:47Z")

</div>

> [@prasad98](#):
>
> Based on this question - consider there are two types of mapping like `product` and `category` . I want them within 1 index only. How should I create it ?

@dadoonet I'm using GoLang client API. This one- [GitHub - olivere/elastic: Deprecated: Use the official Elasticsearch client for Go at https://github.com/elastic/go-elasticsearch](https://github.com/olivere/elastic/tree/release-branch.v7)

My mapping for single index including multiple types: `product` and `category`.

```auto
mappings := map[string]interface{}{
		"properties": map[string]interface{}{
			"category": map[string]interface{}{
				"properties": map[string]interface{}{
					"id": map[string]interface{}{"type": "long"},
					"name": map[string]interface{}{"type": "text"},
					"type": map[string]interface{}{"type": "text"},
					"image_version": map[string]interface{}{
						"type": "nested",
						"properties": map[string]interface{}{
							"image_height": map[string]interface{}{"type": "float"},
							"image_width": map[string]interface{}{"type": "float"},
						},
					},
				},
			},
			"product": map[string]interface{}{
				"properties": map[string]interface{}{
					"activity_name": map[string]interface{}{"type": "text"},
					"app_type": map[string]interface{}{"type": "text"},
				},
			},
		},
	}

```

When I try to bulk index the data, it creates a single index with mappings like this:

```auto
{
  "mappings": {
    "properties": { 
      "category": {
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "keyword"
          },
          "type": {
            "type": "text"
          },
          "image_version": {
            "type": "nested",
            "properties": {
              "image_height": {
                "type": "float"
              },
              "image_width": {
                "type": "float"
              }
            }
          }
        }
      },
      "id": {
        "type": "integer"
      },
      "name": {
        "type": "keyword"
      },
      "type": {
        "type": "text"
      },
      "image_version": {
        "type": "nested",
        "properties": {
          "image_height": {
            "type": "float"
          },
          "image_width": {
            "type": "float"
          }
        }
      },
      "product": {
        "properties": {
          "activity_name": {
            "type": "text"
          },
          "app_type": {
            "type": "text"
          }
        }
      }
    }
  }
}

```

As you can notice it is copying `category` fields data outside `category` root field too.

Why the fields inside `properties` are being copied outside `category` ? How should I avoid it? How should I define custom types?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 15, 2020, 6:39pm UTC](https://discuss.elastic.co/t/how-to-use-custom-field-type-in-mappings-to-achieve-multiple-type-mappings-within-an-index/219530/4 "2020-03-15T18:39:50Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
