`
deepfuture
  • 浏览: 4335277 次
  • 性别: Icon_minigender_1
  • 来自: 湛江
博客专栏
073ec2a9-85b7-3ebf-a3bb-c6361e6c6f64
SQLite源码剖析
浏览量:79441
1591c4b8-62f1-3d3e-9551-25c77465da96
WIN32汇编语言学习应用...
浏览量:68424
F5390db6-59dd-338f-ba18-4e93943ff06a
神奇的perl
浏览量:101548
Dac44363-8a80-3836-99aa-f7b7780fa6e2
lucene等搜索引擎解析...
浏览量:281311
Ec49a563-4109-3c69-9c83-8f6d068ba113
深入lucene3.5源码...
浏览量:14621
9b99bfc2-19c2-3346-9100-7f8879c731ce
VB.NET并行与分布式编...
浏览量:65625
B1db2af3-06b3-35bb-ac08-59ff2d1324b4
silverlight 5...
浏览量:31341
4a56b548-ab3d-35af-a984-e0781d142c23
算法下午茶系列
浏览量:45238
社区版块
存档分类
最新评论

lucene入门-索引网页

阅读更多

 

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;
}
}

0
0
分享到:
评论
1 楼 di1984HIT 2015-01-07  
写的很好~

相关推荐

Global site tag (gtag.js) - Google Analytics