package lucene.test.index; import java.io.IOException; import java.util.Date; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.SimpleAnalyzer; 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.index.IndexWriter.MaxFieldLength; import org.apache.lucene.store.LockObtainFailedException; public class indexTest { private static String indexStore="D://MajorWork//javaWeb//luceneTestapi//indexStore"; static protected String[] id={"001","002","003"}; static protected String[] content={"搜索引擎","全文检索","信息检索"}; public static void main(String[] args) { Date start=new Date(); Analyzer analyzer=new SimpleAnalyzer(); try { IndexWriter writer=new IndexWriter(indexStore,analyzer,true,MaxFieldLength.LIMITED); for(int i=0;i<3;i++) { Document doc=new Document(); Field field_id=new Field("id","id[i]",Field.Store.YES,Field.Index.NOT_ANALYZED); doc.add(field_id); Field field_content=new Field("content","content[i]",Field.Store.YES,Field.Index.ANALYZED); doc.add(field_content); writer.addDocument(doc); } writer.optimize(); writer.close(); Date end=new Date(); long indexTime=end.getTime()-start.getTime(); System.out.println("索引完成所需时间:(ms)"); System.out.println(indexTime); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("创建索引完成!"); } } /* * 索引域:Field是Document对象的基本组成单位,每个Field存储了实际的所有文本数据,这些文本数据在内部调用了分析器Aanlyzer的索引项结果 * Field内的检索查询最终以索引项为单位的.比索引更小的单位无法检索到 * 中文的索引项一中文分词的结果为检索单元,英文的索引项是以单词为检索单元,检索单元为最小的检索单位 * 1.public Field(String name, byte[] value, Store store) * 2.public Field(String name, byte[] value, int offset, int length, Store store) * 3.public Field(String name, String value, Store store, Index index) * 4.public Field(String name, String value, Store store, Index index, TermVector termVector) * 5.public Field(String name, Reader reader) * 6.public Field(String name, Reader reader, TermVector termVector) * 7.public Field(String name, TokenStream tokenStream) * 8.public Field(String name, TokenStream tokenStream, TermVector termVector) * 第1,2个函数用于二进制数据索引;3,4用于直接给定字符串的索引,5,6用于文件内容的索引,即Reader数据流(常用) * name-域名为固定的参数,用于指定添加域的标识,限定检索的范围或提取属性值 * value- * Store-控制域数据的存储,表示数据本身是否存储(注意:并不是指索引是否存储) * 1.Store.NO 只保存索引,不包含数据本身信息,减少空间采用 * 2.Store.YES 保存索引并保存数据原始信息 * 3.Store.COMPRESS 存储并压缩原始信息 * Index-控制索引格式的参数,表示数据是否需要索引,即当前域是否需要用于检索 * 1.Index.NO 不索引,只作为辅助信息 * 2.Index.ANALYZER 2.4版本替换6 * 3.Index.NOT_ANALYZER 2.4版本替换7 * 4.Index.ANALYZER_NO_NORMS * 5.Index.NOT_ANALYZER_NO_NOTRMS * 6.Index.TOKENIZED 使用分词器并建立索引 * 7.Index.UN_TOKENIZED 不分词建立索引(某些内容的整体作为索引) * 8.Index.NO_NORMS 禁用分析器处理 * TermVector-域内信息是否需要分词,在中文处理中分词是索引的基础 * TermVector保存Token.getPositionIncrement() 和Token.startOffset() 以及Token.endOffset() 信息 * 1.Field.TermVector.NO:不保存term vectors * 2.Field.TermVector.YES:保存term vectors * 3.Field.TermVector.WITH_POSITIONS:保存term vectors.(保存值和token位置信息) * 4.Field.TermVector.WITH_OFFSETS:保存term vectors.(保存值和Token的offset) * 5.Field.TermVector.WITH_POSITIONS_OFFSETS:保存term vectors.(保存值和token位置信息和Token的offset) */