- 案例:
Article.java |
package cn.toto.lucene.quickstart; publicclassArticle privateintid; private private /** * */ publicint returnid; } /** * */ publicvoid this.id } /** * */ public returntitle; } /** * */ publicvoid this.title } /** * */ public returncontent; } /** * */ publicvoid this.content } } |
工具类Configuration.java |
package cn.toto.lucene.util; import java.io.File; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; /** * @brief Configuration.java配置对象,提供Lucene需要 索引目录,分词器 * @attention * @author toto * @date 2014-12-7 * @note begin modify by涂作权 */ public class Configuration { private static Directory directory; private static Analyzer analyzer; private static Version version; static { try { //设置磁盘目录,表示的是本地index目录 directory = FSDirectory.open(new File("index")); } catch (Exception e) { e.printStackTrace(); } //表示LUCENE版本 version = Version.LUCENE_36; //表示使用版本 analyzer = new StandardAnalyzer(version); } //提供目录 public static Directory getDirectory() { return directory; } //提供分词器 public static Analyzer getAnalyzer() { return analyzer; } //获取版本 public static Version getVersion() { return version; } } |
工具类LuceneUtils.java |
package cn.toto.lucene.util; import java.io.IOException; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.search.IndexSearcher; // lucene工具类 public class LuceneUtils { private static IndexWriter indexWriter; static { // try { // IndexWriterConfig indexWriterConfig = new IndexWriterConfig( Configuration.getVersion(), Configuration.getAnalyzer()); indexWriter = new IndexWriter(Configuration.getDirectory(), indexWriterConfig); // Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { indexWriter.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } catch (IOException e) { e.printStackTrace(); } } // public static IndexWriter getIndexWriter() { return indexWriter; } // public static IndexSearcher getIndexSearcher() throws Exception { return new IndexSearcher(IndexReader.open(Configuration.getDirectory())); } } |
LuceneTest.java |
package cn.toto.lucene.api; import java.io.File; importjava.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.Field.Store; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.Version; import org.junit.Test; import cn.toto.lucene.quickstart.Article; import cn.toto.lucene.util.LuceneUtils; // API详细分析 publicclass @Test //使用LuceneUtils解决 @SuppressWarnings("unused") publicvoid IndexWriter indexWriter2 = LuceneUtils.getIndexWriter(); IndexWriter indexWriter1 = LuceneUtils.getIndexWriter(); } @Test @SuppressWarnings("all") //使用两个IndexWrtier报错,锁使用问题 publicvoid LockObtainFailedException, //索引目录位置 Directory directory = FSDirectory.open(new //分词器 Analyzer analyzer = //写入索引 IndexWriterConfig indexWriterConfig = Version.LUCENE_36, IndexWriter indexWriter = IndexWriterConfig indexWriterConfig2 = Version.LUCENE_36, IndexWriter indexWriter2 = indexWriterConfig2); } 上面的运行结果如下: @Test //测试Store和 /* * Store.YES * Index.NOT_ANALYZED * Index.NOT_ANALYZED_NO_NORMS */ publicvoid //需要建立索引目标数据 Article article = article.setId(100); article.setTitle("学习全文检索"); article.setContent("lucene是搜索引擎开发技术,lucene并不是一个现成的产品,由Apache提供"); //将索引数据转换 Document document = document.add(new Field.Index.NOT_ANALYZED));//对于id通常不分词 document.add(new Field.Index.ANALYZED_NO_NORMS)); document.add(new Field.Index.ANALYZED)); //建立索引库 //索引目录位置 Directory directory = FSDirectory.open(new //分词器 Analyzer analyzer = //写入索引 IndexWriterConfig indexWriterConfig = IndexWriter indexWriter = //将document数据写入索引库 indexWriter.addDocument(document); indexWriter.close(); } 上面的单元测试的结果 @Test //查询索引库 publicvoid //建立Query对象 String queryStrng = Analyzer analyzer = QueryParser queryParser = analyzer); Query query = queryParser.parse(queryStrng); //根据Query查找 Directory directory = FSDirectory.open(new IndexSearcher indexSearcher = IndexReader.open(directory)); //执行查询获得满足结果前多少条记录 TopDocs topDocs = indexSearcher.search(query, 100);//查询满足结果前100条数据 System.out.println("满足结果记录条数:" //获得每个结果 ScoreDoc[] scoreDocs = topDocs.scoreDocs; for //打印得分 System.out.println("得分:" //获得Document下标 int Document document = indexSearcher.doc(docID); System.out.println("id:" System.out.println("title:" System.out.println("content:" } indexSearcher.close(); } 上面的运行单元测试之后的结果如下: @SuppressWarnings("unused") @Test //测试路径写法 publicvoid //磁盘路径 FSDirectory.open(new FSDirectory.open(new //类路径 FSDirectory.open(new //内存路径 Directory directory = } } |