jsinovassin commented on code in PR #744:
URL: https://github.com/apache/unomi/pull/744#discussion_r2651294355


##########
tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-3.1.0-00-fixProfileNbOfVisits.groovy:
##########
@@ -0,0 +1,98 @@
+import groovy.json.JsonSlurper
+import org.apache.unomi.shell.migration.service.MigrationContext
+import org.apache.unomi.shell.migration.utils.HttpUtils
+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
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+MigrationContext context = migrationContext
+String esAddress = context.getConfigString("esAddress")
+String indexPrefix = context.getConfigString("indexPrefix")
+def jsonSlurper = new JsonSlurper()
+
+context.performMigrationStep("3.1.0-fix-profile-nbOfVisits", () -> {
+    String profileIndex = "${indexPrefix}-profile"
+    String sessionIndex = "${indexPrefix}-session-*"
+
+    context.printMessage("Starting migration to fix Profile.nbOfVisits field")
+
+    // First step: Copy nbOfVisits to totalNbOfVisits for all profiles
+    context.printMessage("Step 1: Copying nbOfVisits to totalNbOfVisits")
+    String copyScript = MigrationUtils.getFileWithoutComments(bundleContext, 
"requestBody/3.1.0/copy_nbOfVisits_to_totalNbOfVisits.painless")
+    String copyRequestBody = MigrationUtils.resourceAsString(bundleContext, 
"requestBody/3.1.0/profile_copy_nbOfVisits_request.json")
+    MigrationUtils.updateByQuery(context.getHttpClient(), esAddress, 
profileIndex, copyRequestBody.replace('#painless', copyScript))
+
+    context.printMessage("Step 1 completed: nbOfVisits copied to 
totalNbOfVisits")
+
+    // Second step: Update nbOfVisits with actual session count for each 
profile
+    context.printMessage("Step 2: Updating nbOfVisits with actual session 
count")
+
+    String scrollQuery = MigrationUtils.resourceAsString(bundleContext, 
"requestBody/3.1.0/profile_scroll_query.json")
+    int profilesProcessed = 0
+    int profilesUpdated = 0
+
+    // Scroll through all profiles
+    MigrationUtils.scrollQuery(context.getHttpClient(), esAddress, 
"/${profileIndex}/_search", scrollQuery, "5m", (hits) -> {

Review Comment:
   In profile_scroll_query.json, profiles are currently retrieved in batches of 
100, resulting in a bulk request for every 100 documents. Increasing the batch 
size to 1,000 would reduce the number of requests and improve throughput. 
Elasticsearch can handle this volume per request, so processing 1,000 documents 
at a time should significantly shorten the migration duration.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to