Index: src/com/ecyrd/jspwiki/WikiEngine.java =================================================================== --- src/com/ecyrd/jspwiki/WikiEngine.java (revision 743271) +++ src/com/ecyrd/jspwiki/WikiEngine.java (working copy) @@ -37,6 +37,7 @@ import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; +import com.ecyrd.jspwiki.SearchMatcher.SearchResultImpl; import com.ecyrd.jspwiki.attachment.Attachment; import com.ecyrd.jspwiki.attachment.AttachmentManager; import com.ecyrd.jspwiki.auth.AuthenticationManager; @@ -1837,10 +1838,38 @@ public Collection findPages( String query ) throws ProviderException, IOException { - Collection results = m_searchManager.findPages( query ); + return findPages(query, -1, -1); + } + /** + * Parses an incoming search request, then + * does a search. + *

+ * The query is dependent on the actual chosen search provider - each one of them has + * a language of its own. + *

+ * This function + * + * @param query The query string + * @param startSearchFrom Start of displayed search results + * @param searchItemAmount How much search results should be displayed + * + * @return A Collection of SearchResult objects. + * @throws ProviderException If the searching failed + * @throws IOException If the searching failed + */ + + + public Collection> findPages( String query , int startSearchFrom ,int SearchItemAmount ) + throws ProviderException, IOException + { + StopWatch sw = new StopWatch(); + sw.start(); + Collection> results = m_searchManager.findPages( query , startSearchFrom , SearchItemAmount); + sw.stop(); + log.info("The search for \"" + query + "\" lasted : " + sw.getTime()); return results; - } + } /** * Finds the corresponding WikiPage object based on the page name. It always finds Index: src/com/ecyrd/jspwiki/search/BasicSearchProvider.java =================================================================== --- src/com/ecyrd/jspwiki/search/BasicSearchProvider.java (revision 743271) +++ src/com/ecyrd/jspwiki/search/BasicSearchProvider.java (working copy) @@ -21,11 +21,7 @@ package com.ecyrd.jspwiki.search; import java.io.IOException; -import java.util.Collection; -import java.util.Iterator; -import java.util.Properties; -import java.util.StringTokenizer; -import java.util.TreeSet; +import java.util.*; import org.apache.log4j.Logger; @@ -36,6 +32,7 @@ import com.ecyrd.jspwiki.SearchResultComparator; import com.ecyrd.jspwiki.WikiEngine; import com.ecyrd.jspwiki.WikiPage; +import com.ecyrd.jspwiki.SearchMatcher.SearchResultImpl; import com.ecyrd.jspwiki.attachment.Attachment; import com.ecyrd.jspwiki.providers.ProviderException; import com.ecyrd.jspwiki.providers.WikiPageProvider; @@ -216,4 +213,14 @@ return "BasicSearchProvider"; } + /** + * BasicSearchProvider needs to parse all pages, thus Start- and Endpoint are useless + */ + public Collection> findPages( String query, int startShowFrom, int showAmount ) + throws ProviderException, + IOException + { + return findPages(parseQuery(query)); + } + } Index: src/com/ecyrd/jspwiki/search/LuceneSearchProvider.java =================================================================== --- src/com/ecyrd/jspwiki/search/LuceneSearchProvider.java (revision 743271) +++ src/com/ecyrd/jspwiki/search/LuceneSearchProvider.java (working copy) @@ -568,10 +568,29 @@ * @param flags A set of flags * @return A Collection of SearchResult instances * @throws ProviderException if there is a problem with the backend - */ + */ public Collection findPages( String query, int flags ) - throws ProviderException + throws ProviderException { + return findPages( query, flags, -1 , -1 ); + } + + public Collection findPages( String query , int Showfrom, int ShowAmount) + throws ProviderException + { + return findPages( query, FLAG_CONTEXTS, Showfrom , ShowAmount ); + } + /** + * Searches pages using a particular combination of flags. + * + * @param query The query to perform in Lucene query language + * @param flags A set of flags + * @return A Collection of SearchResult instances + * @throws ProviderException if there is a problem with the backend + */ + public Collection findPages( String query, int flags , int Showfrom, int ShowAmount) + throws ProviderException + { Searcher searcher = null; ArrayList list = null; Highlighter highlighter = null; @@ -603,6 +622,10 @@ Hits hits = searcher.search(luceneQuery); + boolean showAllFragments = (ShowAmount > -1) ? false : true; + int fragmentsFrom = showAllFragments ? 0 : Showfrom; + int fragmentsTo = showAllFragments ? hits.length() : Showfrom + ShowAmount; + list = new ArrayList(hits.length()); for ( int curr = 0; curr < hits.length(); curr++ ) { @@ -620,18 +643,21 @@ int score = (int)(hits.score(curr) * 100); - // Get highlighted search contexts String text = doc.get(LUCENE_PAGE_CONTENTS); + String[] fragments = new String[0]; - if( text != null && highlighter != null ) + + //Check for values + if (curr >= fragmentsFrom && curr < fragmentsTo) { - TokenStream tokenStream = getLuceneAnalyzer() - .tokenStream(LUCENE_PAGE_CONTENTS, new StringReader(text)); - fragments = highlighter.getBestFragments(tokenStream, - text, MAX_FRAGMENTS); - + if( text != null && highlighter != null) + { + TokenStream tokenStream = getLuceneAnalyzer().tokenStream(LUCENE_PAGE_CONTENTS, new StringReader(text)); + fragments = highlighter.getBestFragments(tokenStream, text, MAX_FRAGMENTS); + + } } SearchResult result = new SearchResultImpl( page, score, fragments ); Index: src/com/ecyrd/jspwiki/search/SearchManager.java =================================================================== --- src/com/ecyrd/jspwiki/search/SearchManager.java (revision 743271) +++ src/com/ecyrd/jspwiki/search/SearchManager.java (working copy) @@ -27,6 +27,7 @@ import org.apache.log4j.Logger; import com.ecyrd.jspwiki.*; +import com.ecyrd.jspwiki.SearchMatcher.SearchResultImpl; import com.ecyrd.jspwiki.event.WikiEvent; import com.ecyrd.jspwiki.event.WikiEventListener; import com.ecyrd.jspwiki.event.WikiEventUtils; @@ -298,11 +299,26 @@ public Collection findPages( String query ) throws ProviderException, IOException { + return findPages( query , -1, -1 ); + } + + /** + * Sends a search to the current search provider. The query is is whatever native format + * the query engine wants to use. + * + * @param query The query. Null is safe, and is interpreted as an empty query. + * @return A collection of WikiPages that matched. + * @throws ProviderException If the provider fails and a search cannot be completed. + * @throws IOException If something else goes wrong. + */ + public Collection> findPages( String query ,int startSearchFrom ,int SearchItemAmount ) + throws ProviderException, IOException + { if( query == null ) query = ""; - Collection c = m_searchProvider.findPages( query ); + Collection> c = m_searchProvider.findPages( query , startSearchFrom , SearchItemAmount); return c; - } + } /** * Removes the page from the search cache (if any). Index: src/com/ecyrd/jspwiki/search/SearchProvider.java =================================================================== --- src/com/ecyrd/jspwiki/search/SearchProvider.java (revision 743271) +++ src/com/ecyrd/jspwiki/search/SearchProvider.java (working copy) @@ -21,10 +21,12 @@ package com.ecyrd.jspwiki.search; import java.io.IOException; +import java.util.ArrayList; import java.util.Collection; import com.ecyrd.jspwiki.WikiPage; import com.ecyrd.jspwiki.WikiProvider; +import com.ecyrd.jspwiki.SearchMatcher.SearchResultImpl; import com.ecyrd.jspwiki.providers.ProviderException; /** @@ -58,4 +60,16 @@ * @throws IOException if for some reason the query could not be executed. */ public Collection findPages(String query) throws ProviderException, IOException; + + /** + * Search for pages matching a search query + * @param query query to search for + * @param startShowFrom Fragments that needed to be shown + * @param showAmount how many Fragments should be shown + * @return collection of pages that match query + * @throws ProviderException if the search provider failed. + * @throws IOException if for some reason the query could not be executed. + */ + public Collection> findPages(String query, int startShowFrom, int showAmount ) throws ProviderException, IOException; + } Index: src/webdocs/Search.jsp =================================================================== --- src/webdocs/Search.jsp (revision 743271) +++ src/webdocs/Search.jsp (working copy) @@ -4,6 +4,7 @@ <%@ page import="com.ecyrd.jspwiki.auth.permissions.*" %> <%@ page import="java.util.*" %> <%@ page errorPage="/Error.jsp" %> +<%@ page import="org.apache.commons.lang.time.StopWatch" %> <%@ taglib uri="/WEB-INF/jspwiki.tld" prefix="wiki" %> <%! @@ -21,6 +22,9 @@ Collection list = null; String query = request.getParameter( "query"); String go = request.getParameter("go"); + + int start = (request.getParameter( "start") != null) ? Integer.parseInt(request.getParameter("start")) : 0; + int maxitems = (request.getParameter( "maxitems") != null) ? Integer.parseInt(request.getParameter("maxitems")) : 20; if( query != null ) { @@ -28,8 +32,7 @@ try { - list = wiki.findPages( query ); - + list = wiki.findPages( query , start, maxitems ); // // Filter down to only those that we actually have a permission to view // Index: src/webdocs/templates/default/AJAXSearch.jsp =================================================================== --- src/webdocs/templates/default/AJAXSearch.jsp (revision 743271) +++ src/webdocs/templates/default/AJAXSearch.jsp (working copy) @@ -23,62 +23,72 @@ WikiEngine wiki; %> <% - /* ********************* actual start ********************* */ +int startitem = 0; // first item to show +int maxitems = 20; // number of items to show in result + +String parm_start = request.getParameter( "start"); +if( parm_start != null ) startitem = Integer.parseInt( parm_start ) ; + + +/* ********************* actual start ********************* */ /* FIXME: too much hackin on this level -- should better happen in toplevel jsp's */ /* Create wiki context and check for authorization */ WikiContext wikiContext = wiki.createContext( request, WikiContext.FIND ); if(!wikiContext.hasAccess( response )) return; - String query = request.getParameter( "query"); + + ArrayList filteredList = new ArrayList(); + //Read result from Request to prevent doble search + filteredList = (ArrayList) pageContext.getAttribute("searchresults", PageContext.REQUEST_SCOPE); - if( (query != null) && ( !query.trim().equals("") ) ) + if( startitem == -1 ) maxitems = filteredList.size(); //show all + + //Query only if there isn“t already a filtered list (coming from search.jsp) + if (filteredList == null) { - try - { - Collection list = wiki.findPages( query ); - - // Filter down to only those that we actually have a permission to view - AuthorizationManager mgr = wiki.getAuthorizationManager(); - - ArrayList items = new ArrayList(); + String query = request.getParameter( "query"); - for( Iterator i = list.iterator(); i.hasNext(); ) - { - SearchResult r = (SearchResult)i.next(); - - WikiPage p = r.getPage(); - - PagePermission pp = new PagePermission( p, PagePermission.VIEW_ACTION ); - - try - { - if( mgr.checkPermission( wikiContext.getWikiSession(), pp ) ) - { - items.add( r ); - } - } - catch( Exception e ) { log.error( "Searching for page "+p, e ); } - } - - pageContext.setAttribute( "searchresults", items, PageContext.REQUEST_SCOPE ); - } - catch( Exception e ) - { - wikiContext.getWikiSession().addMessage( e.getMessage() ); - } + if( (query != null) && ( !query.trim().equals("") ) ) + { + try + { + Collection list = wiki.findPages( query ); + + // Filter down to only those that we actually have a permission to view + AuthorizationManager mgr = wiki.getAuthorizationManager(); + + ArrayList items = new ArrayList(); + + for( Iterator i = list.iterator(); i.hasNext(); ) + { + SearchResult r = (SearchResult)i.next(); + + WikiPage p = r.getPage(); + + PagePermission pp = new PagePermission( p, PagePermission.VIEW_ACTION ); + + try + { + if( mgr.checkPermission( wikiContext.getWikiSession(), pp ) ) + { + items.add( r ); + } + } + catch( Exception e ) { log.error( "Searching for page "+p, e ); } + } + + filteredList = items; + + pageContext.setAttribute( "searchresults", filteredList, PageContext.REQUEST_SCOPE ); + } + catch( Exception e ) + { + wikiContext.getWikiSession().addMessage( e.getMessage() ); + } + } } %> -<% - int startitem = 0; // first item to show - int maxitems = 20; // number of items to show in result - String parm_start = request.getParameter( "start"); - if( parm_start != null ) startitem = Integer.parseInt( parm_start ) ; - - Collection list = (Collection)pageContext.getAttribute( "searchresults", PageContext.REQUEST_SCOPE ); - if( startitem == -1 ) maxitems = list.size(); //show all -%> -

@@ -96,7 +106,7 @@ target="_blank">Wikipedia

-