Skip to content

Conversation

@natea
Copy link

@natea natea commented Aug 15, 2025

Summary

This PR fixes issue #112812 by adding validation to detect when copy_to targets a non-existent field when dynamic mappings are disabled (dynamic: false).

Problem

When using copy_to with a non-existent target field and dynamic mappings disabled, Elasticsearch would silently fail to copy the field value without any error or warning. This made it difficult to debug mapping issues.

Example

// Mapping with dynamic: false
{
  "dynamic": false,
  "properties": {
    "source_field": {
      "type": "text",
      "copy_to": "non_existent_field"  // This field doesn't exist!
    }
  }
}

// Before: Silently accepted, no data copied
// After: Throws IllegalArgumentException with clear error message

Solution

Added validation in FieldMapper.java to check if the target field exists when dynamic mappings are disabled. The validation provides a clear error message indicating which field is missing and why it cannot be created.

Changes

  • FieldMapper.java: Added validation logic in parseCopyFields() method (lines 335-353)
  • CopyToMapperTests.java: Added 3 comprehensive test methods covering various scenarios:
    • Test copying to non-existent field with dynamic=false (should fail)
    • Test copying to existing field with dynamic=false (should succeed)
    • Test that dynamic=true still works as before (auto-creates fields)

Error Message

When validation fails, users now get a clear error message:

Cannot copy to field [target_field] because it does not exist and dynamic mappings are disabled

Testing

  • ✅ All existing copy_to tests pass
  • ✅ Added 3 unit test methods covering edge cases
  • ✅ Tests verify proper validation when dynamic=false
  • ✅ Tests verify backward compatibility when dynamic=true

Related Issues

Fixes #112812

Checklist

  • I have signed the Contributor License Agreement
  • I have run the tests locally and they pass
  • I have added tests that prove my fix is effective
  • My changes follow the existing code style
  • Error messages are clear and actionable

🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

…stic#112812)

This change adds validation to detect when copy_to targets a non-existent
field when dynamic mappings are disabled (dynamic=false). Previously, this
configuration would silently fail to copy values at runtime.

Changes:
- Added validation in FieldMapper.validate() to check if copy_to target
  fields exist when dynamic=false
- Throws IllegalArgumentException with clear error message when validation fails
- Added comprehensive unit tests covering the new validation logic

The validation only applies when dynamic=false since other dynamic settings
(true, strict, runtime) have different behaviors for handling unknown fields.

Closes elastic#112812

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@elasticsearchmachine elasticsearchmachine added needs:triage Requires assignment of a team area label v9.2.0 external-contributor Pull request authored by a developer outside the Elasticsearch team labels Aug 15, 2025
Exclude .claude-flow/, .swarm/ directories and temporary test/analysis files
from version control to keep the repository clean.
@szybia szybia added :Search Foundations/Mapping Index mappings, including merging and defining field types and removed needs:triage Requires assignment of a team area label labels Aug 18, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch label Aug 18, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-foundations (Team:Search Foundations)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contributor Pull request authored by a developer outside the Elasticsearch team :Search Foundations/Mapping Index mappings, including merging and defining field types Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch v9.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect when copying to a non-existent field when dynamic mappings are disabled

3 participants