lucene入门-索引网页
2009-12-23 19:21
302 查看
package bindex;
import java.io.File;
import tool.FileText;
import java.io.IOException;
import org.apache.lucene.document.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;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
public class FileIndexer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String indexPath ="indexes";
try {
IndexWriter indexWriter = new IndexWriter(indexPath,new StandardAnalyzer());
Document doc=new Document();
//第一个文档
File f=new File("htmls/hao123.htm");
String name=f.getName();
Field field=new Field("name",name,Field.Store.YES,Field.Index.TOKENIZED);
doc.add(field);
String content=FileText.getText(f);
field=new Field("conent",content,Field.Store.YES,Field.Index.TOKENIZED);
doc.add(field);
String path=f.getPath();
field=new Field("path",path,Field.Store.YES,Field.Index.NO);
doc.add(field);
indexWriter.addDocument(doc);
//第二个文档
f=new File("htmls/home.htm");
name=f.getName();
field=new Field("name",name,Field.Store.YES,Field.Index.TOKENIZED);
doc.add(field);
content=FileText.getText(f);
field=new Field("conent",content,Field.Store.YES,Field.Index.TOKENIZED);
doc.add(field);
path=f.getPath();
field=new Field("path",path,Field.Store.YES,Field.Index.NO);
doc.add(field);
indexWriter.addDocument(doc);
indexWriter.close();
System.out.println("OK!");
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package tool;
import java.io.*;
public class FileText {
/**
* @param args
*/
public static String getText(File f){
StringBuffer sb=new StringBuffer("");
try{
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
String s=br.readLine();
while(s!=null){
sb.append(s);
s=br.readLine();
}
br.close();
}
catch (Exception e){
sb.append("");
}
return sb.toString();
}
public static String getText(String s){
String t="";
try{
File f=new File(s);
t=getText(f);
}
catch (Exception e){
t="";
}
return t;
}
}
相关文章推荐
- lucene入门-索引网页
- lucene3.0入门(索引的文件写入+索引关键词查找)
- Lucene入门程序
- 【Lucene3.6.2入门系列】第04节_中文分词器
- 【Lucene3.6.2入门系列】第12节_近实时搜索
- lucene入门实例
- lucene中查询索引库代码入门
- Lucene的学习第三篇——入门代码
- Lucene入门与使用
- Lucene学习(一):入门
- Lucene入门初步
- lucene入门篇
- lucene3.6.0 经典案例 入门教程
- Lucene的入门例子
- Lucene入门教程(转载)
- 【Lucene3.6.2入门系列】第04节_中文分词器
- 【Lucene3.6.2入门系列】第15节_SolrJ高亮
- Lucene之入门案例-yellowcong
- Lucene学习(入门)
- Lucene入门和使用