lucene搜索引擎配置续
来源:互联网 发布:网络捕鱼技巧打法 编辑:程序博客网 时间:2024/03/29 03:14
使用lucene索引HTML
lucene索引HTML原理与索引text类似,也是通过新建document对象,然后将HTML的各种信息比如title、content等信息以不同的字段保存到document对象中,具体过程介绍如下:
1、设置IndexWriter对象,及其索引文件保存目录、分词器等信息
2、读取HTML文件相应信息,提取文件的各个信息字段,将HTML中的信息一各种方式提取出来,本人采用正则表达式来提取。
3、将提取的信息写入到document对象中去
4、最后还是别忘了关闭IndexWriter对象
参考代码如下:
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.index.IndexWriter;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.text.DateFormat;
- import java.util.Date;
- import jeasy.analysis.MMAnalyzer;
- //还需调用demo的其他类。
- ;
- public class IndexHTML {
- public static void main(String[] args) throws ClassNotFoundException,
- IOException
- {
- try {
- Date start = new Date();
- IndexWriter writer = new IndexWriter("D://luceneIndex",
- new MMAnalyzer(), true); // 索引保存目录,必须存在
- indexDocs(writer, new File("D://luceneData")); // HTML 文件保存目录
- System.out.println("Optimizing ....");
- writer.optimize();
- writer.close();
- Date end = new Date();
- System.out.print("Total time: ");
- System.out.println(end.getTime() - start.getTime());
- } catch (Exception e) {
- System.out.println("Class " + e.getClass()
- + " throws error!/n errmsg: " + e.getMessage());
- } // end try
- } // end main
- public static void indexDocs(IndexWriter writer, File file)
- throws Exception
- {
- if (file.isDirectory()) {
- String[] files = file.list();
- for (int i = 0; i < files.length; i++) {//假如文件是文件夹则递归调用方法
- indexDocs(writer, new File(file, files[i]));
- } // end for
- } else if (file.getPath().endsWith(".htm") || file.getPath().endsWith(".html") || file.getPath().endsWith(".shtml")) { // 只对 HTML 文件做索引
- System.out.print("Add file:" + file + " ....");
- // Add html file ....
- Document doc = new Document();
- doc.add(new Field("file", file.getName(),Field.Store.YES,Field.Index.TOKENIZED)); // 索引文件名
- doc.add(new Field("modified", DateFormat.getDateTimeInstance().format(new Date(file.lastModified())),Field.Store.YES,Field.Index.TOKENIZED)); // 索引最后修改时间
- String content = "";
- FileReader fReader = new FileReader(file);//读取文件
- BufferedReader bReader = new BufferedReader(fReader);
- String line = bReader.readLine();
- while (line != null) {
- String readline = new String(line.getBytes(),"UTF-8");//读取文件后转换字符编码
- content += readline;
- // 截取 HTML 标题 <title>
- line = bReader.readLine();
- } // end while
- bReader.close();
- fReader.close();
- String tLowContentStr = content.toLowerCase();
- //提取内容忽略script
- int tScriptStartInt = tLowContentStr.indexOf("<script");//第一次出现script的位置
- while(tScriptStartInt != -1){
- int tScriptEndInt = tLowContentStr.indexOf("</script>");
- String tStartStr = content.substring(0, tScriptStartInt);
- String tEndStr = content.substring(tScriptEndInt + 9, content.length());
- content = tStartStr + tEndStr;
- //截取字符串
- tLowContentStr = content.toLowerCase();
- tScriptStartInt = tLowContentStr.indexOf("<script");
- }
- tLowContentStr = content.toLowerCase();
- //读取内容忽略style内容
- tScriptStartInt = tLowContentStr.indexOf("<style");//第一次出现style的位置
- while(tScriptStartInt != -1){
- int tScriptEndInt = tLowContentStr.indexOf("</style>");
- String tStartStr = content.substring(0, tScriptStartInt);
- String tEndStr = content.substring(tScriptEndInt + 8, content.length());
- content = tStartStr + tEndStr;
- //截取字符串
- tLowContentStr = content.toLowerCase();
- tScriptStartInt = tLowContentStr.indexOf("<style");
- }
- int tTitleStartInt = tLowContentStr.indexOf("<title");
- int tTitleEndInt = tLowContentStr.indexOf("</title>");
- String tTitleStr = tLowContentStr.substring(tTitleStartInt + 7 , tTitleEndInt);
- doc.add(new Field("title",tTitleStr,Field.Store.YES,Field.Index.TOKENIZED));
- content = content.replaceAll(" {1,}","");
- content = content.replaceAll(" {1,}", " ");
- content = content.replaceAll("/t{1,}", " ");
- content = content.replaceAll(">{1,}", "");
- content = content.replaceAll("©{1,}", "");
- doc.add(new Field("contents", content.replaceAll("<[^<>]+>", ""),Field.Store.YES,Field.Index.TOKENIZED)); // 索引内容
- writer.addDocument(doc);
- /*Document document = HTMLDocument.Document(file);
- writer.addDocument(document);//使用系统HTML解析*/
- System.out.println(" [OK]");
- } // end if
- }
- }
代码说明:
在读取HTML时因为分词器的原因,有些分词器不支持UTF-8的字符集,因此在读取文件之后,有必要将以前以UTF-8编码的文件以UTF-8的方式读取,否则分词器无法对文件的字符串建立正确索引。如:String readline = new String(line.getBytes(),"UTF-8");
关于如何获取读取字符串到底是GBK还是UTF-8的问题,我们可以获取刚才读取字符的Unicode编码,假如Unicode编码是属于GBK的那那就是GBK。关于有时要提取URL 的正则表达式这里给一个比较通用的正则表达式:
http(s)?://([//w-]+//.)+[//w-[:[//d]]]+(/[//w- ./?%@&=+#]*)?
5、查找,关于查找与以前查找一样也是通过新建查找对象,然后通过查找对象查找结果,参考代码如下:
- Searcher searcher = new IndexSearcher(indexFilePath);
- Analyzer analyzer = new MMAnalyzer();
- QueryParser queryParser = new QueryParser("contents", analyzer);//新建查询对象
- Query query = queryParser.parse(line);
- System.out.println("Searching for: " + query.toString("contents"));
- this.hits = searcher.search(query);
- if (this.hits.length() == 0)
- System.out.println(this.hits.length() + " total matching documents");
- for (int i = 0; i < hits.length(); i++) {
- Document doc = this.hits.doc(i);
- System.out.println(doc.get("file"));
- System.out.println(doc.get("title"));
- System.out.println(doc.get("modified"));
- System.out.println(hits.score(i));
- }
- searcher.close();
6、对数据库建立索引
对于数据库建立索引,只需要读取要建立索引的表的记录,然后将记录中各个字段保存到document中,最后将document字段通过IndexWriter写入到索引中去。这里不再赘述。
- lucene搜索引擎配置续
- lucene搜索引擎配置
- lucene搜索引擎配置
- lucene搜索引擎
- 搜索引擎lucene
- Lucene搜索引擎
- lucene搜索引擎
- 搜索引擎lucene
- Lucene搜索引擎
- Lucene构建搜索引擎
- ajax+Lucene构建搜索引擎
- 关于搜索引擎lucene
- Lucene txt搜索引擎
- 搜索引擎lucene点滴
- Java搜索引擎 Lucene
- Lucene 搜索引擎( ing..)
- WebGIS搜索引擎之Lucene
- Lucene 搜索引擎学习
- 关于responeXML的返回值
- JRTPLIB (v2.9) - 指南
- 苦难是对素质的检验
- 服务器RAID 0+1(10) 硬盘阵列组建图解
- Struts 2 + Spring 2 + JPA + AJAX
- lucene搜索引擎配置续
- 软RAID的配置实例
- 同步变异步的方法
- 五十条经典的爱情观 (转)
- 兰州之行—刺激
- 不用url rewrite实现ASP伪静态技术
- arm-linux-gcc 4.1.1
- 右键复制自动加上网址信息
- Timer同时执行多个定时任务的例子