利用lucene_4.3索引搜索demo
来源:互联网 发布:博购软件下载 编辑:程序博客网 时间:2024/06/06 06:52
lucene版本索引搜索的过程
package com.shentong.search;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.Date;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.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.FSDirectory;import org.apache.lucene.util.Version;public class TxtFileIndexSearchSimple {public static void main(String[] args) {String indexStorePos = "E:\\数据\\luceneindex";String field = "contents";// 搜索索引的域名字String queryString = "中国人";int hitsPerPage = 10;//显示前50个try {IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexStorePos)));IndexSearcher searcher = new IndexSearcher(reader);Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);QueryParser parser = new QueryParser(Version.LUCENE_43, field, analyzer);Query query = parser.parse(queryString);TopDocs results = searcher.search(query, 5 * hitsPerPage);ScoreDoc[] hits = results.scoreDocs;for(int i=0;i<hits.length;i++){Document doc = searcher.doc(hits[i].doc);System.out.println(doc.getField("path").stringValue());}System.out.println(hits.length);}catch(Exception e){System.out.println(e.getClass());}}public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query, int hitsPerPage,boolean raw, boolean interactive) throws IOException {// Collect enough docs to show 5 pagesTopDocs 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);while (true) {if (end > hits.length) {System.out.println("Only results 1 - " + hits.length + " of " + numTotalHits+ " total matching documents collected.");System.out.println("Collect more (y/n) ?");String line = in.readLine();if (line.length() == 0 || line.charAt(0) == 'n') {break;}hits = searcher.search(query, numTotalHits).scoreDocs;}end = Math.min(hits.length, start + hitsPerPage);for (int i = start; i < end; i++) {if (raw) { // output raw formatSystem.out.println("doc=" + hits[i].doc + " score=" + hits[i].score);continue;}Document doc = searcher.doc(hits[i].doc);String path = doc.get("path");if (path != null) {System.out.println((i + 1) + ". " + path);String title = doc.get("title");if (title != null) {System.out.println(" Title: " + doc.get("title"));}} else {System.out.println((i + 1) + ". " + "No path for this document");}}if (!interactive || end == 0) {break;}if (numTotalHits >= end) {boolean quit = false;while (true) {System.out.print("Press ");if (start - hitsPerPage >= 0) {System.out.print("(p)revious page, ");}if (start + hitsPerPage < numTotalHits) {System.out.print("(n)ext page, ");}System.out.println("(q)uit or enter number to jump to a page.");String line = in.readLine();if (line.length() == 0 || line.charAt(0) == 'q') {quit = true;break;}if (line.charAt(0) == 'p') {start = Math.max(0, start - hitsPerPage);break;} else if (line.charAt(0) == 'n') {if (start + hitsPerPage < numTotalHits) {start += hitsPerPage;}break;} else {int page = Integer.parseInt(line);if ((page - 1) * hitsPerPage < numTotalHits) {start = (page - 1) * hitsPerPage;break;} else {System.out.println("No such page");}}}if (quit)break;end = Math.min(numTotalHits, start + hitsPerPage);}}}}
0 0
- 利用lucene_4.3索引搜索demo
- iOS UITableView加索引条和搜索框Demo
- Lucene 6.10 初级 创建索引库和利用索引库进行搜索!
- lucene_4.4_简单测试
- AspNet MVC4 教学-19:Asp.Net MVC4 利用Linq技术的搜索应用快速Demo
- ListView索引实现demo
- android实用Demo索引
- 搜索----Android Demo
- ElasticSearch搜索demo
- 建立索引,搜索关键字
- 关键字索引搜索
- lucene--创建索引,搜索
- 【索引】隐式图搜索
- lucene搜索索引
- Lucene多索引搜索
- Lucene-搜索索引
- Lucene-索引创建&搜索
- 字母索引搜索ListView
- WiFi万能钥匙蹭网原理详细剖析
- python 普通继承方式和super继承方式
- epoll之二:epoll实战
- k&S公司笔试和面试
- zoj-3880-Demacia of the Ancients
- 利用lucene_4.3索引搜索demo
- 剑指offer——二叉搜索树的第k个结点
- MLiA 朴素贝叶斯
- 深度学习框架的介绍与比较(Caffe, TensorFlow, MXNet, Torch, Theano)
- 2.3、Android Studio使用Layout Editor设计UI
- 在Spring3中使用注解(@Scheduled)创建计划任务(最简化配置)
- 数据结构之【队列】--复习
- Zookeeper-入门
- 信与信封问题