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

snoopdave pushed a commit to branch roller-6.1.x
in repository https://gitbox.apache.org/repos/asf/roller.git


The following commit(s) were added to refs/heads/roller-6.1.x by this push:
     new a76037402 Return only published entries from search and drop the index 
entry when AtomPub saves a draft (#226)
a76037402 is described below

commit a7603740258c3ecb0f8d660ce1d876f1b806de7b
Author: David M. Johnson <[email protected]>
AuthorDate: Thu Oct 29 22:48:08 2026 -0400

    Return only published entries from search and drop the index entry when 
AtomPub saves a draft (#226)
    
    * Return only published entries from search and drop the index entry when 
AtomPub saves a draft
    * Page search results over published entries only; note the change in 
CHANGES.md
    * Stop loading search hits once the page is filled
    * Keep the index in line with saved and deleted entries in one place
---
 CHANGES.md                                         |   5 +
 .../weblogger/business/search/IndexManager.java    |  13 +++
 .../business/search/lucene/LuceneIndexManager.java |  88 ++++++++++------
 .../weblogger/ui/struts2/editor/EntryEdit.java     |   6 +-
 .../weblogger/ui/struts2/editor/EntryRemove.java   |  15 +--
 .../webservices/atomprotocol/EntryCollection.java  |  18 +---
 .../business/search/IndexManagerTest.java          |  24 +++++
 .../business/search/UpdateEntryIndexTest.java      |  63 ++++++++++++
 .../search/lucene/SearchHitPagingTest.java         | 112 +++++++++++++++++++++
 9 files changed, 281 insertions(+), 63 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 13306195d..047994d7f 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -88,6 +88,11 @@
   parsed. Author user names and comment author names and email addresses are
   no longer added to the search index. An index built by an earlier version
   is rebuilt in the background at the first startup after the upgrade.
+- **Search shows only published entries.** Weblog search returns entries that
+  are published at the time of the search, even if the search index still
+  holds an entry that has since gone back to draft. Updating an entry through
+  AtomPub so that it is no longer published also removes it from the index,
+  and so does deleting an entry through AtomPub.
 
 ### Bug fixes
 
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/business/search/IndexManager.java
 
b/app/src/main/java/org/apache/roller/weblogger/business/search/IndexManager.java
index 8a8a933f0..7d8781e25 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/business/search/IndexManager.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/business/search/IndexManager.java
@@ -62,6 +62,19 @@ public interface IndexManager {
     /** Remove entry from index, returns immediately and operates in 
background */
     void removeEntryIndexOperation(WeblogEntry entry) throws 
WebloggerException;
 
+    /**
+     * Bring the index in line with an entry that was saved: re-index it if it
+     * is published, else remove it. Returns immediately and operates in
+     * background.
+     */
+    default void updateEntryIndex(WeblogEntry entry) throws WebloggerException 
{
+        if (entry.isPublished()) {
+            addEntryReIndexOperation(entry);
+        } else {
+            removeEntryIndexOperation(entry);
+        }
+    }
+
     SearchResultList search(
         String term,
         String weblogHandle,
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/business/search/lucene/LuceneIndexManager.java
 
b/app/src/main/java/org/apache/roller/weblogger/business/search/lucene/LuceneIndexManager.java
index a2e734a16..ce58d1537 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/business/search/lucene/LuceneIndexManager.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/business/search/lucene/LuceneIndexManager.java
@@ -455,46 +455,60 @@ public class LuceneIndexManager implements IndexManager {
 
         List<WeblogEntryWrapper> results = new ArrayList<>();
 
-        // determine offset
-        int offset = pageNum * entryCount;
-        if (offset >= hits.length) {
-            offset = 0;
-        }
-
-        // determine limit
-        int limit = entryCount;
-        if (offset + limit > hits.length) {
-            limit = hits.length - offset;
-        }
-
         try {
-            Set<String> categories = new TreeSet<>();
-            TreeSet<String> categorySet = new TreeSet<>();
             Weblogger roller = WebloggerFactory.getWeblogger();
             WeblogEntryManager weblogMgr = roller.getWeblogEntryManager();
 
-            WeblogEntry entry;
-            Document doc;
-            String handle;
+            // a page past the last hit falls back to the first page; check
+            // before loading any entries
+            if ((long) pageNum * entryCount >= hits.length) {
+                pageNum = 0;
+            }
+
+            // The index can lag behind the entries, so page through the hits
+            // whose entry is published now (and not a future post). Only
+            // those count toward the offset and limit. Stop once the requested
+            // page is filled; if the hits run out first, an out-of-range page
+            // falls back to the first page below.
+            List<VisibleHit> visible = new ArrayList<>();
             Timestamp now = new Timestamp(new Date().getTime());
-            for (int i = offset; i < offset + limit; i++) {
-                doc = search.getSearcher().doc(hits[i].doc);
-                handle = 
doc.getField(FieldConstants.WEBSITE_HANDLE).stringValue();
-                entry = 
weblogMgr.getWeblogEntry(doc.getField(FieldConstants.ID).stringValue());
-
-                if (!(websiteSpecificSearch && handle.equals(weblogHandle))
-                    && doc.getField(FieldConstants.CATEGORY) != null) {
-                    
categorySet.add(doc.getField(FieldConstants.CATEGORY).stringValue());
+            long wanted = (long) (pageNum + 1) * entryCount;
+            for (ScoreDoc hit : hits) {
+                if (visible.size() >= wanted) {
+                    break;
                 }
-
+                Document doc = search.getSearcher().doc(hit.doc);
                 // maybe null if search result returned inactive user
-                // or entry's user is not the requested user.
-                // but don't return future posts
-                if (entry != null && entry.getPubTime().before(now)) {
-                    results.add(WeblogEntryWrapper.wrap(entry, urlStrategy));
+                // or entry's user is not the requested user
+                WeblogEntry entry = 
weblogMgr.getWeblogEntry(doc.getField(FieldConstants.ID).stringValue());
+                if (entry != null && entry.isPublished()
+                        && entry.getPubTime() != null && 
entry.getPubTime().before(now)) {
+                    visible.add(new VisibleHit(entry, doc));
                 }
             }
 
+            // determine offset
+            int offset = pageNum * entryCount;
+            if (offset >= visible.size()) {
+                offset = 0;
+            }
+
+            // determine limit
+            int limit = entryCount;
+            if (offset + limit > visible.size()) {
+                limit = visible.size() - offset;
+            }
+
+            Set<String> categories = new TreeSet<>();
+            TreeSet<String> categorySet = new TreeSet<>();
+            for (VisibleHit hit : visible.subList(offset, offset + limit)) {
+                if (!(websiteSpecificSearch && hit.handle.equals(weblogHandle))
+                    && hit.category != null) {
+                    categorySet.add(hit.category);
+                }
+                results.add(WeblogEntryWrapper.wrap(hit.entry, urlStrategy));
+            }
+
             if (!categorySet.isEmpty()) {
                 categories = categorySet;
             }
@@ -505,4 +519,18 @@ public class LuceneIndexManager implements IndexManager {
             throw new WebloggerException(e);
         }
     }
+
+    /** A hit whose entry is shown, with the index fields the page needs. */
+    private static final class VisibleHit {
+        private final WeblogEntry entry;
+        private final String handle;
+        private final String category;
+
+        VisibleHit(WeblogEntry entry, Document doc) {
+            this.entry = entry;
+            this.handle = 
doc.getField(FieldConstants.WEBSITE_HANDLE).stringValue();
+            this.category = doc.getField(FieldConstants.CATEGORY) == null
+                    ? null : 
doc.getField(FieldConstants.CATEGORY).stringValue();
+        }
+    }
 }
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryEdit.java
 
b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryEdit.java
index 3d2372cdb..1d2f4e493 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryEdit.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryEdit.java
@@ -289,11 +289,7 @@ public final class EntryEdit extends UIAction {
                 entrySaved = true;
 
                 // notify search of the new entry
-                if (weblogEntry.isPublished()) {
-                    indexMgr.addEntryReIndexOperation(entry);
-                } else if ("entryEdit".equals(actionName)) {
-                    indexMgr.removeEntryIndexOperation(entry);
-                }
+                indexMgr.updateEntryIndex(weblogEntry);
 
                 // notify caches
                 CacheManager.invalidate(weblogEntry);
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryRemove.java
 
b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryRemove.java
index 0db70756e..25bb69edc 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryRemove.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/EntryRemove.java
@@ -78,20 +78,11 @@ public class EntryRemove extends UIAction {
                 WeblogEntry entry = getRemoveEntry();
                 IndexManager manager = 
WebloggerFactory.getWeblogger().getIndexManager();
 
-                try {
-                    // remove the entry from the search index
-                    // TODO: can we do this in a better way?
-                    WeblogEntry.PubStatus originalStatus = entry.getStatus();
-                    entry.setStatus(WeblogEntry.PubStatus.DRAFT);
-                    manager.addEntryReIndexOperation(entry);
-                    entry.setStatus(originalStatus);
-                } catch (WebloggerException ex) {
-                    log.warn("Trouble triggering entry indexing", ex);
-                }
-
                 // remove from search index
-                if (entry.isPublished()) {
+                try {
                     manager.removeEntryIndexOperation(entry);
+                } catch (WebloggerException ex) {
+                    log.warn("Trouble removing entry from search index", ex);
                 }
 
                 // flush caches
diff --git 
a/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/EntryCollection.java
 
b/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/EntryCollection.java
index e637ffc7d..a7d12bd44 100644
--- 
a/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/EntryCollection.java
+++ 
b/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/EntryCollection.java
@@ -48,7 +48,6 @@ import org.apache.roller.weblogger.WebloggerException;
 import org.apache.roller.weblogger.business.WeblogEntryManager;
 import org.apache.roller.weblogger.business.Weblogger;
 import org.apache.roller.weblogger.business.WebloggerFactory;
-import org.apache.roller.weblogger.business.search.IndexManager;
 import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
 import org.apache.roller.weblogger.pojos.User;
 import org.apache.roller.weblogger.pojos.Weblog;
@@ -265,9 +264,7 @@ public class EntryCollection {
                     roller.flush();
                     
                     CacheManager.invalidate(rollerEntry.getWebsite());
-                    if (rollerEntry.isPublished()) {
-                        
roller.getIndexManager().addEntryReIndexOperation(rollerEntry);
-                    }
+                    roller.getIndexManager().updateEntryIndex(rollerEntry);
                     log.debug("Exiting");
                     return;
                 }
@@ -291,7 +288,7 @@ public class EntryCollection {
             if (RollerAtomHandler.canEdit(user, rollerEntry)) {
                 WeblogEntryManager mgr = roller.getWeblogEntryManager();
                 CacheManager.invalidate(rollerEntry.getWebsite());
-                reindexEntry(rollerEntry);
+                
roller.getIndexManager().removeEntryIndexOperation(rollerEntry);
                 mgr.removeWeblogEntry(rollerEntry);
                 log.debug("Deleted entry:" + rollerEntry.getAnchor());
                 roller.flush();
@@ -460,15 +457,4 @@ public class EntryCollection {
         rollerEntry.setTagsAsString(tags);        
     }
 
-    private void reindexEntry(WeblogEntry entry) throws WebloggerException {
-        IndexManager manager = roller.getIndexManager();
-        
-        // TODO: figure out what's up here and at WeblogEntryFormAction line 
696
-        //manager.removeEntryIndexOperation(entry);
-        
-        // if published, index the entry
-        if (entry.isPublished()) {
-            manager.addEntryReIndexOperation(entry);
-        }
-    }
 }
\ No newline at end of file
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/business/search/IndexManagerTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/business/search/IndexManagerTest.java
index 2c98d3be5..c274fcf74 100644
--- 
a/app/src/test/java/org/apache/roller/weblogger/business/search/IndexManagerTest.java
+++ 
b/app/src/test/java/org/apache/roller/weblogger/business/search/IndexManagerTest.java
@@ -107,6 +107,28 @@ public class IndexManagerTest {
                 testWeblog.getHandle(), null, testWeblog.getLocale(), 0, 
RESULTS_PER_PAGE,
                 WebloggerFactory.getWeblogger().getUrlStrategy()));
 
+            // an entry returned to draft leaves search results even before
+            // its index document is removed, and does not use up a page slot
+            WeblogEntryManager entryMgr = 
WebloggerFactory.getWeblogger().getWeblogEntryManager();
+            WeblogEntry action = 
TestUtils.getManagedWeblogEntry(entries.get(1));
+            action.setStatus(WeblogEntry.PubStatus.DRAFT);
+            entryMgr.saveWeblogEntry(action);
+            TestUtils.endSession(true);
+            result = indexManager.search("Enterprise",
+                testWeblog.getHandle(), null, testWeblog.getLocale(), 0, 1,
+                WebloggerFactory.getWeblogger().getUrlStrategy());
+            assertEquals(1, result.getResults().size());
+            assertEquals("The Tholian Web", 
result.getResults().get(0).getTitle());
+
+            WeblogEntry tholian = 
TestUtils.getManagedWeblogEntry(entries.get(0));
+            tholian.setStatus(WeblogEntry.PubStatus.DRAFT);
+            entryMgr.saveWeblogEntry(tholian);
+            TestUtils.endSession(true);
+            result = indexManager.search("Tholian",
+                testWeblog.getHandle(), null, testWeblog.getLocale(), 0, 
RESULTS_PER_PAGE,
+                WebloggerFactory.getWeblogger().getUrlStrategy());
+            assertEquals(0, result.getResults().size());
+
         } finally {
             for (WeblogEntry entry : entries) {
                 
indexManager.removeEntryIndexOperation(TestUtils.getManagedWeblogEntry(entry));
@@ -126,6 +148,7 @@ public class IndexManagerTest {
         List<WeblogEntry> entries = 
Instancio.ofList(WeblogEntry.class).size(10).create();
 
         entries.get(0).setTitle("The Tholian Web");
+        entries.get(0).setStatus(WeblogEntry.PubStatus.PUBLISHED);
         entries.get(0).setPubTime(new Timestamp(System.currentTimeMillis()));
         entries.get(0).setText(
             "When the Enterprise attempts to ascertain the fate of the  "
@@ -135,6 +158,7 @@ public class IndexManagerTest {
         Thread.sleep(500);
 
         entries.get(1).setTitle("A Piece of the Action");
+        entries.get(1).setStatus(WeblogEntry.PubStatus.PUBLISHED);
         entries.get(1).setPubTime(new Timestamp(System.currentTimeMillis()));
         entries.get(1).setText(
             "The crew of the Enterprise attempts to make contact with "
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/business/search/UpdateEntryIndexTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/business/search/UpdateEntryIndexTest.java
new file mode 100644
index 000000000..29599cd1b
--- /dev/null
+++ 
b/app/src/test/java/org/apache/roller/weblogger/business/search/UpdateEntryIndexTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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
+ *
+ *     https://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.
+ */
+package org.apache.roller.weblogger.business.search;
+
+import org.apache.roller.weblogger.pojos.WeblogEntry;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doCallRealMethod;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * A saved entry is re-indexed if it is published and removed otherwise.
+ */
+class UpdateEntryIndexTest {
+
+    @Test
+    void publishedEntryIsReIndexed() throws Exception {
+        IndexManager manager = manager();
+        WeblogEntry entry = entry(true);
+        manager.updateEntryIndex(entry);
+        verify(manager).addEntryReIndexOperation(entry);
+        verify(manager, never()).removeEntryIndexOperation(any());
+    }
+
+    @Test
+    void unpublishedEntryIsRemoved() throws Exception {
+        IndexManager manager = manager();
+        WeblogEntry entry = entry(false);
+        manager.updateEntryIndex(entry);
+        verify(manager).removeEntryIndexOperation(entry);
+        verify(manager, never()).addEntryReIndexOperation(any());
+    }
+
+    private static IndexManager manager() throws Exception {
+        IndexManager manager = mock(IndexManager.class);
+        doCallRealMethod().when(manager).updateEntryIndex(any());
+        return manager;
+    }
+
+    private static WeblogEntry entry(boolean published) {
+        WeblogEntry entry = mock(WeblogEntry.class);
+        when(entry.isPublished()).thenReturn(published);
+        return entry;
+    }
+}
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/business/search/lucene/SearchHitPagingTest.java
 
b/app/src/test/java/org/apache/roller/weblogger/business/search/lucene/SearchHitPagingTest.java
new file mode 100644
index 000000000..1961cd1d4
--- /dev/null
+++ 
b/app/src/test/java/org/apache/roller/weblogger/business/search/lucene/SearchHitPagingTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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.
+ */
+package org.apache.roller.weblogger.business.search.lucene;
+
+import java.sql.Timestamp;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.StringField;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.roller.weblogger.business.URLStrategy;
+import org.apache.roller.weblogger.business.WeblogEntryManager;
+import org.apache.roller.weblogger.business.Weblogger;
+import org.apache.roller.weblogger.business.WebloggerFactory;
+import org.apache.roller.weblogger.business.search.SearchResultList;
+import org.apache.roller.weblogger.pojos.WeblogEntry;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Search pages over published entries and loads only as many hits as the
+ * requested page needs.
+ */
+class SearchHitPagingTest {
+
+    private static final int HITS = 20;
+
+    @Test
+    void firstPageLoadsOnlyItsEntries() throws Exception {
+        WeblogEntryManager entries = entries(-1);
+        SearchResultList result = convert(entries, 0, 2);
+        assertEquals(2, result.getResults().size());
+        verify(entries, times(2)).getWeblogEntry(anyString());
+    }
+
+    @Test
+    void draftHitDoesNotUseAPageSlot() throws Exception {
+        WeblogEntryManager entries = entries(0);
+        SearchResultList result = convert(entries, 0, 2);
+        assertEquals(2, result.getResults().size());
+        assertEquals("id1", result.getResults().get(0).getId());
+        verify(entries, times(3)).getWeblogEntry(anyString());
+    }
+
+    @Test
+    void pageBeyondTheHitsFallsBackToTheFirstPage() throws Exception {
+        WeblogEntryManager entries = entries(-1);
+        SearchResultList result = convert(entries, 50, 2);
+        assertEquals(0, result.getOffset());
+        assertEquals("id0", result.getResults().get(0).getId());
+        verify(entries, times(2)).getWeblogEntry(anyString());
+    }
+
+    /** Entries id0..id19, published in the past; the one at draftIndex is a 
draft. */
+    private static WeblogEntryManager entries(int draftIndex) throws Exception 
{
+        WeblogEntryManager entries = mock(WeblogEntryManager.class);
+        Timestamp past = new Timestamp(System.currentTimeMillis() - 60_000);
+        for (int i = 0; i < HITS; i++) {
+            WeblogEntry entry = mock(WeblogEntry.class);
+            when(entry.getId()).thenReturn("id" + i);
+            when(entry.isPublished()).thenReturn(i != draftIndex);
+            when(entry.getPubTime()).thenReturn(past);
+            when(entries.getWeblogEntry("id" + i)).thenReturn(entry);
+        }
+        return entries;
+    }
+
+    private static SearchResultList convert(WeblogEntryManager entries, int 
page, int size)
+            throws Exception {
+        IndexSearcher searcher = mock(IndexSearcher.class);
+        ScoreDoc[] hits = new ScoreDoc[HITS];
+        for (int i = 0; i < HITS; i++) {
+            hits[i] = new ScoreDoc(i, 1f);
+            Document doc = new Document();
+            doc.add(new StringField(FieldConstants.ID, "id" + i, 
Field.Store.YES));
+            doc.add(new StringField(FieldConstants.WEBSITE_HANDLE, "team", 
Field.Store.YES));
+            when(searcher.doc(i)).thenReturn(doc);
+        }
+        SearchOperation search = mock(SearchOperation.class);
+        when(search.getSearcher()).thenReturn(searcher);
+        Weblogger weblogger = mock(Weblogger.class);
+        when(weblogger.getWeblogEntryManager()).thenReturn(entries);
+        try (MockedStatic<WebloggerFactory> factory = 
mockStatic(WebloggerFactory.class)) {
+            factory.when(WebloggerFactory::getWeblogger).thenReturn(weblogger);
+            return LuceneIndexManager.convertHitsToEntryList(
+                    hits, search, page, size, "team", true, 
mock(URLStrategy.class));
+        }
+    }
+}

Reply via email to