WikiSearchEngine.java
/*
* Copyright (c) 2002-2023, City of Paris
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice
* and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* License 1.0
*/
package fr.paris.lutece.plugins.wiki.search;
import fr.paris.lutece.portal.service.search.IndexationService;
import fr.paris.lutece.portal.service.search.LuceneSearchEngine;
import fr.paris.lutece.portal.service.search.SearchEngine;
import fr.paris.lutece.portal.service.search.SearchItem;
import fr.paris.lutece.portal.service.search.SearchResult;
import fr.paris.lutece.portal.service.util.AppLogService;
import fr.paris.lutece.portal.web.l10n.LocaleService;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
/**
* WikiSearchEngine
*/
public class WikiSearchEngine implements SearchEngine
{
/**
* Return search results
*
* @param strQuery
* The search query
* @param request
* The HTTP request
* @return Results as a collection of SearchResult
*/
@Override
public List<SearchResult> getSearchResults( String strQuery, HttpServletRequest request )
{
ArrayList<SearchItem> listResults = new ArrayList<>( );
IndexSearcher searcher;
try ( Directory directory = IndexationService.getDirectoryIndex( ) ; IndexReader reader = DirectoryReader.open( directory ) ; )
{
searcher = new IndexSearcher( reader );
BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder( );
// Contents
if ( ( strQuery != null ) && !strQuery.equals( "" ) )
{
QueryParser parser = new QueryParser( SearchItem.FIELD_CONTENTS, IndexationService.getAnalyser( ) );
strQuery = strQuery + " OR " + SearchItem.FIELD_TITLE + ":(" + strQuery + ")";
queryBuilder.add( parser.parse( strQuery ), BooleanClause.Occur.MUST );
}
// Language
String strLanguage = LocaleService.getContextUserLocale( request ).getLanguage( );
Query queryLanguage = new TermQuery( new Term( SearchItem.FIELD_METADATA, strLanguage ) );
queryBuilder.add( queryLanguage, BooleanClause.Occur.MUST );
// Type
Query queryType = new TermQuery( new Term( SearchItem.FIELD_TYPE, WikiIndexer.getDocumentType( ) ) );
queryBuilder.add( queryType, BooleanClause.Occur.MUST );
// Get results documents
TopDocs topDocs = searcher.search( queryBuilder.build( ), LuceneSearchEngine.MAX_RESPONSES );
ScoreDoc [ ] hits = topDocs.scoreDocs;
for ( int i = 0; i < hits.length; i++ )
{
int docId = hits [i].doc;
Document document = searcher.doc( docId );
SearchItem item = new SearchItem( document );
listResults.add( item );
}
}
catch( Exception e )
{
AppLogService.error( e.getMessage( ), e );
}
return convertList( listResults );
}
/**
* Convert a list of Lucene items into a list of generic search items
*
* @param listSource
* The list of Lucene items
* @return A list of generic search items
*/
private List<SearchResult> convertList( List<SearchItem> listSource )
{
List<SearchResult> listDest = new ArrayList<>( );
for ( SearchItem item : listSource )
{
SearchResult result = new SearchResult( );
result.setId( item.getId( ) );
try
{
result.setDate( DateTools.stringToDate( item.getDate( ) ) );
}
catch( ParseException e )
{
AppLogService.error( "Bad Date Format for indexed item \"" + item.getTitle( ) + "\" : " + e.getMessage( ) );
}
result.setUrl( item.getUrl( ) );
result.setTitle( item.getTitle( ) );
result.setSummary( item.getSummary( ) );
result.setType( item.getType( ) );
listDest.add( result );
}
return listDest;
}
}