Lucene6入门教程(三)搜索和查询
2017-08-21 16:43
447 查看
要想实现搜索,就需要构建索引和搜索引擎这两个方面,上节已经实现目录的构建,那么,在(三)中,我们需要实现的便是搜索引擎的构建了。 **(一)搜索构建的步骤如下:** (1)创建Directory和IndexReader;(即空间支持和读取文件) (2)根据IndexReader创建IndexSearch;(对IndexReader的查询实现) (3)创建搜索的Query、parser和Query ;(表示查询方法/范围/对象) (4)searcher开始搜索并且返回TopDocs;(开始搜索,记录topDocs) (5)根据TopDocs获取对象,结束。 **(二)IndexSearch()介绍** IndexSearch()是索引查询器,用以完成Lucene6搜索引擎(索引内容和查询索引)中,索引查询器的构建过程。 IndexSearch()的特点: (1)它实现了对单个IndexReader查询; (2)之后调用search(Query,n)方法实现对索引的查询; (3)可共享,多线程和实时查询等; **(三)Query(查询)的介绍** Query类是是一个抽象类,包含各种各样的实用方法,它的所有类型查询的子类都是在Lucene6的搜索过程中使用的,常与Term类(查询搜索中的最低单位,类似于Field)使用。 搜索过程中,需要用创建parser确定要搜索的内容,需要query1创建搜索的对象。
//java代码,搜索查询“content” package com.Licene6; 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.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.Directory; import org.apache.lucene.store.FSDirectory; import java.io.IOException; import java.nio.file.Paths; /***Created by Mo *On 2017/8/18 ***14:18. * * Lucene6.4.1搜索 ******/ public class Searcher { //这个方法是搜索索引的方法,传入索引路径和查询表达式 public static void search(String indexDir,String query) throws IOException, ParseException { //打开索引目录 Directory dir= FSDirectory.open(Paths.get(indexDir)); // 1234、创建搜索的Query IndexSearcher searcher=new IndexSearcher(DirectoryReader.open(dir)); // 使用标准的分词器 Analyzer analyzer = new StandardAnalyzer(); // 在content中搜索,创建parser确定要搜索的内容,其中,第2个参数为搜索的域 QueryParser parser=new QueryParser("contents",analyzer); // 创建Query表示搜索域为content中,包含搜索内容为query1的文档 Query query1=parser.parse(query); long start=System.currentTimeMillis(); // 开始搜索 TopDocs hits=searcher.search(query1,11); long end=System.currentTimeMillis(); System.out.println(hits.totalHits); System.out.println(end-start);//计算搜搜时间等 //获取搜索的地址等 for(ScoreDoc scoreDoc:hits.scoreDocs){ Document doc=searcher.doc(scoreDoc.doc); System.out.println(doc.get("fullpath"));//地址,完整的 } } public static void main(String[] args) throws IOException, ParseException { String indexDir="D:\\workspace\\lucene6.4.1\\learing2017.8\\0818\\index";//索引,index时建立的 String query="content";//搜索的word search(indexDir, query); } }
参考文献:
1.索引查询器:http://blog.csdn.net/wuyinggui10000/article/details/45698667
2.搜索初步:http://blog.csdn.net/zpf336/article/details/45079319
相关文章推荐
- Lucene6入门教程(六)搜索/查询方式
- Lucene 6.2.1入门教程(三) 近实时搜索的变化
- 最新版本Lucene 4.2.1入门教程(查询构造)
- Lucene 6.2.1入门教程(一) 创建索引和基本搜索索引
- Lucene教程--入门程序详解
- solr入门之搜索建议的具体查询及拼音加汉字搜索建议的思考
- 1.搜索引擎的历史,搜索引擎起步,发展,繁荣,搜索引擎的原理,搜索技术用途,信息检索过程,倒排索引,什么是Lucene,Lucene快速入门
- 【Lucene4.8教程之一】使用Lucene4.8进行索引及搜索的基本操作
- solr入门之lucene的索引操作之增加,删除,修改,查询及查询解析代码模板
- Lucene入门教程
- 【Lucene3.6.2入门系列】第08节_高级搜索之自定义评分
- JAVA:lucene 入门学习,简单实例模访google搜索
- Lucene入门教程
- lucene入门-简单的WEB搜索界面
- 兄弟连区块链入门到精通教程基础开发通过接口查询xmra提币情况
- Lucene 3.6.2入门简述Lucene中常见的搜索功能
- Asp.Net MVC4.0 官方教程 入门指南之七--增加搜索方法和视图
- 一步一步跟我学习lucene(12)---lucene搜索之分组处理group查询
- lucene-查询query->PhraseQuery多关键字的搜索
- ASP.NET Aries 入门开发教程3:开发一个列表页面及操控查询区_0