为什么我创建的索引总为空?郁闷
来源:互联网 发布:apache ant jdk1.7 编辑:程序博客网 时间:2024/04/29 16:31
/*
*michzel new java files
*
*Created on 2010-9-15
*
*Copyright 2010 Anchora info company. all rights reserved
*/
package LuceneTest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.demo.IndexFiles;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.store.*;
import org.apache.lucene.util.Version;
import org.apache.lucene.store.LockFactory;
@SuppressWarnings("unused")
public class IndexPerf {
public static final String INDEX_STORE_PATH= "d://test";
public static final int DEFAULT_MAX_DOC_IN_RAM=64;
public static final int DEFAULT_MAX_FIELD_LENGTH = Integer.MAX_VALUE;
private static IndexPerf indexer= null;
private IndexWriter ramWriter= null;
private IndexWriter fsWriter= null;
private int docs_in_ram;
private int ramMaxFile= DEFAULT_MAX_DOC_IN_RAM;
private boolean refreshed= true;
FSDirectory fsdir= null;
RAMDirectory ramdir= null;
//静态方法构造一个单例
public static IndexPerf getInstance() throws IOException{
if(indexer==null){
indexer= new IndexPerf();
}
return indexer;
}
//构造索引器
public void newWriter(){
if(fsWriter != null || ramWriter != null){
System.out.println("close the indexer first then to creat a new one");
return;
}
try{
File file = new File(INDEX_STORE_PATH);
fsdir= FSDirectory.open(file);
ramdir= new RAMDirectory();
StandardAnalyzer anlyzer= new StandardAnalyzer(Version.LUCENE_30);
ramWriter= new IndexWriter(ramdir, anlyzer,
true,IndexWriter.MaxFieldLength.UNLIMITED);
ramWriter.setInfoStream(System.out);
fsWriter= new IndexWriter(fsdir,anlyzer,
true,IndexWriter.MaxFieldLength.UNLIMITED);
initWriter();
}catch (IOException e){
e.printStackTrace();
}
}
//初始化索引器
private void initWriter() throws IOException{
fsWriter.setMaxFieldLength(DEFAULT_MAX_FIELD_LENGTH);
}
//优化索引
public void optimizeIndex() throws IOException{
if(fsWriter == null || ramWriter == null){
System.out.println("use newWriter() method to initialize a new Indexer first.");
return ;
}
//refreshRam();
//fsWriter.optimize();
}
//索引某个目录下的文件
public void toIndex(String path) throws IOException{
toIndex(new File(path));
}
//索引某个对象
public void toIndex(File file) throws IOException{
refreshed = false;
Date start = new Date();
int number= indexFiles(file);
Date end= new Date();
System.out.println("总共耗时"+(end.getTime()-start.getTime())+"毫秒");
System.out.println("一共为"+number+"个文件建立索引");
}
//关闭索引器
public void close() throws IOException{
//refreshRam();
ramWriter.close(true);
fsWriter.addIndexesNoOptimize(new Directory[]{ramdir});
fsWriter.close(true);
}
//刷新内存
private void refreshRam() throws IOException{
if(!refreshed){
System.out.println("Refreshing");
fsWriter.addIndexesNoOptimize(new Directory[]{ramdir});
ramdir.close();
ramdir = new RAMDirectory();
ramWriter= new IndexWriter(fsdir,new
StandardAnalyzer(Version.LUCENE_30), true,IndexWriter.MaxFieldLength.UNLIMITED);
docs_in_ram=0;
refreshed= true;
}
}
//向索引中加入文档
private void addDocument(File file) throws IOException{
ramWriter.addDocument(MyDocument.getDocument(file));
docs_in_ram++;
System.out.println("向索引中加入文档");
//refreshed= false;
}
//递归遍历文件目录来建立索引
private int indexFiles(File file) throws IOException{
if(file.isDirectory()){
File[] files= file.listFiles();
int num= 0;
for(int i= 0; i<files.length;i++){
num+= indexFiles(files[i]);
System.out.println(+i);
}
return num;
}else{
if(file.getPath().endsWith(".txt")){
System.out.println("正在建索:"+file.getCanonicalPath());
addDocument(file);
return 1;
}
else{
System.out.println("文件类型不支持"+file);
return 0;
}
}
}
public static void main(String[] args)throws IOException{
IndexPerf indexer= IndexPerf.getInstance();
indexer.newWriter();
indexer.toIndex("d://data");
indexer.close();
}
}
- 为什么我创建的索引总为空?郁闷
- 多文档 GetActiveView 为什么总为空
- 为什么查询结果总为空?
- 为什么我的课件总也看不完。。。郁闷中。。。
- 为什么我的目标总无法完成?
- 为什么不用我的索引
- 郁闷..我的blog版本为什么还是Beta4...
- 我为我的结课项目郁闷
- 一个窗口的window.parent属性总不为空
- 为什么总抛出空指针异常,请各位大师帮我改进一下,我将不胜感激
- 使用GPS定位为什么location总为空 而且onLocationChanged()方法没调用呀
- 为什么ViewState["value"]的取值会为空?
- Content后台的获取为什么为空
- 为什么我录制flight以后脚本为空呢?
- Lucene创建空索引
- HttpFileCollection为什么为空?
- 为什么我总活在别人的影子里
- 为什么Oracle不使用我的索引?
- unity3d和网页数据交互的基本原理
- jsp 页面点击事件后弹出DIV层代码
- ORACLE 高效的批量处理
- 把JSP包在DIV中
- 思想啊思想
- 为什么我创建的索引总为空?郁闷
- jsp页面弹出div样式
- Android project 的常用编译方法
- 初学Java 写的一个简陋的Swing Demo
- Intent Filter匹配
- 项目管理的知识点
- [VB.Net]文件拖放示例
- java ftpclient问题
- ACL/CreateMapping/Kernel Object Namespace