You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by js...@apache.org on 2022/07/12 16:15:35 UTC

[unomi] branch UNOMI-603-migration-profiles updated: feedbacks

This is an automated email from the ASF dual-hosted git repository.

jsinovassinnaik pushed a commit to branch UNOMI-603-migration-profiles
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/UNOMI-603-migration-profiles by this push:
     new 55a0a13f8 feedbacks
55a0a13f8 is described below

commit 55a0a13f80a7086934a086c66e1240aaec2b8e8f
Author: jsinovassin <js...@jahia.com>
AuthorDate: Tue Jul 12 18:10:20 2022 +0200

    feedbacks
---
 .../shell/migration/utils/MigrationUtils.java      | 30 +++++++++++++++++++---
 .../migrate-2.0.0-03-profileReindex.groovy         |  2 +-
 .../2.0.0/migrate_existing_interests.painless}     | 17 ++++++++----
 3 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/MigrationUtils.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/MigrationUtils.java
index 809e8dbc8..f055be2d3 100644
--- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/MigrationUtils.java
+++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/MigrationUtils.java
@@ -22,8 +22,11 @@ import org.apache.http.impl.client.CloseableHttpClient;
 import org.json.JSONObject;
 import org.osgi.framework.BundleContext;
 
+import java.io.BufferedReader;
+import java.io.DataInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
 
@@ -57,6 +60,24 @@ public class MigrationUtils {
         }
     }
 
+    public static String getFileWithoutComments(BundleContext bundleContext, final String resource) {
+        final URL url = bundleContext.getBundle().getResource(resource);
+        try (InputStream stream = url.openStream()) {
+            DataInputStream in = new DataInputStream(stream);
+            BufferedReader br = new BufferedReader(new InputStreamReader(in));
+            String line;
+            StringBuilder value = new StringBuilder();
+            while ((line = br.readLine()) != null) {
+                if (!line.startsWith("/*") && !line.startsWith(" *") && !line.startsWith("*/"))
+                    value.append(line);
+            }
+            in.close();
+            return value.toString();
+        } catch (final Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     public static void reIndex(CloseableHttpClient httpClient, BundleContext bundleContext, String esAddress, String indexName,
             String newIndexSettings, String painlessScript) throws IOException {
         String indexNameCloned = indexName + "-cloned";
@@ -69,9 +90,8 @@ public class MigrationUtils {
                 .replace("#maxDocValueFieldsSearch", originalIndexSettings.getJSONObject(indexName).getJSONObject("settings").getJSONObject("index").getString("max_docvalue_fields_search"))
                 .replace("#mappingTotalFieldsLimit", originalIndexSettings.getJSONObject(indexName).getJSONObject("settings").getJSONObject("index").getJSONObject("mapping").getJSONObject("total_fields").getString("limit"));
         String reIndexRequest = resourceAsString(bundleContext, "requestBody/2.0.0/base_reindex_request.json")
-                .replace("#source", indexNameCloned)
-                .replace("#dest", indexName)
-                .replace("#painless", StringUtils.isNotEmpty(painlessScript)? ", \"script\":" + painlessScript: "");
+                .replace("#source", indexNameCloned).replace("#dest", indexName)
+                .replace("#painless", StringUtils.isNotEmpty(painlessScript) ? getScriptPart(painlessScript) : "");
 
         String setIndexReadOnlyRequest = resourceAsString(bundleContext, "requestBody/2.0.0/base_set_index_readonly_request.json");
 
@@ -88,4 +108,8 @@ public class MigrationUtils {
         // Remove clone
         HttpUtils.executeDeleteRequest(httpClient, esAddress + "/" + indexNameCloned, null);
     }
+
+    private static String getScriptPart(String painlessScript) {
+        return ", \"script\": {\"source\": \"" + painlessScript + "\", \"lang\": \"painless\"}";
+    }
 }
diff --git a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-03-profileReindex.groovy b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-03-profileReindex.groovy
index 44f2731f1..5eaa63e0e 100644
--- a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-03-profileReindex.groovy
+++ b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-03-profileReindex.groovy
@@ -19,4 +19,4 @@ import org.apache.unomi.shell.migration.utils.MigrationUtils
 
 String newIndexSettings = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.0.0/profile_index.json");
 MigrationUtils.reIndex(httpClient, bundleContext, migrationConfig.get("esAddress"), migrationConfig.get("indexPrefix") + "-profile",
-        newIndexSettings, MigrationUtils.resourceAsString(bundleContext, "requestBody/2.0.0/update_profile_interests.json"))
+        newIndexSettings, MigrationUtils.getFileWithoutComments(bundleContext, "requestBody/2.0.0/migrate_existing_interests.painless"))
diff --git a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-03-profileReindex.groovy b/tools/shell-commands/src/main/resources/requestBody/2.0.0/migrate_existing_interests.painless
similarity index 63%
copy from tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-03-profileReindex.groovy
copy to tools/shell-commands/src/main/resources/requestBody/2.0.0/migrate_existing_interests.painless
index 44f2731f1..93fd9cec6 100644
--- a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.0.0-03-profileReindex.groovy
+++ b/tools/shell-commands/src/main/resources/requestBody/2.0.0/migrate_existing_interests.painless
@@ -1,5 +1,3 @@
-import org.apache.unomi.shell.migration.utils.MigrationUtils
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -17,6 +15,15 @@ import org.apache.unomi.shell.migration.utils.MigrationUtils
  * limitations under the License.
  */
 
-String newIndexSettings = MigrationUtils.resourceAsString(bundleContext, "requestBody/2.0.0/profile_index.json");
-MigrationUtils.reIndex(httpClient, bundleContext, migrationConfig.get("esAddress"), migrationConfig.get("indexPrefix") + "-profile",
-        newIndexSettings, MigrationUtils.resourceAsString(bundleContext, "requestBody/2.0.0/update_profile_interests.json"))
+List interests = new ArrayList();
+
+if (ctx._source.properties.interests != null && !(ctx._source.properties.interests instanceof List)){
+    for (entry in ctx._source.properties.interests.entrySet()){
+      Map interest = new HashMap();
+      interest.put('key',entry.getKey());
+      interest.put('value', entry.getValue());
+      interests.add(interest);
+    }
+    ctx._source.properties.remove('interests');
+    ctx._source.properties.put('interests',interests);
+}