Skip to content

Elasticsearch Mapping Conflict Error Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Elasticsearch Mapping Conflict Error Fix. We cover key concepts, practical examples, and best practices.

Elasticsearch throws mapper_<a href="/compiler-design/syntax-analysis/">Parsing</a>_exception or illegal_argument_exception: cannot be changed from type [...] to [...] — you tried to index a document with a field type that conflicts with the existing mapping.

The Problem

// First document indexed — "count" is long
POST /orders/_doc/1
{ "count": 42 }

// Second document — "count" is text — FAILS
POST /orders/_doc/2
{ "count": "forty-two" }
{
  "error": {
    "type": "mapper_parsing_exception",
    "reason": "failed to parse field [count] of type [long]"
  }
}

After the first document, Elasticsearch inferred count as a long. The second document tries to index a string value into a long field, which is impossible.

Step-by-Step Fix

1. Use explicit mappings from the start

PUT /orders
{
  "mappings": {
    "properties": {
      "count": { "type": "integer" },
      "price": { "type": "float" },
      "status": { "type": "keyword" },
      "description": { "type": "text" },
      "created_at": { "type": "date" }
    }
  }
}

2. Fix the conflicting document

// Convert the value server-side before indexing
POST /orders/_doc/2
{ "count": 42 }

3. Reindex with a corrected mapping

PUT /orders_v2
{
  "mappings": {
    "properties": {
      "count": { "type": "text" },
      "count_raw": { "type": "keyword" }
    }
  }
}

POST /_reindex
{
  "source": { "index": "orders" },
  "dest": { "index": "orders_v2" }
}

// Then alias
POST /_aliases
{
  "actions": [
    { "add": { "index": "orders_v2", "alias": "orders" } },
    { "remove": { "index": "old-orders", "alias": "orders" } }
  ]
}

4. Use dynamic templates for flexible schemas

PUT /logs
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings_as_keywords": {
          "match_mapping_type": "string",
          "mapping": { "type": "keyword" }
        }
      },
      {
        "longs_as_integers": {
          "match_mapping_type": "long",
          "mapping": { "type": "integer" }
        }
      }
    ]
  }
}

5. Use runtime fields for query-time type conversion

PUT /orders/_mapping
{
  "runtime": {
    "count_as_string": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['count'].toString())"
      }
    }
  }
}

Expected output after fix:

{
  "acknowledged": true,
  "errors": false,
  "items": [
    { "index": { "_id": "1", "status": 201 } },
    { "index": { "_id": "2", "status": 201 } }
  ]
}

Prevention Tips

  • Define explicit mappings before indexing any data
  • Use dynamic: "strict" to reject unknown fields
  • Use dynamic templates for flexible schemas
  • Always validate data types client-side before indexing
  • Use index templates for consistent mappings across indices

Common Mistakes with mapping error

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world ELASTICSEARCH code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Can I change a field type after data is indexed?

No. Elasticsearch does not support changing the type of an existing field. You must create a new index with the correct mapping, reindex the data, and swap the alias. The only exception is adding new fields to an existing mapping.

What's the best way to handle mixed-type data?

Use ignore_malformed: true in the mapping to skip documents with incompatible types instead of failing the entire bulk request. Better yet, validate and coerce data types in your application before sending to Elasticsearch.

How do I see the current mapping of an index?

Use GET /my-index/_mapping. This returns the full mapping for all fields. Use GET /my-index/_field_caps?fields=count to see the type capabilities of a specific field across multiple indices.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro