📄 jahiasearchbaseservice.java
字号:
private void indexField ( JahiaField aField, IndexWriter writer ){ if ( writer == null || aField == null || aField.getValue()==null ) return; Document doc = new Document(); // Add comparable meta-data to index. doc.add( Field.Keyword( JahiaSearchConstant.FIELD_OBJTYPE, String.valueOf( JahiaSearchConstant.FIELD_FIELDOBJ ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_FIELDID, String.valueOf( aField.getID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_JAHIAID, String.valueOf( aField.getJahiaID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_PAGEID, String.valueOf( aField.getPageID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_CTNID, String.valueOf( aField.getctnid() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_FIELDTYPE, String.valueOf( aField.getType() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_RIGHT, String.valueOf( aField.getAclID() ) ) ); try { JahiaFieldDefinition fieldDef = aField.getDefinition(); if ( fieldDef != null ) { doc.add( Field.Keyword( JahiaSearchConstant.FIELD_FIELDNAME, fieldDef.getName() ) ); } // index the content but do not store it. doc.add( Field.UnStored( JahiaSearchConstant.FIELD_VALUE, JahiaTools.html2text(aField.getValue()) ) ); writer.addDocument(doc); } catch ( Throwable t ){ JahiaConsole.println( CLASS_NAME+".indexField", "Exception : " + t.getMessage() ); return; } } //-------------------------------------------------------------------------- /** * Indexes a container with a given IndexWriter * Don't forget to close the index writer to flush change to the index file! * * @param JahiaContainer container, the container to index. * @param IndexWriter writer, the index writer to use. */ private void indexContainer ( JahiaContainer container, IndexWriter writer ){ if ( writer == null || container == null ) return; Document doc = new Document(); // Add comparable meta-data to index. doc.add( Field.Keyword( JahiaSearchConstant.FIELD_OBJTYPE, String.valueOf( JahiaSearchConstant.FIELD_CTNOBJ ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_CTNID, String.valueOf( container.getID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_JAHIAID, String.valueOf( container.getJahiaID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_PAGEID, String.valueOf( container.getPageID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_CTNLISTID, String.valueOf( container.getListID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_RIGHT, String.valueOf( container.getAclID() ) ) ); // Add data for filtering JahiaField aField = null; JahiaFieldDefinition fieldDef = null; Hashtable hash; Enumeration keys; String key; String val; StringBuffer buff; try { Enumeration enum = container.getFields(); while ( enum.hasMoreElements() ) { aField = (JahiaField)enum.nextElement(); fieldDef = aField.getDefinition(); if ( fieldDef != null ) { JahiaConsole.println( CLASS_NAME+".indexContainer", "Indexing field : " + fieldDef.getName() ); hash = aField.getIndexableAttributes(); keys = hash.keys(); while ( keys.hasMoreElements() ) { key = (String)keys.nextElement(); val = (String)hash.get(key); buff = new StringBuffer(JahiaSearchConstant.FIELD_PREFIX); buff.append(fieldDef.getName()); buff.append("_"); buff.append(key); doc.add( Field.Keyword( buff.toString() , val ) ); } } } writer.addDocument(doc); } catch ( Throwable t ){ JahiaConsole.println( CLASS_NAME+".indexContainer", "Exception : " + t.getMessage() ); return; } } //************************************************************************** // // Private section // //************************************************************************** /** * This class represents a field that must be added to the search engine */ private class AddedField { int id; int jahiaID; int pageID; int ctnID; int type; int right; String fieldName = ""; String value; public AddedField (JahiaField theField) { this.id = theField.getID(); this.jahiaID = theField.getJahiaID(); this.pageID = theField.getPageID(); this.ctnID = theField.getctnid(); this.type = theField.getType(); this.right = theField.getAclID(); if (theField.getValue() != null) { this.value = new String(theField.getValue()); } else { this.value = ""; } try { JahiaFieldDefinition fieldDef = theField.getDefinition(); if ( fieldDef != null ) { this.fieldName = fieldDef.getName(); } } catch ( Throwable t ){ } } public int getID() { return id; } public int getJahiaID() { return jahiaID; } public int getPageID() { return pageID; } public int getctnid() { return ctnID; } public int getType() { return type; } public int getAclID() { return right; } public String getFieldName() { return fieldName; } public String getValue() { return value; } } /** * This class represents a field that must be removed from the search engine */ private class RemovedField { int id; int jahiaID; public RemovedField (JahiaField theField) { this.id = theField.getID(); this.jahiaID = theField.getJahiaID(); } public int getID() { return id; } public int getJahiaID() { return jahiaID; } } //-------------------------------------------------------------------------- /** * Add a field into the search engine index in background */ private void backgroundAddFieldToSearchEngine ( AddedField aField ){ if ( aField == null ) return; IndexWriter writer = null; try { // Try to get the site's index if any. writer = getIndexWriter( aField.getJahiaID(), this.analyzer, false ); // If the site's index doesn't exist. Create it. if ( writer == null ) writer = getIndexWriter( aField.getJahiaID(), this.analyzer, true ); // Index the field. indexField(aField,writer); // JahiaConsole.println( CLASS_NAME+".backgroundAddFieldToSearchEngine", // "Field added :" + aField.getID() ); } catch ( Throwable t ) { JahiaConsole.println( CLASS_NAME+".addFieldToSearchEngine", "Exception : " + t.getMessage() ); } finally { closeIndexWriter(writer); } } //-------------------------------------------------------------------------- /** * Remove a field from search engine in background */ private void backgroundRemoveFieldFromSearchEngine ( RemovedField aField ){ /* JahiaConsole.println( CLASS_NAME+".removeFieldFromSearchEngine", "Started" ); */ IndexReader reader = null; try { // Try to get the site's index if any. reader = getIndexReader( aField.getJahiaID() ); if ( reader == null ) return; // Create a term with the field id as unique identifier. Term term = new Term( JahiaSearchConstant.FIELD_FIELDID, String.valueOf(aField.getID()) ); // Remove all documents containing the term. int nbDeleted = reader.delete(term); // JahiaConsole.println( CLASS_NAME+".backgroundRemoveFieldFromSearchEngine", // "Field removed :" + aField.getID() + " , " // + String.valueOf(nbDeleted) + " Doc deleted "); } catch ( Throwable t ) { JahiaConsole.println( CLASS_NAME+".removeFieldFromSearchEngine", "Exception : " + t.getMessage() ); } finally { closeIndexReader(reader); } } //-------------------------------------------------------------------------- /** * 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. */ private void indexField ( AddedField aField, IndexWriter writer ){ if ( writer == null || aField == null || aField.getValue()==null ) return; Document doc = new Document(); // Add comparable meta-data to index. doc.add( Field.Keyword( JahiaSearchConstant.FIELD_OBJTYPE, String.valueOf( JahiaSearchConstant.FIELD_FIELDOBJ ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_FIELDID, String.valueOf( aField.getID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_JAHIAID, String.valueOf( aField.getJahiaID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_PAGEID, String.valueOf( aField.getPageID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_CTNID, String.valueOf( aField.getctnid() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_FIELDTYPE, String.valueOf( aField.getType() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_RIGHT, String.valueOf( aField.getAclID() ) ) ); doc.add( Field.Keyword( JahiaSearchConstant.FIELD_FIELDNAME, aField.getFieldName() ) ); try { // index the content but do not store it. //JahiaConsole.println( CLASS_NAME+".indexField", // "Field value : " + aField.getValue() ); //doc.add( Field.UnStored( JahiaSearchConstant.FIELD_VALUE, // JahiaTools.html2text(aField.getValue()) ) ); doc.add( Field.UnStored( JahiaSearchConstant.FIELD_VALUE, aField.getValue() ) ); writer.addDocument(doc); } catch ( Throwable t ){ JahiaConsole.println( CLASS_NAME+".indexField", "Exception : " + t.getMessage() ); return; } } //-------------------------------------------------------------------------- /** * Returns the IndexWriter for a given site. * Don't forget to close the returned index writer to flush change to the index file ! * * @param int siteID, the site id. * @param Analyzer the analyzer to use. * @param boolean if true, create a new index and replace existing one. * @return IndexWriter writer, the IndexWriter, null on error. */ private IndexWriter getIndexWriter( int siteID, Analyzer analyzer, boolean create ) throws IOException, JahiaException { ServicesRegistry sReg = ServicesRegistry.getInstance(); JahiaSite site = null; IndexWriter writer = null; site = sReg.getJahiaSitesService().getSite(siteID); if ( site != null ){ writer = new IndexWriter(composeSiteIndexDir(site),analyzer,create); site = null; } return writer; } //-------------------------------------------------------------------------- /** * Returns the IndexReader for a given site. * Don't forget to close the returned index reader to flush change to the index file ! * * @param int siteID, the site id. * @return IndexReader reader, the IndexReader, null if not found. */ private IndexReader getIndexReader( int siteID ) throws IOException, JahiaException { ServicesRegistry sReg = ServicesRegistry.getInstance(); JahiaSite site = null; IndexReader reader = null; site = sReg.getJahiaSitesService().getSite(siteID); if ( site != null ){ reader = IndexReader.open(composeSiteIndexDir(site)); site = null; } return reader; } //-------------------------------------------------------------------------- /** * Compose a index directory full path for a given site. * * @param JahiaSite site, the site. * @return String the path */ private String composeSiteIndexDir( JahiaSite site ) { if ( site == null ) return null; StringBuffer buff = new StringBuffer(searchIndexesDiskPath); buff.append(File.separator); buff.append(site.getSiteKey()); return buff.toString(); } //-------------------------------------------------------------------------- /** * Close a IndexWriter * * @param IndexWriter writer, the index writer */ private void closeIndexWriter( IndexWriter writer ) { if ( writer == null ) return; try { writer.close(); } catch ( Throwable t ) { JahiaConsole.println(CLASS_NAME+".closeIndexWriter", "Exception : " + t.getMessage()); } } //-------------------------------------------------------------------------- /** * Close a IndexReader * * @param IndexReader reader, the index reader */ private void closeIndexReader( IndexReader reader ) { if ( reader == null ) return; try { reader.close(); } catch ( Throwable t ) { JahiaConsole.println(CLASS_NAME+".closeIndexReader", "Exception : " + t.getMessage()); } } /** * Removes all *.lock files in the specified directory, going down * recursively. Make sure you call this ONLY on lucene managed directories. * * @param startingDirectory directory in which to remove the *.lock files */ private synchronized void removeStaleLockFiles( String startingDirectory ) { File f = new File(startingDirectory); if (!f.isDirectory()) { JahiaConsole.println("JahiaSearchBaseService.removeStaleLockFiles", "Called on a file name, exiting..."); return; } String[] dirContents = f.list(); for (int i=0; i < dirContents.length; i++) { String curEntry = startingDirectory + File.separator + dirContents[i]; File curFile = new File(curEntry); if (curFile.isDirectory()) { // let's do a recursive call for this directory. removeStaleLockFiles(curFile.toString()); } else if (curFile.toString().endsWith(".lock")) { JahiaConsole.println("JahiaSearchBaseService.removeStaleLockFiles", "Removing stale lock file : [" + curFile.toString() + "]"); curFile.delete(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -