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

mridulpathak pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-plugins.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 8f6d60a62 Fixed: A database error while indexing deletes the product 
or content from the Lucene index (OFBIZ-13698)
8f6d60a62 is described below

commit 8f6d60a628412b92bf095fce0e632ce7d98d3ba3
Author: Mridul Pathak <[email protected]>
AuthorDate: Mon Oct 19 21:16:09 2026 +0530

    Fixed: A database error while indexing deletes the product or content from 
the Lucene index (OFBIZ-13698)
    
    ProductDocument and ContentDocument returned null when the record could not 
be read, which DocumentIndexer took as a removed record, deleting its document 
from the index. prepareDocument() now throws when the document cannot be 
prepared, and DocumentIndexer logs the error and leaves the index unchanged for 
that record.
---
 .../ofbiz/content/search/ContentDocument.java      | 28 +++-----------
 .../ofbiz/content/search/DocumentIndexer.java      | 45 ++++++++++++++--------
 .../ofbiz/content/search/LuceneDocument.java       |  9 ++++-
 .../ofbiz/content/search/ProductDocument.java      |  5 +--
 4 files changed, 46 insertions(+), 41 deletions(-)

diff --git 
a/lucene/src/main/java/org/apache/ofbiz/content/search/ContentDocument.java 
b/lucene/src/main/java/org/apache/ofbiz/content/search/ContentDocument.java
index 27b6a85dc..5f78028a0 100644
--- a/lucene/src/main/java/org/apache/ofbiz/content/search/ContentDocument.java
+++ b/lucene/src/main/java/org/apache/ofbiz/content/search/ContentDocument.java
@@ -30,14 +30,12 @@ import org.apache.lucene.document.Field.Store;
 import org.apache.lucene.document.StringField;
 import org.apache.lucene.document.TextField;
 import org.apache.lucene.index.Term;
-import org.apache.ofbiz.base.util.Debug;
 import org.apache.ofbiz.base.util.GeneralException;
 import org.apache.ofbiz.base.util.StringUtil;
 import org.apache.ofbiz.base.util.UtilMisc;
 import org.apache.ofbiz.base.util.UtilValidate;
 import org.apache.ofbiz.content.content.ContentWorker;
 import org.apache.ofbiz.entity.Delegator;
-import org.apache.ofbiz.entity.GenericEntityException;
 import org.apache.ofbiz.entity.GenericValue;
 import org.apache.ofbiz.service.LocalDispatcher;
 
@@ -47,7 +45,6 @@ import org.apache.ofbiz.service.LocalDispatcher;
 
 public class ContentDocument implements LuceneDocument {
 
-    private static final String MODULE = ContentDocument.class.getName();
     private final Term documentIdentifier;
     private final LocalDispatcher dispatcher;
     private final GenericValue content;
@@ -69,7 +66,7 @@ public class ContentDocument implements LuceneDocument {
     }
 
     @Override
-    public Document prepareDocument(Delegator delegator) {
+    public Document prepareDocument(Delegator delegator) throws 
GeneralException {
         Document doc;
         // make a new, empty document
         doc = new Document();
@@ -107,15 +104,9 @@ public class ContentDocument implements LuceneDocument {
         return doc;
     }
 
-    private boolean indexDataResource(Document doc) {
+    private boolean indexDataResource(Document doc) throws GeneralException {
         String contentId = content.getString("contentId");
-        GenericValue dataResource;
-        try {
-            dataResource = content.getRelatedOne("DataResource", true);
-        } catch (GenericEntityException e) {
-            Debug.logError(e, MODULE);
-            return false;
-        }
+        GenericValue dataResource = content.getRelatedOne("DataResource", 
true);
         if (dataResource == null) {
             return false;
         }
@@ -131,21 +122,14 @@ public class ContentDocument implements LuceneDocument {
         String text;
         try {
             text = ContentWorker.renderContentAsText(dispatcher, contentId, 
null, locale, mimeTypeId, true);
-        } catch (GeneralException | IOException e) {
-            Debug.logError(e, MODULE);
-            return false;
+        } catch (IOException e) {
+            throw new GeneralException("Could not render content " + contentId 
+ " as text", e);
         }
         if (UtilValidate.isNotEmpty(text)) {
             Field field = new TextField("content", text, Store.NO);
             doc.add(field);
         }
-        List<GenericValue> featureDataResourceList;
-        try {
-            featureDataResourceList = 
content.getRelated("ProductFeatureDataResource", null, null, true);
-        } catch (GenericEntityException e) {
-            Debug.logError(e, MODULE);
-            return false;
-        }
+        List<GenericValue> featureDataResourceList = 
content.getRelated("ProductFeatureDataResource", null, null, true);
         List<String> featureList = new ArrayList<>();
         for (GenericValue productFeatureDataResource : 
featureDataResourceList) {
             String feature = 
productFeatureDataResource.getString("productFeatureId");
diff --git 
a/lucene/src/main/java/org/apache/ofbiz/content/search/DocumentIndexer.java 
b/lucene/src/main/java/org/apache/ofbiz/content/search/DocumentIndexer.java
index ac319f551..0dbe1d3ec 100644
--- a/lucene/src/main/java/org/apache/ofbiz/content/search/DocumentIndexer.java
+++ b/lucene/src/main/java/org/apache/ofbiz/content/search/DocumentIndexer.java
@@ -25,6 +25,7 @@ import java.util.Map;
 import java.util.concurrent.LinkedBlockingQueue;
 
 import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.base.util.GeneralException;
 import org.apache.ofbiz.entity.Delegator;
 
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
@@ -90,7 +91,19 @@ public final class DocumentIndexer extends Thread {
                     break;
                 }
                 Term documentIdentifier = 
ofbizDocument.getDocumentIdentifier();
-                Document document = 
ofbizDocument.prepareDocument(this.delegator);
+                Document document = null;
+                boolean prepared = false;
+                try {
+                    document = ofbizDocument.prepareDocument(this.delegator);
+                    prepared = true;
+                } catch (GeneralException e) {
+                    // the record may still exist: keep its document in the 
index instead of deleting it
+                    Debug.logError(e, getName() + ": could not prepare Lucene 
document, index left unchanged for: " + ofbizDocument, MODULE);
+                }
+                if (!prepared && indexWriter == null) {
+                    // nothing to write and no pending changes to commit
+                    continue;
+                }
                 if (indexWriter == null) {
                     int lockRetries = 0;
                     while (indexWriter == null) {
@@ -121,23 +134,25 @@ public final class DocumentIndexer extends Thread {
                         }
                     }
                 }
-                try {
-                    if (document == null) {
-                        indexWriter.deleteDocuments(documentIdentifier);
-                        if (Debug.infoOn()) {
-                            Debug.logInfo(getName() + ": deleted Lucene 
document: " + ofbizDocument, MODULE);
-                        }
-                    } else {
-                        indexWriter.updateDocument(documentIdentifier, 
document);
-                        if (Debug.infoOn()) {
-                            Debug.logInfo(getName() + ": indexed Lucene 
document: " + ofbizDocument, MODULE);
+                if (prepared) {
+                    try {
+                        if (document == null) {
+                            indexWriter.deleteDocuments(documentIdentifier);
+                            if (Debug.infoOn()) {
+                                Debug.logInfo(getName() + ": deleted Lucene 
document: " + ofbizDocument, MODULE);
+                            }
+                        } else {
+                            indexWriter.updateDocument(documentIdentifier, 
document);
+                            if (Debug.infoOn()) {
+                                Debug.logInfo(getName() + ": indexed Lucene 
document: " + ofbizDocument, MODULE);
+                            }
                         }
+                    } catch (Exception e) {
+                        Debug.logError(e, getName() + ": error processing 
Lucene document: " + ofbizDocument, MODULE);
+                        continue;
                     }
-                } catch (Exception e) {
-                    Debug.logError(e, getName() + ": error processing Lucene 
document: " + ofbizDocument, MODULE);
-                    continue;
+                    uncommittedDocs++;
                 }
-                uncommittedDocs++;
                 if (uncommittedDocs == UNCOMMITTED_DOC_LIMIT || 
documentIndexQueue.peek() == null) {
                     // limit reached or queue empty, time to commit
                     try {
diff --git 
a/lucene/src/main/java/org/apache/ofbiz/content/search/LuceneDocument.java 
b/lucene/src/main/java/org/apache/ofbiz/content/search/LuceneDocument.java
index fd359a239..e32d4b60a 100644
--- a/lucene/src/main/java/org/apache/ofbiz/content/search/LuceneDocument.java
+++ b/lucene/src/main/java/org/apache/ofbiz/content/search/LuceneDocument.java
@@ -18,6 +18,7 @@
  
*******************************************************************************/
 package org.apache.ofbiz.content.search;
 
+import org.apache.ofbiz.base.util.GeneralException;
 import org.apache.ofbiz.entity.Delegator;
 
 import org.apache.lucene.document.Document;
@@ -27,5 +28,11 @@ public interface LuceneDocument {
 
     Term getDocumentIdentifier();
 
-    Document prepareDocument(Delegator delegator);
+    /**
+     * Prepares the Lucene document of the record.
+     * @param delegator the delegator to read the record with
+     * @return the document to index, or {@code null} when the record is to be 
removed from the index
+     * @throws GeneralException when the document cannot be prepared, the 
index is then left unchanged for this record
+     */
+    Document prepareDocument(Delegator delegator) throws GeneralException;
 }
diff --git 
a/lucene/src/main/java/org/apache/ofbiz/content/search/ProductDocument.java 
b/lucene/src/main/java/org/apache/ofbiz/content/search/ProductDocument.java
index f3ed1700b..032452803 100644
--- a/lucene/src/main/java/org/apache/ofbiz/content/search/ProductDocument.java
+++ b/lucene/src/main/java/org/apache/ofbiz/content/search/ProductDocument.java
@@ -143,7 +143,7 @@ public class ProductDocument implements LuceneDocument {
     }
 
     @Override
-    public Document prepareDocument(Delegator delegator) {
+    public Document prepareDocument(Delegator delegator) throws 
GenericEntityException {
         String productId = getDocumentIdentifier().text();
         try {
             GenericValue product = 
EntityQuery.use(delegator).from("Product").where("productId", 
productId).queryOne();
@@ -366,9 +366,8 @@ public class ProductDocument implements LuceneDocument {
                 return doc;
             }
         } catch (GenericEntityException e) {
-            Debug.logError(e, MODULE);
+            throw new GenericEntityException("Could not prepare the Lucene 
document of product " + productId, e);
         }
-        return null;
     }
 
     private Timestamp populateCategoryData(Document doc, GenericValue product) 
throws GenericEntityException {

Reply via email to