📄 searchengine.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////////package org.jahia.services.search;import java.io.*; // File access methodsimport java.util.*; // Vectorimport org.apache.regexp.*; // Regular Expressionimport org.apache.lucene.analysis.Analyzer;import org.apache.lucene.index.IndexReader;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.search.Hits;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.Searcher;import org.jahia.gui.*; // GuiBeanimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.params.*; // ParamBeanimport org.jahia.exceptions.*;import org.jahia.data.search.*; // JahiaSearchResultimport org.jahia.registries.*; // JahiaRegistriesimport org.jahia.data.fields.*; // JahiaFieldsimport org.jahia.services.pages.JahiaPage;import org.jahia.services.usermanager.*; // JahiaUser/** * Search engine using Lucene. * * @author Khue Nguyen <a href="mailto:khue@jahia.org">khue@jahia.org</a> */class SearchEngine { private static final String CLASS_NAME = SearchEngine.class.getName(); private Analyzer analyzer; private IndexReader reader; /** * Constructor * * @param Analyzer, the analyzer. * @param IndexReader reader, the index reader to use. */ public SearchEngine ( Analyzer analyzer, IndexReader reader ) { this.analyzer = analyzer; this.reader = reader; } //-------------------------------------------------------------------------- /** * 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. * * @param JahiaSearcher jSearcher, the jahia searcher. * @param ParamBean jParams, the param bean. * @return JahiaSearchResult result, the search result as an JahiaSearchResult object. */ public JahiaSearchResult doPageSearch( JahiaSearcher jSearcher, ParamBean jParams ) throws JahiaException { JahiaConsole.println( CLASS_NAME+".doPageSearch", "Started" ); JahiaSearchResult result = new JahiaSearchResult(); if ( reader == null || jSearcher== null || jSearcher.getQuery()==null ) return result; try { // instanciate the searcher with the site index. Searcher searcher = new IndexSearcher(reader); // instanciate the query Query query = QueryParser.parse(jSearcher.getQuery().toLowerCase(), JahiaSearchConstant.FIELD_VALUE, analyzer); // get the search result. Hits hits = searcher.search(query); result = buildJahiaSearchResult(hits,jParams); } catch ( Throwable t ) { JahiaConsole.println( CLASS_NAME+".doPageSearch", "Exception : " + t.getMessage() ); } return result; } //-------------------------------------------------------------------------- /** * Return a vector of matching containers for a given container list. * Perform a search for a given query ( valid lucene query ). * The search is limited to the site returned by the param bean. * * @param JahiaSearcher jSearcher, the jahia searcher. * @param ParamBean jParams, the param bean. * @return JahiaSearchResult result, the search result as an JahiaSearchResult object. */ public JahiaSearchResult doContainerSearch( JahiaSearcher jSearcher, ParamBean jParams ) throws JahiaException { JahiaConsole.println( CLASS_NAME+".doContainerSearch", "Started" ); JahiaSearchResult result = new JahiaSearchResult(); if ( reader == null || jSearcher== null || jSearcher.getQuery()==null ) return result; StringBuffer buff = new StringBuffer(1024); try { ContainerSearcher cSearcher = (ContainerSearcher)jSearcher; // instanciate the searcher with the site index. Searcher searcher = new IndexSearcher(reader); ServicesRegistry sReg = ServicesRegistry.getInstance(); // restric result to containers of the ctnlist. // get container list containers. Vector ctnids = sReg.getJahiaContainersService().getctnidsInList( cSearcher.getCtnListID() ); if ( ctnids.size() == 0 ) return result; int nbCtn = ctnids.size(); buff.append("("); buff.append(cSearcher.getQuery().toLowerCase()); buff.append(") AND ( "); for ( int i=0 ; i<nbCtn ; i++ ) { buff.append(JahiaSearchConstant.FIELD_CTNID); buff.append(":"); buff.append(((Integer)ctnids.get(i)).toString()); if ( i<(nbCtn-1) ) { buff.append(" OR "); } else { buff.append(" ) "); } } JahiaConsole.println( CLASS_NAME+".doContainerSearch", "Query is : " + buff.toString() ); // instanciate the query Query query = QueryParser.parse(buff.toString(), JahiaSearchConstant.FIELD_VALUE, this.analyzer); // get the search result. Hits hits = searcher.search(query); if ( hits == null || hits.length()==0 ) return result; JahiaConsole.println( CLASS_NAME+".doContainerSearch", "lucene result : " + hits.length() ); result = buildContainerSearchResult(hits,jParams); JahiaConsole.println( CLASS_NAME+".doContainerSearch", "jahia result : " + result.getHitCount() ); } catch ( Throwable t ) { JahiaConsole.println( CLASS_NAME+".doContainerSearch", "Exception : " + t.getMessage() ); } finally { buff = null; } return result; } //-------------------------------------------------------------------------- /** * Build jahia search result ( matching container ids ) from hits returned by lucene. * The vector results of the returned JahiaSearchResult is a vector of ids, not of containers ! * No right check on container, only on field. * * TODO : have a common method for building JahiaSearchResults for pages and containers. * * @param Hits hits, the search result returned by lucene. * @param ParamBean param, the param bean used to control access. * @param int results the param bean. * @return JahiaSearchResult result, the result. */ private JahiaSearchResult buildContainerSearchResult( Hits hits, ParamBean jParams ) { JahiaSearchResult result = new JahiaSearchResult(); if ( hits == null || hits.length()==0 ) return result; ServicesRegistry sReg = ServicesRegistry.getInstance(); JahiaField aField = null; Object obj = null; int objID = 0; JahiaUser currentUser = jParams.getUser(); JahiaSearchHit info; int nbHits = hits.length(); String fieldIDVal = null; boolean found = false; for (int i=0; i<nbHits ; i++) { try { fieldIDVal = hits.doc(i).get(JahiaSearchConstant.FIELD_FIELDID); if ( fieldIDVal != null ){ aField = sReg.getJahiaFieldService() .loadField(Integer.parseInt(fieldIDVal), jParams); } } catch (Throwable t) { // } // security check over the field. if ( (aField != null) && aField.checkReadAccess(currentUser,jParams.getSiteID()) ) { found = false; int size = result.results().size(); for (int j=0; j<size; j++) // page already found { if (((JahiaSearchHit)result.results().elementAt(j)).id == aField.getctnid()) { ((JahiaSearchHit)result.results().elementAt(j)).wordcount += 1; found = true; break; } } objID = aField.getctnid(); if (!found) { info = new JahiaSearchHit(); info.wordcount = 1; info.id = objID; info.obj = new Integer(objID); // actually we do not load container String value = aField.getValue(); if (value!=null){ try { value = (new RE("<(.*?)>")).subst(value,""); } catch (RESyntaxException e) { JahiaConsole.println(CLASS_NAME+".buildContainerSearchResult",e.toString()); } catch (Throwable t) { JahiaConsole.println(CLASS_NAME+".buildContainerSearchResult",t.toString()); } info.teaser = GuiBean.glueTitle(value,JahiaSearchConstant.TEASER_LENGTH); } else { info.teaser = ""; } result.addHit(info); } } fieldIDVal = null; aField = null; obj = null; } // sort the result if ( result.results().size()>1 ){ // a dummy hit JahiaSearchHit dummyHit = new JahiaSearchHit(); dummyHit.wordcount = 0; Collections.sort(result.results(),dummyHit); } return result; } //-------------------------------------------------------------------------- /** * Build jahia search result ( matching pages ) from hits returned by lucene. * * TODO : have a common method for building JahiaSearchResults for pages and containers. * * @param Hits hits, the search result returned by lucene. * @param ParamBean param, the param bean used to control access. * @return JahiaSearchResult result, the result. */ private JahiaSearchResult buildJahiaSearchResult( Hits hits, ParamBean jParams ){ JahiaSearchResult result = new JahiaSearchResult(); if ( hits == null || hits.length()==0 ) return result; ServicesRegistry sReg = ServicesRegistry.getInstance(); JahiaField aField = null; JahiaPage aPage = null; JahiaUser currentUser = jParams.getUser(); JahiaSearchHit info; int nbHits = hits.length(); String fieldIDVal = null; boolean found = false; int pagecount = 0; for (int i=0; i<nbHits ; i++) { try { fieldIDVal = hits.doc(i).get(JahiaSearchConstant.FIELD_FIELDID); if ( fieldIDVal != null ){ aField = sReg.getJahiaFieldService() .loadField(Integer.parseInt(fieldIDVal), jParams); aPage = sReg.getJahiaPageService().lookupPage (aField.getPageID(), jParams); } } catch (Throwable t) { // } // security check over the field and the corresponding page if ( aPage != null && aPage.checkReadAccess(currentUser) && aField.checkReadAccess(currentUser,jParams.getSiteID()) ) { found = false; for (int j=0; j<result.getHitCount(); j++) // page already found { if (((JahiaSearchHit)result.results().elementAt(j)).id == aField.getPageID()) { ((JahiaSearchHit)result.results().elementAt(j)).wordcount += 1; found = true; break; } } if (!found) { info = new JahiaSearchHit(); info.wordcount = 1; info.id = aPage.getID(); info.obj = aPage; //Keep for backward compatibility --------------------------- info.page = aPage; info.pageID = aPage.getID(); //----------------------------------------------------------- String value = aField.getValue(); if (value!=null){ try { value = (new RE("<(.*?)>")).subst(value,""); } catch (RESyntaxException e) { JahiaConsole.println(CLASS_NAME+".buildJahiaSearchResult",e.toString()); } catch (Throwable t) { JahiaConsole.println(CLASS_NAME+".buildJahiaSearchResult",t.toString()); } info.teaser = GuiBean.glueTitle(value,JahiaSearchConstant.TEASER_LENGTH); } else { info.teaser = ""; } result.addHit(info); pagecount++; } } fieldIDVal = null; aField = null; aPage = null; } // sort the result if ( result.getHitCount()>1 ){ // a dummy hit JahiaSearchHit dummyHit = new JahiaSearchHit(); dummyHit.wordcount = 0; Collections.sort(result.results(),dummyHit); } //Keep for backward compatibility --------------------------- result.pagecount = pagecount; result.pages = result.results(); //----------------------------------------------------------- return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -