- 浏览: 140961 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
zi_wu_xian:
docx格式的word文件虽然是zip格式的,也可以看到xml ...
用Java操作Office 2007 -
MyDreamNotDream:
看代码看到这里很不容易呢。
Java中HashMap的实现原理 -
四书五经:
to 楼上的 SonofGod :这个时候这样去获取:如果(值 ...
Java中HashMap的实现原理 -
SonofGod:
请问 楼主 在疑问3中。多个key的hash值一样的话,存储时 ...
Java中HashMap的实现原理 -
SonofGod:
请问 楼主 在疑问2中。多个可以的hash得到一样的hash值 ...
Java中HashMap的实现原理
因为lucene索引的时候是将String型的信息建立索引的,所以这里必须是将word/pdf/html等文件的内容转化问字符型。
lucene的jar包自己去下载。
首先是建立索引的代码:
public class TextFileIndexer {
public static void main(String[] args) throws Exception {
/* 指明要索引文件夹的位置,这里是d盘的s文件夹下 */
File fileDir = new File("d:\\s");
/* 这里放索引文件的位置 */
File indexDir = new File("d:\\index");
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,
true);
File[] textFiles = fileDir.listFiles();
long startTime = new Date().getTime();
//增加document到索引去
System.out.println("File正在被索引.");
/*
* 注意要变的就是这里,路径和读取文件的方法
* */
String path ="d:\\s\\2.doc";
String temp = ReadFile.readWord(path);
// String path ="d:\\s\\index.htm";
// String temp = ReadFile.readHtml(path);
Document document = new Document();
Field FieldPath = new Field("path",path,
Field.Store.YES, Field.Index.NO);
Field FieldBody = new Field("body", temp, Field.Store.YES,
Field.Index.TOKENIZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(FieldPath);
document.add(FieldBody);
indexWriter.addDocument(document);
//optimize()方法是对索引进行优化
indexWriter.optimize();
indexWriter.close();
//测试一下索引的时间
long endTime = new Date().getTime();
System.out
.println("这花费了"
+ (endTime - startTime)
+ " 毫秒来把文档增加到索引里面去!"
+ fileDir.getPath());
}
}
上面已经注释了要换的地方,我们要做的就是换文件的路径和读取文件的方法。
下面来具体看下读取文件的方法
1.首先来看WORD文档:
我这里用的是poi,相关jar包自己去下载,然后加到工程中(以下所要用的jar包也是,不再重复说)。
来看相关代码:
public static String readWord(String path) {
StringBuffer content = new StringBuffer("");// 文档内容
try {
HWPFDocument doc = new HWPFDocument(new FileInputStream(path));
Range range = doc.getRange();
int paragraphCount = range.numParagraphs();// 段落
for (int i = 0; i < paragraphCount; i++) {// 遍历段落读取数据
Paragraph pp = range.getParagraph(i);
content.append(pp.text());
}
} catch (Exception e) {
}
return content.toString().trim();
}
2.PDF文件用的是PDFbox:
public static String readPdf(String path) throws Exception {
StringBuffer content = new StringBuffer("");// 文档内容
FileInputStream fis = new FileInputStream(path);
PDFParser p = new PDFParser(fis);
p.parse();
PDFTextStripper ts = new PDFTextStripper();
content.append(ts.getText(p.getPDDocument()));
fis.close();
return content.toString().trim();
}
3.html文件:
public static String readHtml(String urlString) {
StringBuffer content = new StringBuffer("");
File file = new File(urlString);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
// 读取页面
BufferedReader reader = new BufferedReader(new InputStreamReader(
fis,"utf-8"));//这里的字符编码要注意,要对上html头文件的一致,否则会出乱码
String line = null;
while ((line = reader.readLine()) != null) {
content.append(line + "\n");
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
String contentString = content.toString();
return contentString;
}
4.txt文件:
public static String readTxt(String path) {
StringBuffer content = new StringBuffer("");// 文档内容
try {
FileReader reader = new FileReader(path);
BufferedReader br = new BufferedReader(reader);
String s1 = null;
while ((s1 = br.readLine()) != null) {
content.append(s1 + "\r");
}
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString().trim();
}
接下来数搜索代码:
public class TestQuery {
public static void main(String[] args) throws IOException, ParseException {
Hits hits = null;
//搜索内容自己换
String queryString = "根据国务院的决定";
Query query = null;
IndexSearcher searcher = new IndexSearcher("d:\\index"); //这里注意索引存放的路径
Analyzer analyzer = new StandardAnalyzer();
try {
QueryParser qp = new QueryParser("body", analyzer);
/**
* 建索引的时候我们指定了body建立为内容,我们搜索的时候也是针对body的,所以
* QueryParser qp = new QueryParser("body", analyzer);
* 这句和建立索引时候
Field FieldBody = new Field("body", temp, Field.Store.YES,
Field.Index.TOKENIZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
*的这句的"body"是对应的。
*/
query = qp.parse(queryString);
} catch (ParseException e) {
System.out.println("异常");
}
if (searcher != null) {
hits = searcher.search(query);
if (hits.length() > 0) {
System.out.println("找到:" + hits.length() + " 个结果!");
for (int i = 0; i < hits.length(); i++) {//输出搜索信息
Document document = hits.doc(i);
System.out.println("contents:"+document.get("body"));
//同样原理这里的document.get("body")就是取得建立在索引文件里面的额body的所有内容
//你若想输出文件路径就用document.get("path")就可以了
}
} else{
System.out.println("0个结果!");
}
}
}
lucene的jar包自己去下载。
首先是建立索引的代码:
public class TextFileIndexer {
public static void main(String[] args) throws Exception {
/* 指明要索引文件夹的位置,这里是d盘的s文件夹下 */
File fileDir = new File("d:\\s");
/* 这里放索引文件的位置 */
File indexDir = new File("d:\\index");
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,
true);
File[] textFiles = fileDir.listFiles();
long startTime = new Date().getTime();
//增加document到索引去
System.out.println("File正在被索引.");
/*
* 注意要变的就是这里,路径和读取文件的方法
* */
String path ="d:\\s\\2.doc";
String temp = ReadFile.readWord(path);
// String path ="d:\\s\\index.htm";
// String temp = ReadFile.readHtml(path);
Document document = new Document();
Field FieldPath = new Field("path",path,
Field.Store.YES, Field.Index.NO);
Field FieldBody = new Field("body", temp, Field.Store.YES,
Field.Index.TOKENIZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(FieldPath);
document.add(FieldBody);
indexWriter.addDocument(document);
//optimize()方法是对索引进行优化
indexWriter.optimize();
indexWriter.close();
//测试一下索引的时间
long endTime = new Date().getTime();
System.out
.println("这花费了"
+ (endTime - startTime)
+ " 毫秒来把文档增加到索引里面去!"
+ fileDir.getPath());
}
}
上面已经注释了要换的地方,我们要做的就是换文件的路径和读取文件的方法。
下面来具体看下读取文件的方法
1.首先来看WORD文档:
我这里用的是poi,相关jar包自己去下载,然后加到工程中(以下所要用的jar包也是,不再重复说)。
来看相关代码:
public static String readWord(String path) {
StringBuffer content = new StringBuffer("");// 文档内容
try {
HWPFDocument doc = new HWPFDocument(new FileInputStream(path));
Range range = doc.getRange();
int paragraphCount = range.numParagraphs();// 段落
for (int i = 0; i < paragraphCount; i++) {// 遍历段落读取数据
Paragraph pp = range.getParagraph(i);
content.append(pp.text());
}
} catch (Exception e) {
}
return content.toString().trim();
}
2.PDF文件用的是PDFbox:
public static String readPdf(String path) throws Exception {
StringBuffer content = new StringBuffer("");// 文档内容
FileInputStream fis = new FileInputStream(path);
PDFParser p = new PDFParser(fis);
p.parse();
PDFTextStripper ts = new PDFTextStripper();
content.append(ts.getText(p.getPDDocument()));
fis.close();
return content.toString().trim();
}
3.html文件:
public static String readHtml(String urlString) {
StringBuffer content = new StringBuffer("");
File file = new File(urlString);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
// 读取页面
BufferedReader reader = new BufferedReader(new InputStreamReader(
fis,"utf-8"));//这里的字符编码要注意,要对上html头文件的一致,否则会出乱码
String line = null;
while ((line = reader.readLine()) != null) {
content.append(line + "\n");
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
String contentString = content.toString();
return contentString;
}
4.txt文件:
public static String readTxt(String path) {
StringBuffer content = new StringBuffer("");// 文档内容
try {
FileReader reader = new FileReader(path);
BufferedReader br = new BufferedReader(reader);
String s1 = null;
while ((s1 = br.readLine()) != null) {
content.append(s1 + "\r");
}
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString().trim();
}
接下来数搜索代码:
public class TestQuery {
public static void main(String[] args) throws IOException, ParseException {
Hits hits = null;
//搜索内容自己换
String queryString = "根据国务院的决定";
Query query = null;
IndexSearcher searcher = new IndexSearcher("d:\\index"); //这里注意索引存放的路径
Analyzer analyzer = new StandardAnalyzer();
try {
QueryParser qp = new QueryParser("body", analyzer);
/**
* 建索引的时候我们指定了body建立为内容,我们搜索的时候也是针对body的,所以
* QueryParser qp = new QueryParser("body", analyzer);
* 这句和建立索引时候
Field FieldBody = new Field("body", temp, Field.Store.YES,
Field.Index.TOKENIZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
*的这句的"body"是对应的。
*/
query = qp.parse(queryString);
} catch (ParseException e) {
System.out.println("异常");
}
if (searcher != null) {
hits = searcher.search(query);
if (hits.length() > 0) {
System.out.println("找到:" + hits.length() + " 个结果!");
for (int i = 0; i < hits.length(); i++) {//输出搜索信息
Document document = hits.doc(i);
System.out.println("contents:"+document.get("body"));
//同样原理这里的document.get("body")就是取得建立在索引文件里面的额body的所有内容
//你若想输出文件路径就用document.get("path")就可以了
}
} else{
System.out.println("0个结果!");
}
}
}
发表评论
-
微信收货地址共享接口-终极解决
2015-06-25 13:10 8304最近要接入微信的收货地址共享接口,总是不成功,折腾了好 ... -
Java中HashMap的实现原理
2011-04-28 14:30 2728昨天有人来公司面试,因为面试的地方和我坐的地方比较近,所以也听 ... -
java注解(annotation)简介
2010-06-13 10:10 1313[Java 5.0] Annotation – @Deprec ... -
quartz和spring-quartz
2010-06-13 10:03 873quartz和spring-quartz -
Java 线程实例讲解综述
2010-06-13 09:57 981Java 线程实例讲解综述 编写具有多线程能力的程序经常会用 ... -
Java Double 精度问题总结
2010-06-13 09:56 5248使用Java,double 进行运算时,经常出现精度丢失的问题 ... -
eXtremeComponents的eXtremeTable分页特性
2010-05-14 17:27 3281下面是我使用的例子: <ec:table ite ... -
java---final 关键字 和 static 用法
2010-03-17 13:58 840final 关键字 和 static 用法 一、final ... -
java版的escape和unescape方法
2010-03-17 09:21 2501其中unescape方法可以用来解开javascript的es ... -
StatSVN的使用说明
2010-03-04 10:27 999一、 checkout 希望统计的版本或者分支到某个目录(不管 ... -
Velocity语法
2010-03-01 18:01 8361. 变量 (1)变量的 ... -
用KeyTool生成安全证书
2010-02-22 17:14 1094详细请见:Tomcat的帮助文档,:https://local ... -
Spring 注解学习手札
2010-02-10 10:02 779http://snowolf.iteye.com/blog/5 ... -
JDK、JRE、JVM的关系
2010-01-25 11:23 853JDK就是Java Development Kit.简单的说J ... -
类装载器学习
2010-01-22 12:54 773Java的类装载器(Class Loader)和命名空间(Na ... -
Tomcat发布项目方法
2010-01-22 10:46 2566第一种方法:在tomcat中的conf目录中,在server. ... -
理解Java ClassLoader机制
2010-01-21 16:20 849当JVM(Java虚拟机)启动时,会形成由三个类加载器组成的初 ... -
cookie和session的工作机制
2010-01-19 15:19 763转载自:http://hi.baidu.com/jmtbai/ ... -
如何设置Tomcat的JVM虚拟机内存大小
2010-01-18 14:25 925Tomcat本身不能直接在计算机上运行,需要依赖于硬件基础之上 ... -
浅谈设置JVM内存分配的几个妙招
2010-01-18 14:24 1566安装Java开发软件时,默 ...
相关推荐
注:随光盘附带的mirror.rar文件是使用Heritrix从pconline网上抓取的手机信息页面(以网站镜象格式存储),在本书最后搜索引擎完整实例中需要使用到。由于光盘容量有限,笔者删除了原始镜像目录中一部分的网页,可能...
注:随光盘附带的mirror.rar文件是使用Heritrix从pconline网上抓取的手机信息页面(以网站镜象格式存储),在本书最后搜索引擎完整实例中需要使用到。由于光盘容量有限,笔者删除了原始镜像目录中一部分的网页,可能...
大名鼎鼎的Lucene 搜索引擎被广泛用于搜索领域,但是操作复杂繁琐,总是让开发者敬而远之。而 Elasticsearch将 Lucene 作为其核心来实现所有索引和搜索的功能,通过简单的 RESTful 语法来隐藏掉 Lucene 的复杂性,...
Lucene是一个信息检索的函数库(Library),利用它你可以为你...也就是说不管是MS word, Html ,pdf还是其他什么形式的文件只要你可以从中抽取出文字形式的内容就可以被Lucene所用.你就可以用Lucene对它们进行索引以及搜索.
注:随光盘附带的mirror.rar文件是使用Heritrix从pconline网上抓取的手机信息页面(以网站镜象格式存储),在本书最后搜索引擎完整实例中需要使用到。由于光盘容量有限,笔者删除了原始镜像目录中一部分的网页,可能...
自己动手写搜索引擎 1 第1章 了解搜索引擎 1 1.1 Google神话 1 1.2 体验搜索引擎 1 1.3 你也可以做搜索引擎 4 1.4 本章小结 4 第2章 遍历搜索引擎技术 5 2.1 30分钟实现的搜索引擎 5 2.1.1 准备工作环境(10分钟) 5...
该书详细讲解了搜索引擎与信息检索基础,Lucene入门实例,Lucene索引的建立,使用Lucene进行搜索,排序,过滤和分页,Lucene的分析器,对Word、Excel和PDF格式文档的处理,Compass搜索引擎框架,Lucene分布式和...
What’s Lucene Lucene是一个信息检索的函数库(Library),...也就是说不管是MS word, Html ,pdf还是其他什么形式的文件只要你可以从中抽取出文字形式的内容就可以被Lucene所用.你就可以用Lucene对它们进行索引以及搜索.
本例使用了lucene6.4.0与lire,可对各文档(word\excel\pdf\txt)中关键字进行检索,并调用tess4j通过图片内容识别,对图片中文字进行检索。ImageCreatIndex、ImageSearch分别建立图片索引与检索功能,为以图搜图实例 ...
进行了优化,并且提供了一个完善的功能管理界面,是一款非常优秀的全文搜索引擎。 Solr 可以高亮显示搜索结果,通过索引复制来提高可用,性,提供一套强大 Data Schema 来定义字段,类型和设置文本分析,提供基于 ...
我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的首选。他可以快速的存储、搜索和分析海量数据。Spring Boot通过整合Spring Data ElasticSearch为我们提供了非常便捷的检索功能支持; ...
用JAVA开发的一个小型的目录监视系统,系统会每5秒自动扫描一次需要监视的目录,可以用来监视目录中文件大小及文件增减数目的变化。 Java日期选择控件完整源代码 14个目标文件 内容索引:JAVA源码,系统相关,日历,...
Solr是一个独立的企业级搜索应用服务器,基于Lucene。...用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引;也可以通过Http Get操作提出查找请求,并得到XML格式的返回结果。
用JAVA开发的一个小型的目录监视系统,系统会每5秒自动扫描一次需要监视的目录,可以用来监视目录中文件大小及文件增减数目的变化。 Java日期选择控件完整源代码 14个目标文件 内容索引:JAVA源码,系统相关,日历,...
AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...
AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...
AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...
AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...
AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...