本文档旨在分析Lucene如何把业务信息写到磁盘上的大致流程,并不涉及Document中每个Field如何存储(该部分放在另外一篇wiki中介绍)。
一,Lucene建索引API
1
2
3
4
5
6
7
8
9
10
|
Directory dire = NIOFSDirectory.open(FileSystems.getDefault().getPath(indexDirectory));
IndexWriterConfig iwc =
new
IndexWriterConfig(
new
StandardAnalyzer());
iwc.setRAMBufferSizeMB(
64
);
//兆默认刷
indexWriter =
new
IndexWriter(dire, iwc);
Document doc = createDocument(artiste, skuId);
indexWriter.addDocument(doc);
indexWriter.commit();
indexWriter.close();
|
二,创建IndexWriter
NIOFSDirectory.open()
|
如果是64位JRE会得到MMapDirectory(采用内存映射的方式写索引数据到File中)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
IndexWriterConfig
//properties
this
.analyzer = analyzer;
ramBufferSizeMB = IndexWriterConfig.DEFAULT_RAM_BUFFER_SIZE_MB;
//默认超过16M就会触发flush磁盘操作
maxBufferedDocs = IndexWriterConfig.DEFAULT_MAX_BUFFERED_DOCS;
//默认按照RAM空间大小触发flush
maxBufferedDeleteTerms = IndexWriterConfig.DEFAULT_MAX_BUFFERED_DELETE_TERMS;
//
mergedSegmentWarmer =
null
;
delPolicy =
new
KeepOnlyLastCommitDeletionPolicy();
//删除策略
commit =
null
;
useCompoundFile = IndexWriterConfig.DEFAULT_USE_COMPOUND_FILE_SYSTEM;
openMode = OpenMode.CREATE_OR_APPEND;
//IndexWriter打开模式
similarity = IndexSearcher.getDefaultSimilarity();
//相似度计算,一般初始化Searcher的时候会用(因为只有查询的时候才会用到相似度计算)
mergeScheduler =
new
ConcurrentMergeScheduler();
//每个segement的merge交个一个线程完成
writeLockTimeout = IndexWriterConfig.WRITE_LOCK_TIMEOUT;
//写操作遇到锁超时时间
indexingChain = DocumentsWriterPerThread.defaultIndexingChain;
codec = Codec.getDefault();
|