Is a full reindex required after adding a new field to Opensearch mapping? (Searchkick)

36 views Asked by At

I am using the Searchkick gem to communicate with Opensearch.

The Searchkick docs state:

Control what data is indexed with the search_data method. Call Product.reindex after changing this method.

It makes sense to me that a full reindex would be necessary if I changed an existing mapping, but it is not clear to me that a full reindex is necessary if I add a new attribute to the mapping, reindex a subset of documents, and expect only documents from among the reindexed ones to be returned in a search.

This approach seems supported by the Opensearch docs, which state (emphasis mine):

If you want to create or add mappings and fields to an index, you can use the put mapping API operation. For an existing mapping, this operation updates the mapping.

You can’t 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.

Of course, any record that is not reindexed to contain data for the new attribute will not be returned in any searches against that attribute, but other than that, does partially reindexing a subset of documents after adding a new attribute to search_data present any problems?

In my local testing, taking the following actions works as expected:

  • begin with a model that includes searchkick and has an associated index and mapping
  • add a new attribute to search_data for that model
  • reindex a single record, model.reindex
  • see that the mapping is correctly updated with the new attribute
  • see that Model.search(where: { new_attribute: 'foo' }) returns the expected result - the single document I reindexed with that data
  • see that Model.search(where: { existing_attribute: 'bar' }) returns the expected result - the universe of documents who fit the pre-existing query

This suggests it can be done, but I am concerned I am missing something, as I have not found any sources describing an approach like this.

0

There are 0 answers