- 浏览: 380148 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (215)
- ubuntu (27)
- 虚拟机 (13)
- 数据库 (29)
- JAVA (40)
- 搜索 (23)
- 开发工具 (2)
- 产品 (2)
- 工具 (1)
- 应用服务器 (5)
- linux (17)
- log (1)
- 多线程 (3)
- 异常 (1)
- shell (6)
- 12306 ;互联网 (1)
- 持续集成&Hudson (4)
- js (1)
- JAVA OR-Map (1)
- 漏洞 (1)
- svn (1)
- MAVEN (3)
- 架构 (1)
- windows (1)
- json (1)
- web (3)
- jenkins (2)
- iptables (2)
- JAVA ssh (0)
- 项目管理 (1)
- tomcat (1)
- 安全 (1)
- 数据库 mysql (1)
- 性能 (1)
最新评论
-
sbwfgihc:
怎么用的啊,
<转>mysql 树查询语句 -
panghaoyu:
实现一个智能提示功能需要ajax、数据库、jsp/php、算法 ...
<转>Solr Suggest实现搜索智能提示 -
songsove:
请问您qq多少
solr 对拼音搜索和拼音首字母搜索的支持 -
panghaoyu:
实现一个智能提示功能需要ajax、数据库、jsp/php、算法 ...
<转>Solr Suggest实现搜索智能提示 -
norain1988:
这样就可以实现相关搜索了吗
solr 百度相关搜索类似功能的实现
因为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个结果!");
}
}
}
发表评论
-
百度GOOGLE的用户鼠标行为分析<转>
2012-08-29 09:37 1484转自:http://www.20ju.com/conte ... -
lucene 一些性能调优建议
2012-08-22 16:12 5533lucene 排序 Lucene默认按照相关度 ... -
<转>庖丁解牛分词之自定义词库[自定义词典]
2012-07-03 10:21 893原链接: http://hi.baidu.com/stone_ ... -
庖丁(Paoding)分词的词典原理解析
2012-07-03 10:14 3635Paoding分词过程中词典 ... -
solr 百度相关搜索类似功能的实现
2012-06-27 16:26 5736这个功能的实现有几种不同的思路。 大概半个月前,我在网 ... -
solr 索引库同步数据库
2012-06-27 14:01 5618对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输 ... -
solr 对拼音搜索和拼音首字母搜索的支持
2012-06-27 13:52 9049对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输 ... -
<转>Solr Suggest实现搜索智能提示
2012-06-27 10:15 13839智能提示简介 搜索的智能提示目前是各大搜索的标配应用, ... -
IK的整个分词处理过程
2012-06-26 14:25 11552IK的整个分词处理过 ... -
Solr的检索运算符
2012-06-21 14:47 4212“:” 指定字段查指定值,如返回所有值*:*² “?” 表示 ... -
Solr 学习(5) —- Solr查询语法和参数
2012-06-21 14:25 11172、查询语法 solr的一些查询语法 ... -
Apache Solr schema.xml及solrconfig.xml文件中文注解
2012-06-21 10:55 1160一、字段配置(schema)schema.xml位于so ... -
<转>LUCENE3.0 自学吧 4 TermDocs
2012-06-07 22:13 1205利用TermDocs也是可以得到一些有用的结果。。。代码 ... -
<转>Lucene里经常被聊到的几个话题
2012-06-07 21:54 1232lvpei.cnblogs.com.自己总结的Lucen ... -
<转>浅谈文档索引普遍原理<笔记>
2012-06-07 21:49 998--lvpei.cnblogs.com 搜索引擎普遍采 ... -
Lucene中Field类很重要
2012-06-07 21:46 8222org.apache.lucene.demo.Index ... -
Lucene架构简单分析
2012-06-06 15:30 1634Lucene简介Lucene 是一个基 ... -
<转>lucene3.0中Field.Index, Field.Store,Field.TermVector详解
2012-06-06 14:49 890lucene在doc.add(new Field(&quo ... -
<转>用于Lucene的各中文分词比较
2012-06-05 09:58 5554对几种中文分析器,从分词准确性和效率两方面进行比较。分析器依次 ... -
Lucene3参考资料
2012-06-05 09:46 963http://wenku.baidu.com/view/706 ...
相关推荐
注:随光盘附带的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网上抓取的手机信息页面(以网站镜象格式存储),在本书最后搜索引擎完整实例中需要使用到。由于光盘容量有限,笔者删除了原始镜像目录中一部分的网页,可能...
该书详细讲解了搜索引擎与信息检索基础,Lucene入门实例,Lucene索引的建立,使用Lucene进行搜索,排序,过滤和分页,Lucene的分析器,对Word、Excel和PDF格式文档的处理,Compass搜索引擎框架,Lucene分布式和...
What’s Lucene Lucene是一个信息检索的函数库(Library),...也就是说不管是MS word, Html ,pdf还是其他什么形式的文件只要你可以从中抽取出文字形式的内容就可以被Lucene所用.你就可以用Lucene对它们进行索引以及搜索.
自己动手写搜索引擎 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...
本例使用了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是...