Home > Net >  ElasticSearch Define Custom Mapping Conflict With Default `_doc` Mapping
ElasticSearch Define Custom Mapping Conflict With Default `_doc` Mapping

Time:01-31

this problem happen when try to create custom mapping type. after create custom mapping for first insert elastic want to create _doc mapping type and conflict happen here.

in first step i create a mapping

{
    "mappings": {
        "properties": {
            "field1": {
                "type": "keyword"
            },
            "field2": {
                "type": "short",
                "index": false
            }
        }
    }
}

and into the second step put first data in elastic

curl --location --request PUT 'localhost:9200/myindex/mydoc/1

{
    "field1": "first info",
    "field2": 0
}

but in second step when i try to put first data into the elastic encounter following error

 "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Mapper for [field2] conflicts with existing mapper:\n\tCannot update parameter [index] from [false] to [true]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "Mapper for [field2] conflicts with existing mapper:\n\tCannot update parameter [index] from [false] to [true]"
    },
    "status": 400

seems to in this step elastic try to create dynamic mapping and this mapping want to create field2 as index field.

also according to another answers i used from dynamic: false or dynamic: "strict" setting in mapping like follow block code but it is not work

{
    "mappings": {
        "dynamic": false,
        "properties": {
            "field1": {
                "type": "keyword"
            },
            "field2": {
                "type": "short",
                "index": false
            }
        }
    }
}

this error also happen for type of fields. for example my field type is "keyword" but on auto mapping try to convert this type to "text"!

CodePudding user response:

dynamic parameter of Elasticsearch, have default value as true. When trying to replicate your issue, I followed the below steps

  1. Create a new index with the given index mapping
PUT <index-name>
{
  "mappings": {
    "properties": {
      "field1": {
        "type": "keyword"
      },
      "field2": {
        "type": "short",
        "index": false
      }
    }
  }
}
  1. Index data into the new index. According to the question, I should not be able to index the data, and elasticsearch will return mapper parsing exception.
POST <index-name>/_doc/1
{
  "field1": "first info",
  "field2": 0
}

But following the above steps, I am able to successfully, index the data in the index.


The index option controls whether field values are indexed. It accepts true or false and defaults to true. Fields that are not indexed are not queryable.

The error which you are getting will occur only when you are trying to update the index param of the field2.

Initially, when you have created the index mapping with field2 having index param set to false, this cannot be updated to true. (this is referred to as conflicting changes)

If you want to update the index param, then you have to create a new index with new index mapping, and index the documents in it, which can be done using reindex API.

CodePudding user response:

instead of create custom explicit mapping can define a mapping template like following code

curl --location --request PUT 'localhost:9200/_template/my-custom-type?include_type_name=true' \
--header 'Content-Type: application/json' \
--data-raw '{
    "index_patterns": [
        "my-index"
    ],
    "mappings": {
        "my-custom-type": {
            "properties": {
                "field1": {
                    "type": "keyword"
                },
                "field2": {
                    "type": "short",
                    "index": false
                }
            }
        }
    }
}'

after do this not exist any mapping yet but with first insert, mapping will be created according to above structure!

curl --location --request PUT 'localhost:9200/my-index/my-custom-type/1' \
--header 'Content-Type: application/json' \
--data-raw '{
    "field1": "SaeedMozaffari",
    "field2": 0
}'

with this trick elastic don't create _doc mapping so don't update any type in your mapping so this error will not be happen!

  •  Tags:  
  • Related