第1关:使用lucene创建索引
任务描述
本关任务:使用lucene完成索引库的创建。
相关知识
如果你还没有接触过Lucene,那么强烈建议你查看背景知识了解Lucene的基础知识和开发环境的配置,如果你已经学习过Lucene那么你可以根据下列知识尝试完成本关任务。
1package step1; 2import java.io.File; 3import java.io.IOException; 4import org.apache.commons.io.FileUtils; 5import org.apache.lucene.analysis.Analyzer; 6import org.apache.lucene.analysis.standard.StandardAnalyzer; 7import org.apache.lucene.document.Document; 8import org.apache.lucene.document.Field; 9import org.apache.lucene.document.Field.Store; 10import org.apache.lucene.document.LongField; 11import org.apache.lucene.document.StoredField; 12import org.apache.lucene.document.TextField; 13import org.apache.lucene.index.IndexWriter; 14import org.apache.lucene.index.IndexWriterConfig; 15import org.apache.lucene.store.Directory; 16import org.apache.lucene.store.FSDirectory; 17import org.apache.lucene.util.Version; 18public class WriterIndex { 19 //创建索引 20 public static void createIndex() throws IOException{ 21 /********** Begin **********/ 22 //创建索引库 23 Directory dir = FSDirectory.open(new File("/temp/doc/1101/index")); 24 //创建标准分析器 25 Analyzer analyzer = new StandardAnalyzer(); 26 //创建indexwriterConfig对象 27 //第一个参数:lucene的版本信息,可以选择对应的lucene版本也可以使用LATEST 28 //第二个参数:分析器对象 29 IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer); 30 //创建indexwriter对象 31 IndexWriter index = new IndexWriter(dir,config); 32 //原始文档的路径 33 File resource = new File("source/searchsource"); 34 for (File f : resource.listFiles()) { 35 //文件名 36 String fileName = f.getName(); 37 //文件内容 38 String fileContent = FileUtils.readFileToString(f); 39 //文件路径 40 String filePath = f.getPath(); 41 //文件大小 42 long fileSize = FileUtils.sizeOf(f); 43 //创建文件名域 44 //第一个参数:域的名称 45 //第二个参数:域的内容 46 //第三个参数:是否存储 47 Field fileNameField = new TextField("filename", fileName, Store.YES); 48 //文件内容域 49 Field fileContentField = new TextField("content", fileContent, Store.YES); 50 //文件路径域(不分析、不索引、只存储) 51 Field filePathField = new StoredField("path", filePath); 52 //文件大小域 53 Field fileSizeField = new LongField("size", fileSize, Store.YES); 54 55 //创建document对象 56 Document document = new Document(); 57 //添加field 58 document.add(fileNameField); 59 document.add(fileContentField); 60 document.add(filePathField); 61 document.add(fileSizeField); 62 index.addDocument(document); 63 } 64 //关闭indexwriter 65 index.close(); 66 /********** End **********/ 67 } 68 69}
第2关:查询索引
任务描述
本关任务:在索引库中查询指定索引,即在索引库中完成搜索功能。
相关知识
查询索引也是搜索的过程。搜索就是用户输入关键字,从索引(index)中进行搜索的过程。根据关键字搜索索引,根据索引找到对应的文档,从而找到要搜索的内容(这里指磁盘上的文件)。
全文检索系统提供用户搜索的界面供用户提交搜索的关键字,搜索完成展示搜索结果。
1package step2; 2import java.io.File; 3import java.io.IOException; 4import javax.sql.rowset.serial.SerialArray; 5import org.apache.lucene.document.Document; 6import org.apache.lucene.index.DirectoryReader; 7import org.apache.lucene.index.IndexReader; 8import org.apache.lucene.index.Term; 9import org.apache.lucene.search.IndexSearcher; 10import org.apache.lucene.search.Query; 11import org.apache.lucene.search.ScoreDoc; 12import org.apache.lucene.search.TermQuery; 13import org.apache.lucene.search.TopDocs; 14import org.apache.lucene.store.Directory; 15import org.apache.lucene.store.FSDirectory; 16public class SearchIndex { 17 18 public static void searchIndex() throws IOException{ 19 /********** Begin **********/ 20 //指定索引库存放的路径 21 Directory directory = FSDirectory.open(new File("/temp/doc/1101/index")); 22 //创建indexReader对象 23 IndexReader reader = DirectoryReader.open(directory); 24 //创建indexsearcher对象 25 IndexSearcher searcher = new IndexSearcher(reader); 26 //创建查询 27 Query query = new TermQuery(new Term("content","mybatis")); 28 //执行查询 29 //第一个参数是查询对象,第二个参数是查询结果返回的最大值 30 TopDocs topDocs = searcher.search(query, 10); 31 //查询结束的总条数 32 System.out.println("查询结果的总条数:" + topDocs.totalHits); 33 //遍历查询结果 34 //topDocs.scoreDocs 存储了document对象的id 35 ScoreDoc[] scoreDocs = topDocs.scoreDocs; 36 for (ScoreDoc scoreDoc : scoreDocs) { 37 //sourceDoc.doc属性就是document对象的Id 38 //根据document的id找到document对象的id 39 //根据id获取document对象 40 Document document = searcher.doc(scoreDoc.doc); 41 System.out.println(document.get("path")); 42 System.out.println(document.get("content")); 43 } 44 reader.close(); 45 /********** End **********/ 46 } 47 48} 49
第3关:分词器的使用
任务描述
本关任务:使用分词器对一段文字进行分词处理,获取每一个单词的位置。
相关知识
要完成本关任务,你需要先学习分析器(Analyzer)的使用。
Analyzer
文本文件在被索引之前,需要经过Analyzer处理。Analyzer是由IndexWriter的构造方法来指定的,它负责从被索引文件中提取语汇单元,并提出剩下的无用信息。如果被索引内容不是纯文本文件,那就需要先将其转换为文本文档。
分析器的分析对象为文档,该文件包含一些分离的能被索引的域。
分析器(Analyzer)的执行过程
1package step3; 2import java.io.IOException; 3import org.apache.lucene.analysis.Analyzer; 4import org.apache.lucene.analysis.TokenStream; 5import org.apache.lucene.analysis.cjk.CJKAnalyzer; 6import org.apache.lucene.analysis.standard.StandardAnalyzer; 7import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; 8import org.apache.lucene.analysis.tokenattributes.OffsetAttribute; 9public class AnalyzerTest { 10 public static void main(String[] args) throws IOException { 11 /********** Begin **********/ 12 13 14 //创建一个标准分析器对象 15 Analyzer analyzer = new CJKAnalyzer(); 16 //获得tokenStream对象 17 //第一个参数:域名,可以随便给一个 18 //第二个参数:要分析的文本内容 19 TokenStream tokenStream = analyzer.tokenStream("test", 20 "我喜欢在Educoder上学习"); 21 //添加一个引用,可以获得每个关键词 22 CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); 23 //添加一个偏移量的引用,记录了关键词的开始位置以及结束位置 24 OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class); 25 //将指针调整到列表的头部 26 tokenStream.reset(); 27 //遍历关键词列表,通过incrementToken方法判断列表是否结束 28 while(tokenStream.incrementToken()) { 29 //关键词的起始位置 30 System.out.println("start->" + offsetAttribute.startOffset()); 31 //取关键词 32 System.out.println(charTermAttribute); 33 //结束位置 34 System.out.println("end->" + offsetAttribute.endOffset()); 35 } 36 tokenStream.close(); 37 38 39 /********** End **********/ 40 } 41}
《头歌Educoder答案 Lucene - 全文检索入门》 是转载文章,点击查看原文。