📄 jahiasearchbaseservice.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// JahiaSearchService// NK 25.01.2002 Implementation based on Lucene engine.////package org.jahia.services.search;import java.io.*; // File access methodsimport java.util.*; // Vectorimport org.apache.regexp.*; // Regular Expression// luceneimport org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.Searcher;import org.apache.lucene.search.Hits;import org.apache.lucene.store.Directory;import org.apache.lucene.store.RAMDirectory;import org.jahia.utils.*; // JahiaConsoleimport org.jahia.services.*; // JahiaServiceimport org.jahia.services.fields.*; // JahiaFieldServiceimport org.jahia.data.fields.*; // JahiaFieldsimport org.jahia.data.containers.*;import org.jahia.services.pages.JahiaPage;import org.jahia.data.search.*; // JahiaSearchResultimport org.jahia.gui.*; // GuiBeanimport org.jahia.params.*; // ParamBeanimport org.jahia.services.sites.*;import org.jahia.services.pages.*; // load_jahia_page_valuesimport org.jahia.services.usermanager.*; // JahiaUserimport org.jahia.registries.*; // JahiaRegistriesimport org.jahia.params.*; // ParamBeanimport org.jahia.data.*; // JahiaData pour ConnectionTypesimport org.jahia.exceptions.*;import org.jahia.settings.*;/** * Search Service based on Lucene engine. * * This service is a little unique, since the indexing is done by a background * thread that executes the indexing orders. This is done because indexing is * a slow task and there is no reason that the user should wait on it. * * @author Khue Nguyen <a href="mailto:khue@jahia.org">khue@jahia.org</a> * @version 1.0 */public class JahiaSearchBaseService extends JahiaSearchService implements Runnable { private static final String CLASS_NAME = JahiaSearchBaseService.class.getName(); /** The unique instance of this service **/ private static JahiaSearchBaseService theObject; /** Constants **/ private static final String searchIndexesDir = "search_indexes"; /** The indexes root path **/ private String searchIndexesDiskPath = ""; /** The Lucene analyzer **/ private Analyzer analyzer; /** search handlers **/ private Properties searchHandlers ; // list of all removed/added fields to add/remove from the index in background private Vector indexOrders; // les ordres de add/remove /** * Constructor * Client should always call getInstance() method */ protected JahiaSearchBaseService(){ JahiaConsole.println( CLASS_NAME, "***** Starting the Jahia Search Service *****" ); analyzer = new StandardAnalyzer(); searchHandlers = new Properties(); searchHandlers.setProperty(JahiaSearcher.PAGE_SEARCHER,"doPageSearch"); searchHandlers.setProperty(JahiaSearcher.CONTAINER_SEARCHER,"doContainerSearch"); indexOrders = new Vector(); } //-------------------------------------------------------------------------- /** * Returns the unique instance of this service. * */ public static synchronized JahiaSearchBaseService getInstance() { if (theObject == null) { theObject = new JahiaSearchBaseService(); new Thread(theObject).start(); // start background thread } return theObject; } //-------------------------------------------------------------------------- /** * Initialization * * @param JahiaprivateSetings */ public void init( JahiaPrivateSettings jSettings ) throws JahiaInitializationException { searchIndexesDiskPath = jSettings.jahiaVarDiskPath + File.separator + searchIndexesDir; File f = new File(searchIndexesDiskPath); // Create lucene search index repository if not exists. if ( !f.isDirectory() ) { f.mkdir(); } // now let's remove any stale lock files if there were some. removeStaleLockFiles(searchIndexesDiskPath); JahiaConsole.println(CLASS_NAME+".init", "Initialized"); } //-------------------------------------------------------------------------- /** * Perform a search for a given JahiaSearcher object. * * @param ParamBean jParams, to check read access. * @return JahiaSearchResult, containing a vector of matching objects. */ public JahiaSearchResult search ( JahiaSearcher jSearcher, ParamBean jParams ) throws JahiaException { JahiaConsole.println( CLASS_NAME+".search", "Started" ); JahiaSearchResult result = null; IndexReader reader = null; try { reader = getIndexReader(jParams.getSiteID()); String searchMethod = searchHandlers.getProperty(jSearcher.getName()); Class theClass = Class.forName("org.jahia.services.search.SearchEngine"); // define the types of the parameters Class theParams[] = {Class.forName("org.jahia.services.search.JahiaSearcher"), Class.forName("org.jahia.params.ParamBean")}; java.lang.reflect.Method theMethod = theClass.getMethod (searchMethod, theParams); // the parameter values Object args[] = {jSearcher,jParams}; result = (JahiaSearchResult)theMethod.invoke(new SearchEngine(this.analyzer,reader), args); } catch ( Throwable t ) { t.printStackTrace(); JahiaConsole.println( CLASS_NAME+".search", "Exception : " + t.getMessage() ); } finally { closeIndexReader(reader); } return result; } //-------------------------------------------------------------------------- /** * Return a vector of matching pages. * Perform a search for a given query ( valid lucene query ). * The search is limited to the site returned by the param bean. * * @Deprecated Should use search method with PageSearcher instead. * @param String queryString, a valid lucene query string. * @param ParamBean jParams, to check read access. * @return JahiaSearchResult, containing a vector of matching page. */ public JahiaSearchResult doSearch ( String queryString, ParamBean jParams ) throws JahiaException { PageSearcher pageSearcher = new PageSearcher(); JahiaSearchResult result = (JahiaSearchResult)pageSearcher.search( queryString, jParams ); return result; } //-------------------------------------------------------------------------- /** * Add a field to search engine. * With updated field, you should remove it first from search engine before * adding it again. * * @param JahiaField aField, the field to index. */ public synchronized void addFieldToSearchEngine ( JahiaField aField ){ if ( aField == null ) return; indexOrders.add(new AddedField (aField)); notifyAll(); } //-------------------------------------------------------------------------- /** * Remove a field from search engine. * * @param JahiaField aField, the field to remove. */ public synchronized void removeFieldFromSearchEngine ( JahiaField aField ){ if ( aField == null ) return; indexOrders.add(new RemovedField (aField)); notifyAll(); } //-------------------------------------------------------------------------- /** * Re-index a full site. * Should be called under particular situations ( time consuming ). * * @param int siteID * @return boolean false on error. */ public boolean indexSite (int siteID){ // start the chrono... long startTime = JahiaChrono.getInstance().start(); RAMDirectory ramDir = new RAMDirectory(); IndexWriter writer = null; boolean result = false; try { // Open RAM index writer writer = new IndexWriter(ramDir, this.analyzer, true); ServicesRegistry sReg = ServicesRegistry.getInstance(); // Index the fields JahiaField aField; Vector listFieldID = sReg.getJahiaFieldService().getAllFieldIDs(siteID); int size = listFieldID.size(); for ( int i=0 ; i<size ; i++ ) { try { aField = sReg.getJahiaFieldService() .loadField( ((Integer)listFieldID.get(i)).intValue(), LoadFlags.TEXTS ); indexField (aField,writer); } catch (JahiaException je) { // Don't want an exception to pop up if, // for instance, some template-orphaned page is stuck in the database... JahiaConsole.println( CLASS_NAME+".indexSite", "An Exception occurred: " + je.getMessage() ); } } // Close RAM index writer. writer.close(); // Open FS index writer. writer = getIndexWriter( siteID, this.analyzer, true ); if ( writer == null ) return false; // Move indexes from RAM to FS. Directory[] dirs = {ramDir}; writer.addIndexes(dirs); result = true; } catch ( Throwable t ){ JahiaConsole.println( CLASS_NAME+".indexSite", "Exception : " + t.getMessage() ); } finally { closeIndexWriter(writer); ramDir = null; } JahiaConsole.finalPrintln( "JahiaSearchBaseService", "indexing time [" +JahiaChrono.getInstance().read(startTime)+"ms]"); return result; } //-------------------------------------------------------------------------- /** * Perform an index optimization for a given site. * * @param int siteID * @return boolean false on error. */ public boolean optimizeIndex (int siteID){ IndexWriter writer = null; boolean result = false; try { writer = getIndexWriter( siteID, this.analyzer, false ); if ( writer == null ) return false; writer.optimize(); result = true; } catch ( Throwable t ){ JahiaConsole.println( CLASS_NAME+".optimize", "Exception : " + t.getMessage() ); } finally { closeIndexWriter(writer); } return result; } //-------------------------------------------------------------------------- /** * Return the full path to the directory containing the index for a given site. * * @param int siteID * @return String the site's index path, null if not exist. */ public String getSiteIndex (int siteID) throws JahiaException { String path = null; IndexReader reader = null; try { reader = getIndexReader(siteID); if ( reader != null ){ ServicesRegistry sReg = ServicesRegistry.getInstance(); JahiaSite site = null; site = sReg.getJahiaSitesService().getSite(siteID); path = composeSiteIndexDir(site); } } catch ( Throwable t ){ JahiaConsole.println( CLASS_NAME+".getSiteIndex", "Exception : " + t.getMessage() ); } finally { closeIndexReader(reader); } return path; } //-------------------------------------------------------------------------- /** * Return the full path to search indexes root directory. * * @return String the site's index path, null if not exist. */ public String getSearchIndexRootDir () throws JahiaException { return searchIndexesDiskPath; } //-------------------------------------------------------------------------- /** * Background task that handles the remove/add fields to search engine from * the queue */ public void run() { while (true) { while ( indexOrders.size() > 0 ) { Object nextObject = null; synchronized (indexOrders) { if (indexOrders.size()!=0) { nextObject = indexOrders.elementAt(0); indexOrders.remove(0); } } // okay now we have the next added/removed field, we process it! if (nextObject!=null) { if (nextObject instanceof AddedField) backgroundAddFieldToSearchEngine ((AddedField)nextObject); if (nextObject instanceof RemovedField) backgroundRemoveFieldFromSearchEngine ((RemovedField)nextObject); } } JahiaConsole.println(CLASS_NAME + ".run", "Search engine background indexing queue empty, waiting for orders..."); synchronized (this) { try { wait(); // wait for next notify } catch (InterruptedException ie) {} } JahiaConsole.println(CLASS_NAME + ".run", "Search engine indexing in background, do not shutdown server now !..."); } } //************************************************************************** // // Private section // //************************************************************************** //-------------------------------------------------------------------------- /** * Indexes a field with a given IndexWriter * Don't forget to close the index writer to flush change to the index file! * * @param JahiaField aField, the field to index. * @param IndexWriter writer, the index writer to use. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -