- 浏览: 194839 次
文章分类
最新评论
-
code_xiaoke:
session可是有30分钟有效期的还有如果在分布式的环境下 ...
Java Web 用户登陆示例代码 -
xul0038:
http://www.baidu.com
Java Web 用户登陆示例代码 -
16866:
非常棒,配置信息呢
Nginx负载均衡 -
开发小菜:
什么意思,没明白?能不能写一个例子
JS 实现DIV随浏览器窗口大小变化
因为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;
}
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;
}
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打分规则与Similarity模块详解
2013-02-06 14:08 1128搜索排序结果的控制 Lu ... -
Compass将lucene、Spring、Hibernate三者结合
2013-02-01 11:02 1639版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声 ... -
Lucene3.0详解
2013-02-01 10:57 1339★第一部分:概述 1. 我 ... -
Lucene为不同字段指定不同分词器(转)
2013-01-31 17:34 3370在lucene使用过程中,如 ... -
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎
2012-11-19 09:55 1337前两天看到了一个中国新闻网,这个网站的搜索form的actio ... -
Lucene多字段搜索
2012-11-19 09:53 927最近在学习Lucene的过程中遇到了需要多域搜索并排序的问题, ... -
lucene之sort
2012-11-16 15:06 1041package cn.zqh.lucene.sort; im ... -
Lucene相关度排序的调整
2012-11-16 11:38 1675Lucene的搜索结果默认按 ... -
使用solr搭建你的全文检索
2012-09-28 16:34 989Solr 是一个可供企业使用的、基于 Lucene 的开箱即用 ... -
《Lucene in action》中推荐的Lucene分页方式
2012-09-28 16:33 820在《Lucene in action》 中专门讨论了如何对Hi ... -
lucene3搜索引擎,索引建立搜索排序分页高亮显示, IKAnalyzer分词
2013-01-27 19:17 1703package com.zjr.service.impl; ... -
用MultiSearcher在多索引中进行搜索
2013-07-09 09:45 935/** * 多索引进行查询 */ ... -
lucene 中一个Filed中包含多值的范围查询
2013-07-09 09:45 1282package tools.lucene; ... -
使用lucene PrefixQuery 根据拼音前缀查询
2012-07-17 10:52 1282PrefixQuery类似于 数据库中的 like 'a%'查 ... -
lucene 中一个Filed中包含多值的范围查询
2012-07-17 10:50 981package tools.lucene; ... -
solr部署
2012-07-17 10:41 9791. 认识到solr是作为一个webapp角色; 2. ... -
Solr快速教程
2012-07-17 10:38 1114Solr快速教程 加载及配置步骤: 1.下载最新版本的Solr ... -
PrefixQuery前缀搜索符合的所有放入map
2012-07-17 10:37 1172/** PrefixQuery前缀搜索符合的所有放入map ... -
(转载)Lucene几种中文分词的总结
2012-04-13 14:40 1806Lucene几种中文分词的总结 博客分类: ... -
数据挖掘网址共享
2012-04-13 14:37 1400数据挖掘网络应用搜索引擎企业应用Lotus [/color]知 ...
相关推荐
注:随光盘附带的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是...