Author: tmortagne Date: 2008-01-25 18:56:26 +0100 (Fri, 25 Jan 2008) New Revision: 7089
Modified: xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/pom.xml xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexUpdater.java xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePlugin.java xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePluginApi.java xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/SearchResults.java Log: WIKI-2027: Lucene plugin filter some pages in the search result list Also upgraded to the last Lucene version (2.3). Modified: xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/pom.xml =================================================================== --- xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/pom.xml 2008-01-25 17:47:57 UTC (rev 7088) +++ xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/pom.xml 2008-01-25 17:56:26 UTC (rev 7089) @@ -400,7 +400,7 @@ <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> - <version>2.0.0</version> + <version>2.3.0</version> </dependency> <dependency> <groupId>pdfbox</groupId> Modified: xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexUpdater.java =================================================================== --- xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexUpdater.java 2008-01-25 17:47:57 UTC (rev 7088) +++ xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/IndexUpdater.java 2008-01-25 17:56:26 UTC (rev 7089) @@ -55,7 +55,7 @@ /** * Milliseconds of sleep between checks for changed documents */ - private int indexingInterval = 300000; + private int indexingInterval = 3000; private boolean exit = false; @@ -77,6 +77,8 @@ private XWiki xwiki; + private long activesIndexedDocs = 0; + static List fields = new ArrayList(); public boolean needInitialBuild = false; @@ -107,6 +109,7 @@ Map toIndex = new HashMap(); List toDelete = new ArrayList(); + activesIndexedDocs = 0; try { openSearcher(); @@ -141,6 +144,7 @@ } toIndex.remove(id); } + ++activesIndexedDocs; toIndex.put(id, data); } } catch (Exception e) { @@ -192,6 +196,7 @@ addToIndex(data, doc, context); ++nb; + --activesIndexedDocs; } catch (Exception e) { LOG.error("error indexing document " + id, e); } @@ -200,6 +205,8 @@ if (LOG.isInfoEnabled()) { LOG.info("indexed " + nb + " docs to lucene index"); } + + writer.flush(); } catch (Exception e) { LOG.error("error indexing documents", e); } finally { @@ -210,7 +217,7 @@ plugin.openSearchers(); } try { - Thread.sleep(this.indexingInterval); + Thread.sleep(indexingInterval); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -301,7 +308,7 @@ try { // fix for windows by Daniel Cortes: - FSDirectory f = FSDirectory.getDirectory(indexDir, false); + FSDirectory f = FSDirectory.getDirectory(indexDir); writer = new IndexWriter(f, analyzer, create); // writer = new IndexWriter (indexDir, analyzer, create); writer.setUseCompoundFile(true); @@ -352,8 +359,8 @@ Field fld = null; // collecting all the fields for using up in search - for (Enumeration e = luceneDoc.fields(); e.hasMoreElements();) { - fld = (Field) e.nextElement(); + for (Iterator it = luceneDoc.getFields().iterator(); it.hasNext();) { + fld = (Field) it.next(); if (!fields.contains(fld.name())) { fields.add(fld.name()); } @@ -533,8 +540,30 @@ } } + /** + * @return the number of documents in the queue. + */ public long getQueueSize() { return this.queue.getSize(); } + + /** + * @return the number of documents Lucene index writer. + */ + public long getLuceneDocCount() + { + if (writer != null) + return writer.docCount(); + + return -1; + } + + /** + * @return the number of documents in the second queue gave to Lucene. + */ + public long getActiveQueueSize() + { + return this.activesIndexedDocs; + } } Modified: xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePlugin.java =================================================================== --- xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePlugin.java 2008-01-25 17:47:57 UTC (rev 7088) +++ xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePlugin.java 2008-01-25 17:56:26 UTC (rev 7089) @@ -199,6 +199,8 @@ public SearchResults getSearchResults(String query, String sortField, String virtualWikiNames, String languages, XWikiContext context) throws Exception { + openSearchers(); + return search(query, sortField, virtualWikiNames, languages, this.searchers, context); } @@ -247,7 +249,7 @@ LOG.debug("query " + q + " returned " + hitcount + " hits"); } - return new SearchResults(hits, new com.xpn.xwiki.api.XWiki(context.getWiki(), context)); + return new SearchResults(hits, new com.xpn.xwiki.api.XWiki(context.getWiki(), context), context); } /** @@ -274,7 +276,7 @@ if (LOG.isDebugEnabled()) LOG.debug("query " + q + " returned " + hitcount + " hits"); - return new SearchResults(hits, new com.xpn.xwiki.api.XWiki(context.getWiki(), context)); + return new SearchResults(hits, new com.xpn.xwiki.api.XWiki(context.getWiki(), context), context); } /** @@ -499,4 +501,20 @@ { indexUpdater.addAttachmentsOfDocument(doc, context); } + + /** + * @return the number of documents Lucene index writer. + */ + public long getLuceneDocCount() + { + return indexUpdater.getLuceneDocCount(); + } + + /** + * @return the number of documents in the second queue gave to Lucene. + */ + public long getActiveQueueSize() + { + return indexUpdater.getActiveQueueSize(); + } } Modified: xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePluginApi.java =================================================================== --- xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePluginApi.java 2008-01-25 17:47:57 UTC (rev 7088) +++ xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/LucenePluginApi.java 2008-01-25 17:56:26 UTC (rev 7089) @@ -78,7 +78,7 @@ if (wiki.hasAdminRights()) { nbDocuments = getPlugin().rebuildIndex(context.getContext()); } - + return nbDocuments; } @@ -102,7 +102,7 @@ } catch (Exception e) { e.printStackTrace(); } // end of try-catch - + return null; } @@ -166,12 +166,12 @@ } catch (Exception e) { e.printStackTrace(); } - + return null; } /** - * @return the number of documents in the queue + * @return the number of documents in the queue. */ public long getQueueSize() { @@ -179,6 +179,22 @@ } /** + * @return the number of documents in the second queue gave to Lucene. + */ + public long getActiveQueueSize() + { + return plugin.getActiveQueueSize(); + } + + /** + * @return the number of documents Lucene index writer. + */ + public long getLuceneDocCount() + { + return plugin.getLuceneDocCount(); + } + + /** * @param plugin plugin instance we are the facade for. */ public void setPlugin(LucenePlugin plugin) @@ -212,7 +228,7 @@ } catch (Exception e) { e.printStackTrace(); } // end of try-catch - + return null; } @@ -258,7 +274,7 @@ } catch (Exception e) { e.printStackTrace(); } // end of try-catch - + return null; } @@ -280,7 +296,7 @@ } catch (Exception e) { e.printStackTrace(); } // end of try-catch - + return null; } @@ -342,7 +358,7 @@ } catch (Exception e) { e.printStackTrace(); } - + return null; } @@ -404,7 +420,7 @@ } catch (Exception e) { e.printStackTrace(); } - + return null; } Modified: xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/SearchResults.java =================================================================== --- xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/SearchResults.java 2008-01-25 17:47:57 UTC (rev 7088) +++ xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/plugin/lucene/SearchResults.java 2008-01-25 17:56:26 UTC (rev 7089) @@ -19,6 +19,8 @@ */ package com.xpn.xwiki.plugin.lucene; +import com.xpn.xwiki.XWikiContext; +import com.xpn.xwiki.api.Api; import com.xpn.xwiki.api.XWiki; import org.apache.lucene.search.Hits; import org.apache.commons.logging.Log; @@ -28,13 +30,15 @@ import java.util.List; /** - * Container for the results of a search. <p> This class handles paging through search results and - * enforces the xwiki rights management by only returning search results the user executing the - * search is allowed to view. </p> - * + * Container for the results of a search. + * <p> + * This class handles paging through search results and enforces the xwiki rights management by only + * returning search results the user executing the search is allowed to view. + * </p> + * * @version $Id: $ */ -public class SearchResults +public class SearchResults extends Api { private final XWiki xwiki; @@ -48,8 +52,10 @@ * @param hits Lucene search results * @param xwiki xwiki instance for access rights checking */ - public SearchResults(Hits hits, XWiki xwiki) + public SearchResults(Hits hits, XWiki xwiki, XWikiContext context) { + super(context); + this.hits = hits; this.xwiki = xwiki; } @@ -59,23 +65,32 @@ if (relevantResults == null) { relevantResults = new ArrayList(); final int hitcount = hits.length(); - for (int i = 0; i < hitcount; i++) { - SearchResult result = null; - try { - result = new SearchResult(hits.doc(i), hits.score(i), xwiki); - String pageName = null; - if (result.isWikiContent()) { - pageName = result.getWeb() + "." + result.getName(); + + String database = this.context.getDatabase(); + try { + for (int i = 0; i < hitcount; i++) { + SearchResult result = null; + try { + result = new SearchResult(hits.doc(i), hits.score(i), xwiki); + + this.context.setDatabase(result.getWiki()); + + String pageName = null; + if (result.isWikiContent()) { + pageName = + result.getWiki() + ":" + result.getWeb() + "." + result.getName(); + } + if (result != null && result.isWikiContent() + && xwiki.checkAccess(pageName, "view") && xwiki.exists(pageName)) { + relevantResults.add(result); + } + } catch (Exception e) { + LOG.error("error getting search result", e); + e.printStackTrace(); } - if (result != null && result.isWikiContent() && - xwiki.checkAccess(pageName, "view") && xwiki.exists(pageName)) - { - relevantResults.add(result); - } - } catch (Exception e) { - LOG.error("error getting search result", e); - e.printStackTrace(); } + } finally { + this.context.setDatabase(database); } } return relevantResults; @@ -147,11 +162,11 @@ /** * Returns a list of search results. According to beginIndex and endIndex, only a subset of the * results is returned. To get the first ten results, one would use beginIndex=1 and items=10. - * + * * @param beginIndex 1-based index of first result to return. * @param items number of items to return - * @return List of SearchResult instances starting at <code>beginIndex</code> and containing up - * to <code>items</code> elements. + * @return List of SearchResult instances starting at <code>beginIndex</code> and containing + * up to <code>items</code> elements. */ public List getResults(int beginIndex, int items) { @@ -162,33 +177,43 @@ if (relResults == null) { relResults = new ArrayList(); final int hitcount = hits.length(); - for (int i = 0; i < hitcount; i++) { - SearchResult result = null; - try { - result = new SearchResult(hits.doc(i), hits.score(i), xwiki); - String pageName = null; - if (result.isWikiContent()) { - pageName = result.getWeb() + "." + result.getName(); - } - if (result != null && result.isWikiContent() && xwiki.exists(pageName) && - xwiki.checkAccess(pageName, "view")) - { - if (resultcount>=listStartIndex) { - relResults.add(result); + + String database = this.context.getDatabase(); + try { + for (int i = 0; i < hitcount; i++) { + SearchResult result = null; + try { + result = new SearchResult(hits.doc(i), hits.score(i), xwiki); + + this.context.setDatabase(result.getWiki()); + + String pageName = null; + if (result.isWikiContent()) { + pageName = + result.getWiki() + ":" + result.getWeb() + "." + result.getName(); } - resultcount++; - if (resultcount==listEndIndex) - return relResults; + if (result != null && result.isWikiContent() && xwiki.exists(pageName) + && xwiki.checkAccess(pageName, "view")) { + if (resultcount >= listStartIndex) { + relResults.add(result); + } + resultcount++; + if (resultcount == listEndIndex) + return relResults; + } + } catch (Exception e) { + LOG.error("error getting search result", e); + e.printStackTrace(); } - } catch (Exception e) { - LOG.error("error getting search result", e); - e.printStackTrace(); } + } finally { + this.context.setDatabase(database); } + return relResults; } else { return getRelevantResults().subList(listStartIndex, - listEndIndex < resultcount ? listEndIndex : resultcount); + listEndIndex < resultcount ? listEndIndex : resultcount); } } _______________________________________________ notifications mailing list notifications@xwiki.org http://lists.xwiki.org/mailman/listinfo/notifications