lucene3.5学习笔记02--创建索引和建立搜索
来源:互联网 发布:阿里云的域名还要备案? 编辑:程序博客网 时间:2024/05/21 00:52
先大致了解一下lucene的组成结构
lucene的组成结构:对于外部应用来说索引模块(index)和检索模块(search)是主要的外部应用入口
org.apache.Lucene.search/搜索入口org.apache.Lucene.index/索引入口org.apache.Lucene.analysis/语言分析器org.apache.Lucene.queryParser/查询分析器org.apache.Lucene.document/存储结构org.apache.Lucene.store/ 底层IO/存储结构org.apache.Lucene.util/一些公用的数据结构接下来,我们构建一个最简单的文件搜索样例
- 先在我的电脑里面创建两个空文件夹
E:\lucene\data 用来存放数据,代表要搜索的文件
E:\lucene\index 原来存放lucene为数据创建的索引文件 - 构造一点假数据
E:\lucene\data\1.txt 内容为 a1a2a3
E:\lucene\data\2.txt 内容为 b1b2b3
E:\lucene\data\3.txt 内容为 c1c2c3 honor - 建立索引
public static void createIndex(String filePath, String indexPath) throws IOException {Version version = Version.LUCENE_35;File indexFile = new File(indexPath);FSDirectory directory = FSDirectory.open(indexFile);IndexWriterConfig conf = new IndexWriterConfig(version, new SimpleAnalyzer(version));IndexWriter writer = new IndexWriter(directory, conf);List<File> files = FileList.getFiles(filePath);// 获取该路径下所有文件for(File file:files){System.out.println("Indexing file " + file);// 构造Document对象Document doc = new Document();doc.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.ANALYZED));doc.add(new Field("uri", file.getPath(), Field.Store.YES, Field.Index.NO));String text = FileText.getText(file);// 获取该文件内容doc.add(new Field("text", text, Field.Store.YES, Field.Index.ANALYZED));//将文件内容索引在text// 将文档写入索引writer.addDocument(doc);}// 关闭写索引器writer.close();}
public static void main(String[] args) {String filePath = "E:/lucene/data";String indexPath = "E:/lucene/index";//try{createIndex(filePath, indexPath);}catch(IOException e){e.printStackTrace();}}
这时E:\lucene\index\ 目录下生成的索引文件如下 - 建立搜索
public static void search(String keyword, String indexPath) throws CorruptIndexException, IOException, ParseException {Version version = Version.LUCENE_35;// 指向索引目录的搜索器File indexFile = new File(indexPath);FSDirectory directory = FSDirectory.open(indexFile);IndexReader reader = IndexReader.open(directory);IndexSearcher searcher = new IndexSearcher(reader);// 查询解析器:使用和索引同样的语言分析器 查询text字段QueryParser parser = new QueryParser(version, "text", new SimpleAnalyzer(version));// text 字段Query query = parser.parse(keyword);// 搜索结果使用Hits存储TopDocs hits = searcher.search(query, null, 10);// 通过hits可以访问到相应字段的数据和查询的匹配度System.out.println(hits.totalHits + " total results");System.out.println("-----匹配结果如下------");ScoreDoc[] scoredocs = hits.scoreDocs;for(int i = 0; i < scoredocs.length; i++){ScoreDoc scoreDoc = scoredocs[i];Document d = searcher.doc(scoreDoc.doc);String path = d.get("uri");System.out.println(i + "--得分:" +scoreDoc.score +" 文件路径:"+path);}searcher.close();}
public static void main(String[] args) {String indexPath = "E:/lucene/index";try{// 搜索 honor 这个关键字search("honor",indexPath);}catch(CorruptIndexException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();}catch(ParseException e){e.printStackTrace();}}
控制台输出如下
1 total results
-----匹配结果如下------
0--得分:0.70273256 文件路径:E:\lucene\data\3.txt
怎么样,利用lucene实现检索很简单吧
由于没有涉及到中文,使用lucene自带的分析器就可以了
要是中文还得使用中文分词器,这个接下来再学习
- lucene3.5学习笔记02--创建索引和建立搜索
- lucene3.5建立索引和搜索的简单步骤
- lucene3.5建立索引和查询
- 使用lucene3.6创建索引和实现简单搜索
- 搜索技术--建立索引(lucene3.0)
- Lucene3.5 之 索引和搜索简单示例
- 每日一练 Lucene3.5 建立索引
- lucene3.6.0索引操作的学习笔记
- Lucene3.0 创建索引及多目录搜索
- Lucene3.5——创建索引
- lucene3.0学习ing——建立索引
- Lucene3.5 之 索引删除和更新
- Lucene3.5 之 索引删除和更新
- lucene3.5更新索引
- Lucene 建立索引和搜索
- 【Lucene02】索引和搜索建立
- Lucene3.5 的索引的创建,删除,更新,加权
- Lucene3.4结合IKAnalyzer3.2 建立索引
- awk常用脚本
- java 多线程的同步 wait notifyAll synchronized
- 将普通过程赋值给事件,TMethod的妙用
- DirectX开发环境的快速配置
- 什么是Boot Loader
- lucene3.5学习笔记02--创建索引和建立搜索
- make工具介绍
- 打工的同时如何开始创业
- 收集的.NET基础资料
- ddoc
- perl -- 子过程 -->数组参数传入
- python 列表存储模型迷惑
- C++中const的使用
- 【ASP.NET】 Web Api (.NET 4.5)