lucene全文检索操作实例
发表于|更新于
|阅读量:
Lucene是apache下的一个开放源代码的全文检索引擎工具包。提供了完整的查询引擎和索引引擎,部分文本分析引擎。Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能。
<The rest of contents | 余下全文>
全文检索
将非结构化数据中的一部分信息提取出来,重新组织,使其变得有一定结构,然后对此有一定结构的数据进行搜索,从而达到搜索相对较快的目的。这部分从非结构化数据中提取出的然后重新组织的信息,我们称之索引。
例如:字典。字典的拼音表和部首检字表就相当于字典的索引,对每一个字的解释是非结构化的,如果字典没有音节表和部首检字表,在茫茫辞海中找一个字只能顺序扫描。然而字的某些信息可以提取出来进行结构化处理,比如读音,就比较结构化,分声母和韵母,分别只有几种可以一一列举,于是将读音拿出来按一定的顺序排列,每一项读音都指向此字的详细解释的页数。我们搜索时按结构化的拼音搜到读音,然后按其指向的页数,便可找到我们的非结构化数据——也即对字的解释。
这种先建立索引,再对索引进行搜索的过程就叫全文检索**(Full-text Search)**。
虽然创建索引的过程也是非常耗时的,但是索引一旦创建就可以多次使用,全文检索主要处理的是查询,所以耗时间创建索引是值得的。对于数据量大、数据结构不固定的数据可采用全文检索方式搜索,比如百度、Google等搜索引擎、论坛站内搜索、电商网站站内搜索等。
Tip:lucene对java环境的最低要求为JAVA1.8
创建索引
第一步:创建一个java工程,并导入jar包。
工程所需jar包:

第二步:创建一个indexwriter对象。
1)指定索引库的存放位置Directory对象
2)指定一个IndexWriterConfig对象。
第二步:创建document对象。
第三步:创建field对象,将field添加到document对象中。
第四步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。
第五步:关闭IndexWriter对象。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| @Test public void createIndex() throws Exception { Directory directory = FSDirectory.open(new File("c:\\temp\\index").toPath()); IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig()); File dir = new File("C:\\Users\\liu\\Desktop\\searchsource"); File[] files = dir.listFiles(); for (File f : files) { String name = f.getName(); String filePath = f.getPath(); String fileContent = FileUtils.readFileToString(f, "utf-8"); long fileSize = FileUtils.sizeOf(f); Field fieldName = new TextField("name", name, Field.Store.YES); Field fieldPath = new TextField("path", filePath, Field.Store.YES); Field fieldContent = new TextField("content", fileContent, Field.Store.YES); Field fieldSize = new TextField("size", fileSize + "", Field.Store.YES); Document document = new Document(); document.add(fieldName); document.add(fieldPath); document.add(fieldContent); document.add(fieldSize); indexWriter.addDocument(document);
} indexWriter.close(); }
|
createIndex方法运行后会在c:\\temp\\index文件夹下生成索引文件,如下图:

生成的索引文件为二进制文件,可以使用luke打开索引
查看索引
luck开始界面如下,在indexPath中选择索引存储位置文件夹即可


查询索引
第一步:创建一个Directory对象,也就是索引库存放的位置。
第二步:创建一个indexReader对象,需要指定Directory对象。
第三步:创建一个indexsearcher对象,需要指定IndexReader对象
第四步:创建一个TermQuery对象,指定查询的域和查询的关键词。
第五步:执行查询。
第六步:返回查询结果。遍历查询结果并输出。
第七步:关闭IndexReader对象
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| @Test public void searchIndex() throws Exception { Directory directory = FSDirectory.open(new File("c:\\temp\\index").toPath()); IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Query query = new TermQuery(new Term("content", "spring"));
TopDocs topDocs = indexSearcher.search(query, 10); System.out.println("查询总记录数:" + topDocs.totalHits);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc doc : scoreDocs) {
int docId = doc.doc; Document document = indexSearcher.doc(docId); System.out.println(document.get("name")); System.out.println(document.get("path")); System.out.println(document.get("size")); System.out.println(document.get("content")); System.out.println("--------------------------"); } indexReader.close();
}
|
部分查询结果如下图:
