首页 技术 正文
技术 2022年11月19日
0 收藏 753 点赞 2,611 浏览 2168 个字
  • 前提:在使用lucene进行搜索的时候,必须先生成索引文件,即必须先进行上一章节的案例,生成索引文件如下:

(二)Lucene之根据关键字搜索文件

  • 该索引文件为”segments”开头,如果没有该文件则说明没有索引文件则报错:org.apache.lucene.index.IndexNotFoundException: no segments* file found in SimpleFSDirectory@E:\lucene\index lockFactory=org.apache.lucene.store.NativeFSLockFactory@87aac27: files: [_0.cfe, _0.cfs, _0.si, write.lock]
  • 搜索

package com.shyroke.lucene;import java.io.IOException;
import java.nio.file.Paths;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.index.StandardDirectoryReader;
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.SimpleFSDirectory;public class Search { /**
* 根据关键字检索文件
*
* @param indexDir
* 存放索引的目录
* @param key
* 关键字
* @throws IOException
* @throws ParseException
*/
public static void search(String indexDir, String key) throws IOException, ParseException {
Directory directory = new SimpleFSDirectory(Paths.get(indexDir));
IndexReader reader = DirectoryReader.open(directory); Analyzer analyzer=new StandardAnalyzer();
QueryParser queryParser=new QueryParser("fileContents", analyzer);
Query query=queryParser.parse(key); IndexSearcher searcher = new IndexSearcher(reader); long startTime=System.currentTimeMillis();
TopDocs topDocs=searcher.search(query, 10);
long endTime=System.currentTimeMillis();
System.out.println("匹配 "+key+" 总共花费:"+(endTime-startTime)+"毫秒,查询到"+topDocs.totalHits+"条记录"); for(ScoreDoc scoreDoc:topDocs.scoreDocs) {
Document document=searcher.doc(scoreDoc.doc);
System.out.println(document.get("filePath"));
} } public static void main(String[] args) {
String indexDir="E:\\lucene\\index";
String key="Zygmunt#Saloni";
try {
Search.search(indexDir, key);
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}

结果:

(二)Lucene之根据关键字搜索文件

  • 此时的关键字是“Zygmunt#Saloni” ,查询结果是在LICENSE.txt中,但是该文件中并没有这个内容,但是有

(二)Lucene之根据关键字搜索文件 这样也会匹配到,这是分词器StandardAnalyzer在起作用。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,122
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,594
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,439
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,210
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,846
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,931