这里需要完成一个能对txt文本建立索引,并能完成检索查询。完成这个功能,使用的是Lucene4.5,同时使用其自带的中文分析器。

  准备工作是在一个文件夹里面建一些txt文件,这是我的文件结构:

  

  首先要对这些文本建立索引,代码如下

 package com.test;

 import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.*; import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version; public class Indexer { /**
* @param args
*/ private static String fileInput = "C:\\Users\\Press-Lab\\Desktop\\五月天歌词文件";
//此处是索引存放的路径
private static String indexPath = "C:\\Users\\Press-Lab\\Desktop\\index"; public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//此处去处txt的内容和路径,装入list中,方便下一步放入document中
File[] files = new File(fileInput).listFiles();
List<FileBag> list = new ArrayList<FileBag>();
for(File f : files){
BufferedReader br = new BufferedReader(new FileReader(f));
StringBuffer sb = new StringBuffer();
String line = null;
while((line = br.readLine()) != null){
sb.append(line);
}
br.close();
FileBag fileBag = new FileBag();
fileBag.setContent(sb.toString());
fileBag.setPath(f.getAbsolutePath());
list.add(fileBag);
} //此处为建立索引
Directory dir = FSDirectory.open(new File(indexPath));
//此处使用自带的中文分析器
SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_45);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_45, analyzer);
IndexWriter writer = new IndexWriter(dir, config); //对list中的每一个对象,分别连击索引
for(FileBag fileBag : list){
Document doc = new Document();
doc.add(new Field("contents", fileBag.getContent(), Field.Store.YES,Field.Index.ANALYZED));
doc.add(new StringField("path", fileBag.getPath(), Field.Store.YES));
writer.addDocument(doc);
} writer.close(); } }
//建立一个与txt对应的domain对象
class FileBag{
private String content;
private String path;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
} }

  在这段代码中,我对文本进行了存储,一般情况下是无需存储的,这里为为了方便查看结果才进行存储。

  特别注意代码的54行,如果使用Lucene4.5推荐的TextField,这无法建立索引。不知道这是个什么原因,有人解决过这个问题的麻烦告知下。

  下面是进行检索的代码:

 package com.test;

 import java.io.*;

 import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
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.Term; 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.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version; import com.dong.Constants; public class Seacher { /**
* @param args
*/
private static String indexPath = "C:\\Users\\Press-Lab\\Desktop\\index";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub Directory dir=FSDirectory.open(new File(indexPath));
IndexReader reader=DirectoryReader.open(dir);
IndexSearcher searcher=new IndexSearcher(reader);
//此处也需要是用中文分析器
SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_45);
QueryParser parser = new QueryParser(Version.LUCENE_45, "contents", analyzer);
Query query = parser.parse("如果") ; TopDocs topdocs=searcher.search(query, 5);
ScoreDoc[] scoreDocs=topdocs.scoreDocs; System.out.println("共有数据:" + topdocs.scoreDocs.length + "条");
for(int i=0; i < scoreDocs.length; i++) {
int doc = scoreDocs[i].doc;
Document document = searcher.doc(doc);
System.out.println("第" + i + "条文本的路径是: " + document.get("path"));
System.out.println("第" + i + "条文本内容是: " + document.get("contents")); }
reader.close();
} }

  这里需要注意的是,查询时候也需要中文分析器。

  下一篇要做的是,实现索引的分页查询。

  

用Lucene4.5对中文文本建立索引的更多相关文章

  1. Lucene4.9学习笔记——Lucene建立索引

    基本上创建索引需要三个步骤: 1.创建索引库IndexWriter对象 2.根据文件创建文档Document 3.向索引库中写入文档内容 这其中主要涉及到了IndexWriter(索引的核心组件,用于 ...

  2. 万字总结Keras深度学习中文文本分类

    摘要:文章将详细讲解Keras实现经典的深度学习文本分类算法,包括LSTM.BiLSTM.BiLSTM+Attention和CNN.TextCNN. 本文分享自华为云社区<Keras深度学习中文 ...

  3. lucene 建立索引的过程

    时间 -- ::  CSDN博客 原文 http://blog.csdn.net/caohaicheng/article/details/ 看lucene主页(http://lucene.apach ...

  4. 【Lucene4.8教程之二】索引

    一.基础内容 0.官方文档说明 (1)org.apache.lucene.index provides two primary classes: IndexWriter, which creates ...

  5. (转)Mysql哪些字段适合建立索引

    工作中处理数据时,发现某个表的数据达近亿条,所以要为表建索引提高查询性能,以下两篇文章总结的很好,记录一下,以备后用. 数据库建立索引常用的规则如下: 1.表的主键.外键必须有索引: 2.数据量超过3 ...

  6. Mysql哪些字段适合建立索引

    数据库建立索引常用的规则如下: 1.表的主键.外键必须有索引: 2.数据量超过300的表应该有索引: 3.经常与其他表进行连接的表,在连接字段上应该建立索引: 4.经常出现在Where子句中的字段,特 ...

  7. mysql中建立索引的一些原则

    1.先存数据,再建索引 有索引的好处是搜索比较快但是在有索引的前提下进行插入.更新操作会很慢 2.不要对规模小的数据表建立索引,数据量超过300的表应该有索引:对于规模小的数据表建立索引 不仅不会提高 ...

  8. MongoDB优化,建立索引实例及索引机制原理讲解

    MongoDB优化,建立索引实例及索引机制原理讲解 为什么需要索引? 当你抱怨MongoDB集合查询效率低的时候,可能你就需要考虑使用索引了,为了方便后续介绍,先科普下MongoDB里的索引机制(同样 ...

  9. mysql建立索引的一些小规则

    1.表的主键.外键必须有索引: 2.数据量超过300的表应该有索引: 3.经常与其他表进行连接的表,在连接字段上应该建立索引: 4.经常出现在Where子句中的字段,特别是大表的字段,应该建立索引: ...

随机推荐

  1. Linux性能优化 第六章 性能工具:磁盘I/O

    6.1 磁盘I/O介绍 一般来说,Linux磁盘的每个分区要么包含一个文件系统,要么包含一个交换分区.这些分区被挂载到Linux根文件系统,该系统由/etc/fstab指定.这些被挂载的文件系统包含了 ...

  2. WPF c# 定时器

    //定时查询-定时器 DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += (s, e) = ...

  3. WPF圆角按钮与触发颜色变化

    <Button x:Name="button1" Content="按钮1" Margin="10,10,0,0" Cursor=&q ...

  4. js 原生图片上传

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. java多线程的认识

    [线程定义]   定义:线程(英语:thread)是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程 ...

  6. uva-519-拼图

    给你N*M个碎片,问能否用他们拼成一个矩形,矩形的边缘要全是F,除外界边缘的边要么是I,要么O,不能是F1.碎片会重复出现,所以同样的碎片在同一个位置,如果已经不能放,直接跳过就行2.矩形的边缘要全是 ...

  7. python中的count

    count(self, sub, start=None, end = None)用于计算字符串中子序列的个数,sub, start=None, end = None定义查找范围,不写默认查找全部 举个 ...

  8. 如何删除GitHub中已经建好的仓库(repository)

    我们有时候可能需要清理 GitHub 中一些不维护的或不需要的项目,此时就要用到delete操作了,很多新手可能不知道如何删除已有仓库,下面将简单介绍下,需要注意的是删除操作不能恢复,一旦执行此操作, ...

  9. C# 趣味小程序(4)——遍历特定目录及其子目录

    //递归方法遍历目录,并统计其中文件的数目        private int statisticFiles(string directory)        {            int st ...

  10. ansible常用模块即用法

    Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc  -s  模块名  又可以查看该模块有哪些参数可以使用. 下面 ...