<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: ElasticSearch fails to recreate index due to data Field type change</title><link>https://answers.axonivy.com/questions/3791/elasticsearch-fails-to-recreate-index-due-to-data-field-type-change</link><description>&lt;p&gt;We converted a field of a BusinessData object from &lt;code&gt;long&lt;/code&gt; to &lt;code&gt;String&lt;/code&gt;. Now we face index recreation problems after upgrading the ivy engine to a new version.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;2019-05-06 15:36:36.038 ERROR [ch.ivyteam.ivy.business.data.store.search.internal.BusinessDataSearchIndecesRecreator] [ivy immediate job pool-thread-1] [executionContext=SYSTEM] 
  Recreation of business data search index failed
    [errorId=16A8D5C46C63FD9B, executionContext=SYSTEM]
    ch.ivyteam.ivy.business.data.store.search.internal.elasticsearch.ElasticsearchException: Elasticsearch add documents to index failed because of: One or more of the items in the Bulk request failed, check BulkResult.getItems() for more information.
    bulk error: id=f08fe3d90a454be4b95a0b59785686ff, index=ivy.businessdata-com.axonivy.hrwf.vea.arbeitsvertragsaenderungencasemodel, error={"type":"illegal_argument_exception","reason":"mapper [antrag.lohnUndOderArbeitszeitaenderung.baseSalaryPtNew] cannot be changed from type [float] to [long]"}
    bulk error: id=b15b99eb022b4e1f95cd1b34ee03ae2f, index=ivy.businessdata-com.axonivy.hrwf.vea.arbeitsvertragsaenderungencasemodel, error={"type":"mapper_parsing_exception","reason":"failed to parse [antrag.lohnUndOderArbeitszeitaenderung.expensesPtCurrent]","caused_by":{"type":"number_format_exception","reason":"For input string: \"CHF500\""}}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How can we get a proper data store and make the index recreation running?&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/3791/elasticsearch-fails-to-recreate-index-due-to-data-field-type-change" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 07 May 2019 04:28:40 -0400</lastBuildDate><item><title>Answer by Reguel Wermelinger</title><link>https://answers.axonivy.com/questions/3791/elasticsearch-fails-to-recreate-index-due-to-data-field-type-change/3792</link><description>&lt;p&gt;The error occurs as ElasticSearch determines the type of a field once the first non empty value is defined. So if a BusinessData object with &lt;code&gt;long expensesPtCurrent&lt;/code&gt; is once persistent, the type can be changed to &lt;code&gt;String&lt;/code&gt; without facing compatiblity issues.&lt;/p&gt;
&lt;p&gt;To takle the problem I'd write a conversion in Java to:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;read old values (where the field was a long) as raw JSON&lt;/li&gt;
&lt;li&gt;load and update the persistent businssData object and apply a compatible value in new format (String)&lt;/li&gt;
&lt;li&gt;store the updated value in BusinessData repo&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Sample:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public static void convertLegacy() throws IOException
  {
      ObjectMapper mapper = new ObjectMapper();
      BusinessDataRepository repository = BusinessDataRepository.get();
      List&amp;lt;BusinessDataInfo&amp;lt;Antrag&amp;gt;&amp;gt; infos = repository.search(Antrag.class).execute().getAllInfos();
      for(BusinessDataInfo&amp;lt;Antrag&amp;gt; info : infos)
      {
          String json = BusinessDataSearchIndex.toSearchIndexJson(info.getRawValue());
          JsonNode node = mapper.readTree(json); // JSON = simple API to work with JSON objects
          JsonNode ptCurr = node.get("lohnUndOderArbeitszeitaenderung").get("expensesPtCurrent");
          if (ptCurr.isLong())
          {
              // legacy value
              long oldVal = ptCurr.asLong();
              Antrag antrag = repository.find(info.getId(), Antrag.class);
              antrag.setExpensesCurrent(String.valueOf(oldVal)+ " CHF");
              repository.save(antrag);
          }
      }
  }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To avoid the problem at all:&lt;/p&gt;
&lt;p&gt;Do not change the type of already persistent data. But add a new replacement field. Optionally with fallback support to read legacy values. See: &lt;a href="https://answers.axonivy.com/questions/2606/can-not-persist-business-data-to-repo-after-changing-data-type"&gt;https://answers.axonivy.com/questions/2606/can-not-persist-business-data-to-repo-after-changing-data-type&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Tue, 07 May 2019 04:28:40 -0400</pubDate><guid>https://answers.axonivy.com/questions/3791/elasticsearch-fails-to-recreate-index-due-to-data-field-type-change/3792</guid></item></channel></rss>