package lucene.test.analysis; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.StopAnalyzer; import org.apache.lucene.analysis.Token; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter.MaxFieldLength; import org.apache.lucene.store.LockObtainFailedException; public class StopAnalyzerTest { private static String IndexPath="D://MajorSoft//lucene//temp//IndexPath"; static String source="Apache Lucene is a high-performance, " + "full-featured text search engine library written entirely in Java. " + "It is a technology suitable for nearly any application that requires full-text search, " + "especially cross-platform,it works very well,it is very useful"; public static final String[] self_stop_words={ "a", "an", "and", "are", "as", "at", "be", "but", "by", "for", "if", "in", "into", "is", "it", "no", "not", "of", "on", "or", "such", "that", "the", "their", "then", "there", "these", "they", "this", "to", "was", "will", "with", "very" }; public static void main(String args[]) { Indexer(); } private static void Indexer() { //Analyzer analyzer=new StopAnalyzer(); Analyzer analyzer=new StopAnalyzer(self_stop_words); try { IndexWriter writer=new IndexWriter(IndexPath,analyzer,true,MaxFieldLength.UNLIMITED); Document document=new Document(); Field field_content=new Field("content",source,Field.Store.YES,Field.Index.ANALYZED); document.add(field_content); ArrayList ItemList=new ArrayList(); TokenStream stream=analyzer.tokenStream("content", new StringReader(source)); while(true) { Token item=stream.next(); if(null==item)break; System.out.println("{"+item.termText()+"}"); } writer.optimize(); writer.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }