Lucene 6.5.0 入门Demo(2)
参考文档:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description
对于path路径不是很清楚,参考了:http://blog.csdn.net/zhangweiwtmdbf/article/details/7099988
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | package com.cf.first; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.*; import org.apache.lucene.index.*; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.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.junit.Test; import java.io.File; import java.io.IOException; import java.nio.file.Path; /** * 参考文件:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description * <p> * Created by Administrator on 2017/4/26. */ public class TestLucence { // 创建索引 @Test public void createIndex() throws IOException { //分词器 (对文本进行分词...) Analyzer analyzer = new StandardAnalyzer(); File file = new File( "temp/" ); Path path = file.toPath(); Directory directory = FSDirectory.open(path); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer); //构建用于操作索引的类 IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); Document document = new Document(); IndexableField filed = new TextField( "id" , Integer.toString( 1 ), Field.Store.YES); IndexableField title = new StringField( "title" , "Lucene_百度百科" , Field.Store.YES); IndexableField content = new TextField( "content" , "Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的..." , Field.Store.YES); document.add(filed); document.add(title); document.add(content); indexWriter.addDocument(document); indexWriter.close(); } @Test public void searchIndex() throws IOException, ParseException { Analyzer analyzer = new StandardAnalyzer(); File file = new File( "temp/" ); Path path = file.toPath(); //索引存放的位置.... Directory directory = FSDirectory.open(path); IndexReader indexReader = DirectoryReader.open(directory); //通过indexSearcher 去检索索引目录... IndexSearcher indexSearcher = new IndexSearcher(indexReader); //这个是一个搜索条件..,通过定义条件来进行查找 QueryParser parser = new QueryParser( "content" , analyzer); Query query = parser.parse( "apache" ); //搜索先搜索索引目录.. //找到符合query 条件的前面N条记录... TopDocs topDocs = indexSearcher.search(query, 100 ); System.out.println( "总记录数是====:" + topDocs.totalHits); ScoreDoc[] scoreDocs = topDocs.scoreDocs; //返回一个击中 for (ScoreDoc scoreDoc : scoreDocs) { int docId = scoreDoc.doc; Document document = indexSearcher.doc(docId); System.out.println(document.get( "id" )); System.out.println(document.get( "title" )); System.out.println(document.get( "content" )); } } //删除索引<br> @Test<br> public void deleteIndex() throws IOException {<br>// 创建标准分词器<br> Analyzer analyzer = new StandardAnalyzer();<br><br> IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);<br> File file = new File("temp/");<br><br> Path path = file.toPath();<br> Directory directory = FSDirectory.open(path);<br>// 创建indexWriter<br> IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);<br>// 删除全部<br> indexWriter.deleteAll();<br> indexWriter.close();<br> }} |
--
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步