Lucene是一个全文检索的框架,apache组织提供了一个用Java实现的全文搜索引擎的开源项目。这里我们对apache的lucene的框架做一个简单的介绍。心甘情愿这四个字,透着一股卑微,但也有藏不住的勇敢。
lucene的第一个实例
一、maven中引入的依赖pom.xml
<properties> <lucene.version>6.6.0</lucene.version> <common-io.version>2.5</common-io.version> </properties> <dependencies> <!-- lucene core的依赖--> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>${lucene.version}</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>${lucene.version}</version> </dependency> <!-- commomsIO的依赖--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${common-io.version}</version> </dependency> </dependencies>
二、关于lucene的java代码如下
package com.linux.huhx.lucene_1; import org.apache.commons.io.FileUtils; 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.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; 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.RAMDirectory; import java.io.File; import java.io.IOException; public class BaseLucene_1 { public static void main(String[] args) throws Exception { // 存放在内存中 Directory dir = new RAMDirectory(); new BaseLucene_1().createIndex(dir); new BaseLucene_1().search(dir); } /** * 创建索引 */ public void createIndex(Directory dir) throws IOException { IndexWriter indexWriter = null; // 1. 创建 Directory (索引存放位置),这里是参数dir // 2. 创建IndexWriter 写索引 Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(analyzer); indexWriter = new IndexWriter(dir, iwc); // 3. 创建Document 对象 field Document document; File file = new File("file/example"); for (File f : file.listFiles()) { document = new Document(); // 4. 为Documen添加field document.add(new Field("content", FileUtils.readFileToString(f, "utf-8"), TextField.TYPE_STORED)); document.add(new TextField("fileName", f.getName(), Field.Store.YES)); document.add(new StringField("filePath", f.getAbsolutePath(), Field.Store.YES)); // 5. 通过IndexWriter 添加文档到索引中 indexWriter.addDocument(document); } indexWriter.close(); } /** * 根据内容搜索 * @param dir * @throws Exception */ public void search(Directory dir) throws Exception { IndexReader indexReader = null; // 1. 创建 Directory,这里通过参数传递过来的dir // Directory dir = FSDirectory.open(new File("file/index").toPath()); // 本地磁盘 // 2. 创建 IndexReader indexReader = DirectoryReader.open(dir); // 3. 创建 IndexSearch IndexSearcher indexSearcher = new IndexSearcher(indexReader); // 4. 创建搜索的Query // 创建parse确定搜索的内容,第二个参数为搜索的file QueryParser queryParser = new QueryParser("content", new StandardAnalyzer()); // 创建Query,表示搜索域中的内容 Query query = queryParser.parse("love"); // 5. 搜索并返回 TopDocs TopDocs topDocs = indexSearcher.search(query, 10); // 6. 根据topDocs 获得 scoreDocs ScoreDoc[] socreDocs = topDocs.scoreDocs; for (ScoreDoc doc : socreDocs) { // 获取Document对象 Document document = indexSearcher.doc(doc.doc); // 根据Document对象获取需要的值 System.out.println(document.get("fileName")); System.out.println(document.get("content")); } indexReader.close(); } }
三、运行的结果如下
java1.txt I love you, java. java2.txt I love you, linux. java3.txt I love you, huhx.
友情链接
时间: 2024-11-11 14:00:30