在创建索引的时候,
Field.Store,表示对域的存储
Field.Store.YES:存储字段值(未分词前的字段值)
Field.Store.NO:不存储,存储与索引没有关系
Field.Store.COMPRESS:压缩存储,用于长文本或二进制,但性能受损
Field.Index,表示对域的搜索
Field.Index.ANALYZED:分词建索引
Field.Index.ANALYZED_NO_NORMS:分词建索引,但是Field的值不像通常那样
被保 存,而是只取一个byte,这样节约存储空间
Field.Index.NOT_ANALYZED:不分词且索引
Field.Index.NOT_ANALYZED_NO_NORMS:不分词建索引,Field的值去一个byte保存
TermVector表示文档的条目(由一个Document和Field定位)和它们在当前文档中所出现的次数
Field.TermVector.YES:为每个文档(Document)存储该字段的TermVector
Field.TermVector.NO:不存储TermVector
Field.TermVector.WITH_POSITIONS:存储位置
Field.TermVector.WITH_OFFSETS:存储偏移量
Field.TermVector.WITH_POSITIONS_OFFSETS:存储位置和偏移量
创建索引的简单示例:
/**
* 创建索引(Indexing),生成索引文件
*/
private static void createTextIndex() {
// public final static String INDEX_SOURCE_DIR = "D:\\tomcat-7.0.22";// 要检索的文件夹
//
// public final static String INDEX_STORE_DIR = "d:\\fileIndex";// 索引文件的存放位置
//获取要索引的文件
File baseDir = new File(INDEX_SOURCE_DIR);
List<File> subFiles = new ArrayList<File>();
if (!baseDir.exists()) {
System.out.println("不存在" + INDEX_SOURCE_DIR + "目录");
System.exit(1);
} else if (baseDir.isDirectory()) {
subFiles = FileUtils.searchFilesByType(baseDir, subFiles, ".html");
}
//创建存放索引文件的目录
File indexDir = new File(INDEX_STORE_DIR);
if (!indexDir.exists()) {
indexDir.mkdir();
}
//索引分析器
Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
//创建索引器(核心)
IndexWriter indexWriter = null;
long startTime = 0;
try {
indexWriter = new IndexWriter(FSDirectory.open(indexDir),luceneAnalyzer, true, IndexWriter.MaxFieldLength.LIMITED);
indexWriter.setMaxMergeDocs(5);
indexWriter.setMergeFactor(5);
//不建立复合式索引文件,默认的情况下是复合式的索引文件
indexWriter.setUseCompoundFile(false);
startTime = new Date().getTime();
// 增加document到索引去
for (int i = 0; i < subFiles.size(); i++) {
if(subFiles.get(i).getName().indexOf(".")<=0){
continue;
}
System.out.println(new Date()+" File " + subFiles.get(i).getCanonicalPath()+ " 正在被索引....");
// 将txt 文件写到 Document中
Document document = new Document();
String fileName=subFiles.get(i).getName();//.substring(0,subFiles.get(i).getName().indexOf("."));
Field fieldName = new Field("fileName",fileName,Field.Store.NO,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(fieldName);
String filePath=subFiles.get(i).getPath();
Field FieldPath = new Field("filePath",filePath,Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(FieldPath);
String contents = FileUtils.fileReaderAll(subFiles.get(i).getCanonicalPath(),"GBK");// 转化成GBK
Field FieldBody = new Field("contents", contents, Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(FieldBody);
indexWriter.addDocument(document);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// optimize()方法是对索引进行优化
indexWriter.optimize();
indexWriter.close();
// 测试一下索引的时间
long endTime = new Date().getTime();
System.out.println("这花费了" + (endTime - startTime)+" 毫秒来把文档增加到索引里面去!\n"+indexDir.getPath());
File[] fils=indexDir.listFiles();
for(int i=0;i<fils.length;i++){
System.out.println(fils[i].getCanonicalPath());
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}