Elastic: How to correct the auto generated mapping?

I'm using Elastic cloud hosted in Azure and use NEST for the client. I have a part of auto generated mapping that I need to change from

    "Bonus" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },

to

    "Bonus" : {
      "properties" : {
        "Amount" : {
          "properties" : {
            "Value" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "PayrollSyncDateTime" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },

When I tried to update the mapping, I get illegal_argument_exception error with the message "can't merge a non object mapping [activityData.Bonus] with an object mapping" . How can I correct the auto generated mapping?

Hi @Gopala00 Welcome to the Community.

Think of a mapping as the schema.

A couple fundamental rules...

  1. You can not change a mapping of an existing field on an existing index that is what you are trying to do and getting an error. (You are trying to change a field into and object)

  2. In general You can ADD a mapping to an existing Index as long as it does not conflict with an existing field / object.

  3. In general / best practice you will want to create an index template with the correct mapping so that when you create an index by writing a document to the index the correct mappings will be applied.

  4. If you do not want to create a template you can manually create the mappings for an empty index before you start writing documents.

  5. Generally you do not need / want both the keyword and text data types for a fields. Think of keyword as Categories, Keyword, Types something you filter and aggregate on and text as fill text search

Mappings Docs

Template Docs

Thanks @stephenb for your reply. Is it possible to change the mapping for Bonus to ignore in the index? Will it remove Bonus data from index so that I can redefine the mapping OR will it affect the future data only? What are the steps involved to remove Bonus from the index so that I can redefine mapping?

I am unclear what that means .. apologies.. If you mean can you remove a mapping and will it automatically erase the data .. the answer is no and no.

You can not remove a field mapping from an index.

You can not actually remove a field from an index without reindex.

You can update each document with a new document without that fields ... but the field will still be in the mapping in the index ... and thus you can not change the mapping.

If you really want it gone you need to define a new index with the correct mapping.

2 Likes

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