Author: antonio Date: Tue Nov 16 18:19:12 2004 New Revision: 76097 Modified: cocoon/trunk/src/blocks/lucene/java/org/apache/cocoon/components/search/LuceneCocoonPager.java cocoon/trunk/src/blocks/lucene/java/org/apache/cocoon/generation/SearchGenerator.java cocoon/trunk/status.xml Log: Fix bug #23118: Lucene SearchGenerator incorrectly counts previous-index
Modified: cocoon/trunk/src/blocks/lucene/java/org/apache/cocoon/components/search/LuceneCocoonPager.java ============================================================================== --- cocoon/trunk/src/blocks/lucene/java/org/apache/cocoon/components/search/LuceneCocoonPager.java (original) +++ cocoon/trunk/src/blocks/lucene/java/org/apache/cocoon/components/search/LuceneCocoonPager.java Tue Nov 16 18:19:12 2004 @@ -1,12 +1,12 @@ /* * Copyright 1999-2004 The Apache Software Foundation. - * + * * Licensed 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. @@ -27,7 +27,7 @@ * This class should help you to manage paging of hits. * * @author <a href="mailto:[EMAIL PROTECTED]">Bernhard Huber</a> - * @version CVS $Id: LuceneCocoonPager.java,v 1.4 2004/03/05 13:01:59 bdelacretaz Exp $ + * @version CVS $Id$ */ public class LuceneCocoonPager implements ListIterator { @@ -176,7 +176,7 @@ * @return Description of the Returned Value */ public boolean hasNext() { - return hitsIndex < hits.length(); + return hitsIndex + countOfHitsPerPage < hits.length(); } /** @@ -197,19 +197,16 @@ public Object next() { ArrayList hitsPerPageList = new ArrayList(); int endIndex = Math.min(hits.length(), hitsIndex + countOfHitsPerPage); - if (hitsIndex < endIndex) { - while (hitsIndex < endIndex) { - try { - HitWrapper hit = new HitWrapper(hits.score(hitsIndex), - hits.doc(hitsIndex)); - hitsPerPageList.add(hit); - } catch (IOException ioe) { - throw new NoSuchElementException("no more hits: " + ioe.getMessage()); - } - hitsIndex++; + //do not increment the actual hitsindex + int hits_copy = hitsIndex; + while (hits_copy < endIndex) { + try { + HitWrapper hit = new HitWrapper(hits.score(hits_copy), hits.doc(hits_copy)); + hitsPerPageList.add(hit); + } catch (IOException ioe) { + throw new NoSuchElementException("no more hits: " + ioe.getMessage()); } - } else { - throw new NoSuchElementException(); + hits_copy++; } return hitsPerPageList; } @@ -221,7 +218,7 @@ * @return Description of the Returned Value */ public int nextIndex() { - return Math.min(hitsIndex, hits.length()); + return Math.min(hitsIndex + countOfHitsPerPage, hits.length() - 1); } /** @@ -232,7 +229,7 @@ public Object previous() { ArrayList hitsPerPageList = new ArrayList(); - int startIndex = Math.max(0, hitsIndex - 2 * countOfHitsPerPage); + int startIndex = Math.max(0, hitsIndex - countOfHitsPerPage); int endIndex = Math.min(hits.length() - 1, hitsIndex - countOfHitsPerPage); if (startIndex < endIndex) { @@ -260,7 +257,7 @@ * @return Description of the Returned Value */ public int previousIndex() { - return Math.max(0, hitsIndex - 2 * countOfHitsPerPage); + return Math.max(0, hitsIndex - countOfHitsPerPage); } /** @@ -275,7 +272,7 @@ * A helper class encapsulating found document, and its score * * @author <a href="mailto:[EMAIL PROTECTED]">Bernhard Huber</a> - * @version CVS $Id: LuceneCocoonPager.java,v 1.4 2004/03/05 13:01:59 bdelacretaz Exp $ + * @version CVS $Id$ */ public static class HitWrapper { float score; Modified: cocoon/trunk/src/blocks/lucene/java/org/apache/cocoon/generation/SearchGenerator.java ============================================================================== --- cocoon/trunk/src/blocks/lucene/java/org/apache/cocoon/generation/SearchGenerator.java (original) +++ cocoon/trunk/src/blocks/lucene/java/org/apache/cocoon/generation/SearchGenerator.java Tue Nov 16 18:19:12 2004 @@ -1,12 +1,12 @@ /* * Copyright 1999-2004 The Apache Software Foundation. - * + * * Licensed 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. @@ -15,7 +15,6 @@ */ package org.apache.cocoon.generation; -import org.apache.avalon.framework.activity.Initializable; import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.framework.context.Context; import org.apache.avalon.framework.context.ContextException; @@ -26,10 +25,10 @@ import org.apache.cocoon.Constants; import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.components.search.LuceneCocoonHelper; +import org.apache.cocoon.components.search.LuceneCocoonPager; import org.apache.cocoon.components.search.LuceneCocoonSearcher; import org.apache.cocoon.components.search.LuceneXMLIndexer; -import org.apache.cocoon.components.search.LuceneCocoonPager; -import org.apache.cocoon.components.search.LuceneCocoonHelper; import org.apache.cocoon.environment.ObjectModelHelper; import org.apache.cocoon.environment.Request; import org.apache.cocoon.environment.SourceResolver; @@ -45,10 +44,10 @@ import java.io.File; import java.io.IOException; +import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Enumeration; /** * Generates an XML representation of a search result. @@ -97,7 +96,7 @@ * @version CVS $Id$ */ public class SearchGenerator extends ServiceableGenerator - implements Contextualizable, Initializable, Disposable + implements Contextualizable, Disposable { /** @@ -160,7 +159,7 @@ * Attribute <code>name</code> of <code>hit</code> element. */ protected final static String NAME_ATTRIBUTE = "name"; - + /** * Child element of generated xml content, ie <code>hits</code>. * This element describes all hits. @@ -416,7 +415,6 @@ */ public void service(ServiceManager manager) throws ServiceException { super.service(manager); -// lcs = (LuceneCocoonSearcher) this.manager.lookup(LuceneCocoonSearcher.ROLE); } /** @@ -428,13 +426,15 @@ throws ProcessingException, SAXException, IOException { super.setup(resolver, objectModel, src, par); + try { + lcs = (LuceneCocoonSearcher) this.manager.lookup(LuceneCocoonSearcher.ROLE); + } catch (ServiceException e) { + throw new ProcessingException("Unable to lookup " + LuceneCocoonSearcher.ROLE, e); + } + String param_name; Request request = ObjectModelHelper.getRequest(objectModel); - // get the analyzer -// Analyzer analyzer = LuceneCocoonHelper.getAnalyzer("org.apache.lucene.analysis.standard.StandardAnalyzer"); -// lcs.setAnalyzer(analyzer); - String index_file_name = par.getParameter(INDEX_PARAM, INDEX_PARAM_DEFAULT); if (request.getParameter(INDEX_PARAM) != null) { index_file_name = request.getParameter(INDEX_PARAM); @@ -447,9 +447,9 @@ } // try getting the queryString from the generator sitemap params - + queryString = par.getParameter(QUERY_PARAM, ""); - + // try getting the queryString from the request params if (queryString.equals("")) { param_name = par.getParameter(QUERY_STRING_PARAM, QUERY_STRING_PARAM_DEFAULT); @@ -504,12 +504,6 @@ workDir = (File) context.get(Constants.CONTEXT_WORK_DIR); } - public void initialize() throws IOException { - // get the directory where the index resides -// Directory directory = LuceneCocoonHelper.getDirectory(new File(workDir, "index"), false); -// lcs.setDirectory(directory); - } - /** * Generate xml content describing search results. * Entry point of the ComposerGenerator. @@ -613,7 +607,7 @@ * @throws SAXException when there is a problem creating the output SAX events. */ private void generateHits(LuceneCocoonPager pager) throws SAXException { - if (pager != null && pager.hasNext()) { + if (pager != null) { atts.clear(); atts.addAttribute("", TOTAL_COUNT_ATTRIBUTE, TOTAL_COUNT_ATTRIBUTE, CDATA, String.valueOf(pager.getCountOfHits())); @@ -726,9 +720,7 @@ if (queryString != null && queryString.length() != 0) { Hits hits = null; - // TODO (VG): Move parts into compose/initialize/recycle try { - lcs = (LuceneCocoonSearcher) this.manager.lookup(LuceneCocoonSearcher.ROLE); Analyzer analyzer = LuceneCocoonHelper.getAnalyzer("org.apache.lucene.analysis.standard.StandardAnalyzer"); lcs.setAnalyzer(analyzer); // get the directory where the index resides @@ -737,13 +729,6 @@ hits = lcs.search(queryString, LuceneXMLIndexer.BODY_FIELD); } catch (IOException ioe) { throw new ProcessingException("IOException in search", ioe); - } catch (ServiceException ce) { - throw new ProcessingException("ServiceException in search", ce); - } finally { - if (lcs != null) { - this.manager.release(lcs); - lcs = null; - } } // wrap the hits by an pager help object for accessing only a range of hits @@ -778,6 +763,9 @@ */ public void recycle() { super.recycle(); + if (lcs != null) { + this.manager.release(lcs); + } this.queryString = null; this.startIndex = null; this.pageLength = null; @@ -785,10 +773,6 @@ } public void dispose() { -// if (lcs != null) { -// this.manager.release(lcs); -// lcs = null; -// } super.dispose(); } } Modified: cocoon/trunk/status.xml ============================================================================== --- cocoon/trunk/status.xml (original) +++ cocoon/trunk/status.xml Tue Nov 16 18:19:12 2004 @@ -368,6 +368,9 @@ </action> </release> <release version="2.1.6" date="TBD"> + <action dev="AG" type="fix" fixes-bug="23118" due-to="Jorg Heymans" due-to-email="[EMAIL PROTECTED]"> + Lucene: SearchGenerator incorrectly counts previous-index. Reported by: Tomasz Nowak ([EMAIL PROTECTED]) + </action> <action dev="AG" type="update"> Updated iText to 1.1. </action>