1 5 package org.exoplatform.services.indexing.test; 6 7 import org.apache.lucene.document.Document; 8 import org.exoplatform.container.PortalContainer; 9 import org.exoplatform.services.indexing.FileIndexerPlugin; 10 import org.exoplatform.services.indexing.IndexingService; 11 import org.exoplatform.services.indexing.Searcher; 12 import org.exoplatform.services.indexing.SingleFieldSearchInput; 13 import org.exoplatform.services.indexing.impl.FileIndexerPluginImpl; 14 import org.exoplatform.test.BasicTestCase; 15 16 23 public class TestIndexingService extends BasicTestCase { 24 static private IndexingService service_ ; 25 public TestIndexingService(String name) { 26 super(name); 27 } 28 29 public void setUp() throws Exception { 30 if(service_ == null) { 31 PortalContainer pcontainer = PortalContainer.getInstance() ; 32 service_ = (IndexingService) pcontainer.getComponentInstanceOfType(IndexingService.class) ; 33 } 34 } 35 36 public void testIndexingService() throws Exception { 37 FileIndexerPluginImpl plugin = 38 (FileIndexerPluginImpl)service_.getIndexerPlugin(FileIndexerPlugin.IDENTIFIER) ; 39 String [] acceptExt = { "java" , "xml"} ; 40 plugin.indexDirectory("src","guest", acceptExt , true) ; 41 Thread.sleep(3500) ; 43 Searcher searcher = plugin.getSearcher() ; 44 SingleFieldSearchInput documentSearchInput = 45 new SingleFieldSearchInput(IndexingService.DOCUMENT_FIELD) ; 46 documentSearchInput.setTerm("Tuan") ; 47 java.util.List hits = searcher.search(documentSearchInput).getAll() ; 49 Document foundDoc = null ; 50 for(int i = 0; i < hits.size(); i ++) { 51 foundDoc = (Document )hits.get(i) ; 52 System.out.println("F O U N D : " + foundDoc.getField(IndexingService.IDENTIFIER_FIELD).stringValue()) ; 53 } 54 if(foundDoc != null ) { 55 String objectId = foundDoc.getField(IndexingService.IDENTIFIER_FIELD).stringValue() ; 56 System.out.println(plugin.getObjectAsText(null, objectId)) ; 57 } 58 59 plugin.removeIndex() ; 60 Thread.sleep(3500) ; 61 searcher = plugin.getSearcher() ; 62 hits = searcher.search(documentSearchInput).getAll() ; 63 assertEquals("exect no result is found", 0 , hits.size()) ; 64 service_.optimizeDatabase() ; 65 } 66 } | Popular Tags |