package player.kent.chen.temp.lucene.stemming; import java.io.IOException; public class MyLuceneStemmingDemo { private final static String allText = "The companies organized an better activity than the individuals."; public static void main(String[] args) throws Exception { Analyzer snowball = new SnowballAnalyzer(Version.LUCENE_30, "English"); doSearch(snowball, "company"); //搜不到 doSearch(snowball, "compani"); //搜得到 doSearch(snowball, "organize"); //搜不到 doSearch(snowball, "organiz");//搜不到 doSearch(snowball, "organ");//搜得到 doSearch(snowball, "good");//搜不到 doSearch(snowball, "act");//搜不到 doSearch(snowball, "individual");//搜不到 } private static void doSearch(Analyzer analyzer, String keyword) throws CorruptIndexException, LockObtainFailedException, IOException { Directory indexDir = new RAMDirectory(); IndexWriter indexWriter = new IndexWriter(indexDir, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); Document doc = new Document(); doc.add(new Field("content", allText, Field.Store.YES, Field.Index.ANALYZED)); indexWriter.addDocument(doc); IndexSearcher indexSearcher = new IndexSearcher(indexWriter.getReader()); TopDocs searchResult = indexSearcher .search(new TermQuery(new Term("content", keyword)), 10); System.out.println(MessageFormat.format("\"{0}\" found \"{2}\" hits for keyword \"{1}\"", analyzer.getClass().getSimpleName(), keyword, searchResult.totalHits)); indexSearcher.close(); indexWriter.close(); } }