Lucene教程(三)- 理解搜索过程的核心类
2013-10-29 22:24
363 查看
上一篇博客,我们学习了索引过程的核心类,并且重构了IndexFiles,
在这一篇博客,我们学习一下搜索过程的核心类,并重构一下SearchFiles类。
下面,我们来重构一下SearchFiles类,删除了分页操作,和一些参数什么的。
在这一篇博客,我们学习一下搜索过程的核心类,并重构一下SearchFiles类。
// Now search the index: DirectoryReader ireader = DirectoryReader.open(directory); IndexSearcher isearcher = new IndexSearcher(ireader); // Parse a simple query that searches for "text": QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer); Query query = parser.parse("text"); ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs; assertEquals(1, hits.length); // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = isearcher.doc(hits[i].doc); assertEquals("This is the text to be indexed.", hitDoc.get("fieldname")); } ireader.close(); directory.close();
1.IndexSearcher
IndexSearcher用于搜索IndexWriter类所创建的索引。可以将IndexSearcher类看作是一个以只读方式打开索引的类,并提供了一些search()方法。2. Term
项(Term)是用于搜索的一个基本单元。如同域对象一样,它包括了一对字符串元素:与域中的域名和域值相对应。3. Query
Lucene有很多的具体的查询(Query)子类,后面会详细讲解。下面,我们来重构一下SearchFiles类,删除了分页操作,和一些参数什么的。
package org.ygy.lucene; import java.io.File; import java.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.index.DirectoryReader; import org.apache.lucene.index.IndexReader; 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.FSDirectory; import org.apache.lucene.util.Version; public class SearchFiles { public static void main(String[] args) throws Exception { String index = "F:\\Lucene_index"; String field = "contents"; String queryString = "aa"; int hitsPerPage = 10; //读取索引 IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); //查询索引 IndexSearcher searcher = new IndexSearcher(reader); //分析器 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45); //解析器 QueryParser parser = new QueryParser(Version.LUCENE_45, field, analyzer); Query query = parser.parse(queryString); System.out.println("Searching for: " + query.toString(field)); doSearch(searcher, query, hitsPerPage); reader.close(); } public static void doSearch(IndexSearcher searcher, Query query, int hitsPerPage) throws IOException { TopDocs results = searcher.search(query, 5 * hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits; System.out.println(numTotalHits + " total matching documents"); int start = 0; int end = Math.min(numTotalHits, hitsPerPage); //遍历查询结果 for (int i = start; i < end; i++) { Document doc = searcher.doc(hits[i].doc); String path = doc.get("path"); if (path != null) { System.out.println(i + 1 + ". " + path); } else { System.out.println(i + 1 + ". " + "No path for this document"); } } } }
相关文章推荐
- C++ 指针和引用的区别
- Strict Weak Ordering
- Gherkin学习笔记
- 框架模式和设计模式的区别
- (字符串的处理4.7.22)POJ 3337 Expression Evaluator(解析C风格的字符串)
- 我快成为了我一直讨厌的那种人(说好的回来好好做人呢?)
- Android监听WIFI网络的变化并且获得当前信号强度
- 关于free使用注意
- Bash Shell中命令行选项/参数处理
- linux配置java环境变量64位
- linux菜鸟(me)的一周计划
- C语言学习笔记
- Havana中VM的reboot分析
- iOS开发-UIWebView
- android中得到所有安装的应用程序及区分其是否为系统应用程序还是用户应用程序
- 在A*寻路中使用二叉堆
- hdu 4284 Travel(壮压DP&TSP&floyd)
- 分享一组Rpg Marker人物行走,游戏素材图片,共20张图片
- 《UNIX-Shell编程24学时教程》读书笔记Chap1,2 Shell基础,脚本基础
- [置顶] 二叉树层次遍历的应用--判断一颗二叉树是否为规则二叉树