public Collection findPages( String query, int flags )
throws ProviderException
{
Searcher searcher = null;
ArrayList<SearchResult> list = null;
Highlighter highlighter = null;
try
{
String[] queryfields = { LUCENE_PAGE_CONTENTS, LUCENE_PAGE_NAME, LUCENE_AUTHOR, LUCENE_ATTACHMENTS };
QueryParser qp = new MultiFieldQueryParser( queryfields, getLuceneAnalyzer() );
//QueryParser qp = new QueryParser( LUCENE_PAGE_CONTENTS, getLuceneAnalyzer() );
Query luceneQuery = qp.parse( query );
if( (flags & FLAG_CONTEXTS) != 0 )
{
highlighter = new Highlighter(new SimpleHTMLFormatter("<span class=\"searchmatch\">", "</span>"),
new SimpleHTMLEncoder(),
new QueryScorer(luceneQuery));
}
try
{
searcher = new IndexSearcher(m_luceneDirectory);
}
catch( Exception ex )
{
log.info("Lucene not yet ready; indexing not started",ex);
return null;
}
Hits hits = searcher.search(luceneQuery);
list = new ArrayList<SearchResult>(hits.length());
for ( int curr = 0; curr < hits.length(); curr++ )
{
Document doc = hits.doc(curr);
String pageName = doc.get(LUCENE_ID);
WikiPage page = m_engine.getPage(pageName, WikiPageProvider.LATEST_VERSION);
if(page != null)
{
if(page instanceof Attachment)
{
// Currently attachments don't look nice on the search-results page
// When the search-results are cleaned up this can be enabled again.
}
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 )
{
TokenStream tokenStream = getLuceneAnalyzer()
.tokenStream(LUCENE_PAGE_CONTENTS, new StringReader(text));
fragments = highlighter.getBestFragments(tokenStream,
text, MAX_FRAGMENTS);
}
SearchResult result = new SearchResultImpl( page, score, fragments );