Problem/Motivation

Changing field types gives the following error:

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"mapper [title] cannot be changed from type [text] to [keyword]"}],"type":"illegal_argument_exception","reason":"mapper [title] cannot be changed from type [text] to [keyword]"},"status":400}

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#9 3266032-9.patch419 byteskim.pepper
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

kim.pepper created an issue. See original summary.

kim.pepper’s picture

According to the docs, you can only update field types _before_ any data is indexed.

If you want to update an index’s mappings to add or update field types after index creation, you can do so with the update mapping API operation.

Note that you cannot use this operation to update mappings that already map to existing data in the index. You must first create a new index with your desired mappings, and then use the reindex API operation to map all the documents from your old index to the new index. If you don’t want any downtime while reindexing your indices, you can use aliases.

I think the best we can do is catch this error and display a helpful message to the user.

kim.pepper’s picture

kim.pepper’s picture

We might be able to use \Drupal\search_api\Item\FieldInterface::setTypeLocked() to indicate the field type cannot be changed.

acbramley’s picture

So it is possible to change field mappings if you clear the index and re-index everything. The issue is that once you change a field mapping search_api adds a new task to the DB to update that mapping. Doing any other action on the index tries to execute that task first which results in an exception and doesn't delete the task. It means that it's impossible to clear the index after changing a field's type. The only workaround currently is to manually delete all tasks (e.g delete from search_api_task;) and then clear the index. After doing that you're able to index again with the new field mapping.

I wonder if we can simply hook into the updateMapping process and re-create the index first?

kim.pepper’s picture

Status: Active » Needs review

Add a reindex() call when updating the mapping.

kim.pepper’s picture

Marking for reindex isn't enough. We need to recreate in the index.

kim.pepper’s picture

StatusFileSize
new419 bytes

Testing a patch file.

acbramley’s picture

Status: Needs review » Reviewed & tested by the community

Works for me!

  • kim.pepper committed 4894432 on 1.x
    Issue #3266032 by kim.pepper, acbramley: Cannot change field mapping
    
kim.pepper’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 1.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.