[Logo] JForum - Powering Communities
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Top Downloads] Top Downloads   [Groups] Back to home page 
[Register] Register /  [Login] Login 


JForum 2.8.3 is out with various fixes and improvements. Read all about it here

Search Pagination is broken? RSS feed
Forum Index » Developer Forum
Author Message
Komal Gupta


Joined: 2020/8/7
Messages: 12
Offline
Steps followed:
1. Go to "Search" from the top menu
2. Type 'why' with default options selected
3. Click Search


It says "Search Results: 34 records were found"
The first page renders 20 records
The second page renders 11 records, which were the last 11 records on the first page.
The third page renders 2 records, that were the last 2 records on page 2



Similarly, search for 'suggestions'. It says - Search Results: 39 records were found
The first page renders 33 records
The second page renders 20 records, which were the last 20 records on the first page.
The third page renders 5 records, that were the last 5 records on page 2


udittmer


Joined: 2013/2/21
Messages: 426
Offline
The base problem seems to be that the first page shows all 35 results, when it should only be showing 15.

So yes, that looks like a bug. I'll put it on my list.

Ping & Net - my free Android app for TCP/IP network diagnostics
[Email] [WWW]
Komal Gupta


Joined: 2020/8/7
Messages: 12
Offline
Sure.

Attaching the patch that worked for me in the next post. Feel free to use it if it looks good to you! Thanks!
Komal Gupta


Joined: 2020/8/7
Messages: 12
Offline


Index: src/main/java/net/jforum/search/LuceneContentCollector.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/net/jforum/search/LuceneContentCollector.java (revision 972)
+++ src/main/java/net/jforum/search/LuceneContentCollector.java (date 1658247453000)
@@ -66,6 +66,10 @@

import org.apache.log4j.Logger;

+import net.jforum.util.preferences.ConfigKeys;
+import net.jforum.util.preferences.SystemGlobals;
+
+
/**
* @author Rafael Steil
*/
@@ -75,19 +79,20 @@

private LuceneSettings settings;

- public LuceneContentCollector (LuceneSettings settings)
- {
+ public LuceneContentCollector(LuceneSettings settings) {
this.settings = settings;
}

public List<Post> collect (SearchArgs args, ScoreDoc[] results, Query query) {
try {
- int[] postIds = new int[results.length];
+ int recordsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
+ int finalResultSize = Math.min(recordsPerPage, results.length);
+ int[] postIds = new int[finalResultSize];
//LOGGER.debug("collect: results="+results.length+", args.fetchCount="+args.fetchCount());

IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(this.settings.directory()));
for (int docIndex = args.startFrom(), i = 0;
- docIndex < results.length;
+ i < finalResultSize;
docIndex++, i++) {
ScoreDoc hit = results[docIndex];
Document doc = searcher.doc(hit.doc);

udittmer


Joined: 2013/2/21
Messages: 426
Offline
Thanks for the patch, I've just committed it (minus a bug that caused an exception if the number of results on the last page wasn't the same as the number of topics per page).

Ping & Net - my free Android app for TCP/IP network diagnostics
[Email] [WWW]
Komal Gupta


Joined: 2020/8/7
Messages: 12
Offline
Thanks for catching that. Added another fix for when search result limit size is less than the total hits. Please find patch in the next post.
Komal Gupta


Joined: 2020/8/7
Messages: 12
Offline

Index: src/main/java/net/jforum/search/LuceneContentCollector.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/net/jforum/search/LuceneContentCollector.java (revision 974)
+++ src/main/java/net/jforum/search/LuceneContentCollector.java (date 1658338387000)
@@ -64,33 +64,38 @@
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;

+import net.jforum.util.preferences.ConfigKeys;
+import net.jforum.util.preferences.SystemGlobals;
+
import org.apache.log4j.Logger;

/**
* @author Rafael Steil
*/
-public class LuceneContentCollector
-{
+public class LuceneContentCollector {
private static final Logger LOGGER = Logger.getLogger(LuceneContentCollector.class);

private LuceneSettings settings;

- public LuceneContentCollector (LuceneSettings settings)
- {
+ public LuceneContentCollector(LuceneSettings settings) {
this.settings = settings;
}

- public List<Post> collect (SearchArgs args, ScoreDoc[] results, Query query) {
+ public List<Post> collect(SearchArgs args, ScoreDoc[] results, Query query, int totalHits) {
try {
- int finalResultSize = Math.min(args.fetchCount(), results.length - args.startFrom());
+ int finalResultSize = Math.min(args.fetchCount(), totalHits - args.startFrom());
+// LOGGER.debug(String.format("collect: results=%d, args.fetchCount=%d, args.startFrom=%d, finalResultSize=%d", results.length, args.fetchCount(), args.startFrom(), finalResultSize));
int[] postIds = new int[finalResultSize];
- //LOGGER.debug(String.format("collect: results=%d, args.fetchCount=%d, args.startFrom=%d",
- // results.length, args.fetchCount(), args.startFrom()));

IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(this.settings.directory()));
- for (int docIndex = args.startFrom(), i = 0;
- i < finalResultSize;
- docIndex++, i++) {
+
+ int docIndex = args.startFrom();
+ while (docIndex > SystemGlobals.getIntValue(ConfigKeys.SEARCH_RESULT_LIMIT)) {
+ docIndex -= SystemGlobals.getIntValue(ConfigKeys.SEARCH_RESULT_LIMIT);
+ }
+ LOGGER.debug(String.format("docIndex=%d", docIndex));
+
+ for (int i = 0; i < finalResultSize; docIndex++, i++) {
ScoreDoc hit = results[docIndex];
Document doc = searcher.doc(hit.doc);
postIds[i] = Integer.parseInt(doc.get(SearchFields.Keyword.POST_ID));
Index: src/main/java/net/jforum/search/LuceneSearch.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/net/jforum/search/LuceneSearch.java (revision 974)
+++ src/main/java/net/jforum/search/LuceneSearch.java (date 1658338329000)
@@ -158,7 +158,7 @@
ScoreDoc[] docs = tfd.scoreDocs;
TotalHits th = tfd.totalHits;
if (th.value > 0) {
- result = new SearchResult<>(resultCollector.collect(args, docs, query), (int) th.value);
+ result = new SearchResult<>(resultCollector.collect(args, docs, query, (int) th.value), (int) th.value);
} else {
result = new SearchResult<>(new ArrayList<>(), 0);
}

 
Forum Index » Developer Forum
Go to:   
Mobile view
Powered by JForum 2.8.3 © 2024 JForum Team • Maintained by Andowson Chang and Ulf Dittmer