为一个文件建立索引-Read.DOCVIP

  • 6
  • 0
  • 约3.28千字
  • 约 4页
  • 2018-11-16 发布于天津
  • 举报
为一个文件建立索引-Read

Lucene_创建索引_简单搜索 作者 尚学堂科技:张志宇 lucene-版本 2.2.0 日期 2007-1 tomcat下建立一个web工程文件夹 webapp名称:myluceneweb /WEB-INF/web.xml ?xml version=1.0 encoding=ISO-8859-1? web-app xmlns=/xml/ns/j2ee xmlns:xsi=/2001/XMLSchema-instance xsi:schemaLocation=/xml/ns/j2ee /xml/ns/j2ee/web-app_2_4.xsd version=2.4 /web-app /WEB-INF\classes \WEB-INF\lib\lucene-core-2.2.0.jar \lucene-2.2.0\lucene-2.2.0\src\demo\下面的org文件夹拷贝C:\tomcat\webapps\myluceneweb\WEB-INF\classes下面 并删除掉org\apache\lucene\demo下的类 建立一个工程 名称:MyLucene 指定:source folder 指定:output folder 指定:jdk 指定:lucene的jar包 为一个文件建立索引 略 为一个文件夹的内容建立索引(递归) IndexHTML.java import java.io.File; import java.io.FileInputStream; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.demo.html.HTMLParser; import org.apache.lucene.document.DateTools; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; public class IndexHTML { static String index = C:\\tomcat\\webapps\\index; static String root = G:\\lessons\\lucene\\student\\soft\\lucene-2.2.0\\lucene-2.2.0\\docs\\api; static Document doc = null; static IndexWriter writer = null; public static void main(String[] args) throws Exception { writer = new IndexWriter(index,new StandardAnalyzer(),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 Exception{ if(f.isDirectory()){ String file[] = f.list(); for (int i = 0; i file.length; i++) { indexDocs(new File(f,file[i])); } }else if(f.getName().endsWith(.html)){ indexDoc(f); } } public static void indexDoc(File f) throws Exception{ doc = new Document(); System.out.println(f.getPath()); HTMLParser parser = new HTMLParser(new FileInputStream(f)); doc.add(new Field(path,f.getPath(),Field.Store.YES,Field.Index.NO)); doc.add(new Field(lastmodified,DateTools.timeToString(f

文档评论(0)

1亿VIP精品文档

相关文档