《尚学堂科技张志宇lucene构建一个简单的WEB搜索程序》由会员分享,可在线阅读,更多相关《尚学堂科技张志宇lucene构建一个简单的WEB搜索程序(150页珍藏版)》请在金锄头文库上搜索。
1、Lucene_构建一个简单的WEB搜索程序lucene2.3.2tomcat6.0.16je-analysis1.4.0lukeall0.7.1Mysql jdbc driver3.1.13Tidy04aug2000r7MyEclipse6.0M1_E3.3l 项目周期n 3-4天l 目标n Lucene入门u 全文检索的概念,倒排索引的概念u 建立索引u 搜索u 中文分词的实现n Nutch入门n 串知识点Html,css,javascript,servlet,jsp,mysql,n 介绍MVC的概念n 演示借用一些javascript的成熟的框架实现页面的特殊效果。例如:ricon 学会使
2、用myeclipsen 熟悉mysql数据库的用法l 什么时候用lucenen 数据库大量数据,文本字段内容很多n 非结构化文档1. 安装myeclipse l 建立工程web projectn 工程名称lucenel 如何配置tomcat服务器n 好处自动部署n Windowshow viewserversl 如何部署web appn Deploy按钮,添加tomcat项目l Web browser窗口n 最好不用此browsern Show viewweb browserl 引入jar包Lucene工程文件夹下,建立lib目录,拷贝如下jar包到lib目录n lucene-core-2.2
3、.0.jarn Tidy.jarn lucene-2.2.0lucene-2.2.0contribanalyzerslucene-analyzers-2.2.0.jarn je-analysis-1.4.0.jarn mysql-connector-java-3.1.13-bin.jarl 显示line numberl Alt/自动完成快捷键效果出不来l .快捷键效果出不来2. 为一个文件建立索引(英文)确认已经引入包lucene-core-2.2.0.jarField.Store.YES和Field.Store.NO区别l termVector是Lucene 1.4.3新增的它提供一种向量机
4、制来进行模糊查询,很少用。l DateTools.timeToStringIndexHTML.javaimport java.io.File;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;public class IndexHTML static String index
5、= D:share05_Servlet_JSPtomcatapache-tomcat-5.5.17index;static String root = D:sharelucenesoftlucene-2.2.0lucene-2.2.0docsapiindex.html;public static void main(String args )throws ExceptionIndexWriter writer = new IndexWriter(index,new StandardAnalyzer(),true);Document doc = new Document();File f = n
6、ew File(root);doc.add(new Field (path,f.getPath(),Field.Store.YES,Field.Index.UN_TOKENIZED);doc.add(new Field (content,我们是共产主义接班人,Field.Store.NO,Field.Index.TOKENIZED);writer.addDocument(doc);writer.optimize();writer.close();3. 如何确认索引已经正确建立?java -jar lukeall-0.7.1.jar4. tomcat配置l WEB-INFlibn lucene-
7、core-2.2.0.jarn je-analysis-1.4.0.jarl 确保8080端口可用l reloadablen C:tomcatconfcontext.xml5. 为一个文件建立索引(递归)import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.doc
8、ument.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.store.LockObtainFailedException;public class IndexHTML1 static IndexWriter writer;public static void main(String args) throw
9、s Exception String root = D:share01_J2SEsofthtml_zh_CNhtmlzh_CNapijavalang;String index = D:sharetoolsapache-tomcat-6.0.14apache-tomcat-6.0.14index_cn;writer = new IndexWriter(index,new StandardAnalyzer(),true);File f = new File(root);indexDocs(f);writer.optimize();writer.close();private static void
10、 indexDocs(File f) throws Exception if(f.isDirectory()File subs = f.listFiles();for (int i = 0; i subs.length; i+) indexDocs(subsi);elseindexDoc(f);private static void indexDoc(File f) throws Exception System.out.println(f.getPath();Document doc = new Document();doc.add(new Field(path,f.getPath(),Fi
11、eld.Store.YES,Field.Index.UN_TOKENIZED);doc.add(new Field(content,new FileReader(f);writer.addDocument(doc);6. 为一个文件建立索引(使用Tidy)l 确认已经引入包Tidy.jarl 确认已经引入包je-analysis-1.4.0.jarimport java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.Input
12、StreamReader;import java.io.Reader;import java.text.DecimalFormat;import jeasy.analysis.MMAnalyzer;import org.apache.lucene.document.DateTools;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.w3c.dom.Element;impo
13、rt org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Text;import org.w3c.tidy.Tidy;public class IndexHTMLTidy / 索引建立到那个目录static String index = C:tomcatindex_cn;/ 英文内容/ static String root =/ G:lessonslucenestudentsoftlucene-2.2.0lucene-2.2.0docsapiindex.html;/ 中文内容,java.lang下面的内容即可stati
14、c String root = E:appdevelopjavaapihtml_zh_CNhtmlzh_CNapijavalang;static Document doc = null;static IndexWriter writer = null;public static void main(String args) throws Exception writer = new IndexWriter(index, new MMAnalyzer(), true);File f = new File(root);indexDocs(f);writer.addDocument(doc);writer.optimize();writer.close();System.out.println(ok.);public static void indexDocs(File f) throws