-
Notifications
You must be signed in to change notification settings - Fork 242
/
IndexWriterConfigSource.java
75 lines (64 loc) · 2.68 KB
/
IndexWriterConfigSource.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.backend.lucene.lowlevel.writer.impl;
import java.util.List;
import org.hibernate.search.engine.cfg.spi.ConfigurationPropertySource;
import org.hibernate.search.util.common.reporting.EventContext;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogByteSizeMergePolicy;
import org.apache.lucene.search.similarities.Similarity;
/**
* A source of index writer configuration that can be re-used on multiple writers.
* <p>
* This is mostly necessary because we don't have access to the user configuration after startup,
* and we don't want to re-use the same IndexWriterConfig instance multiple times in order to be safe
* and make sure a previous, failing index writer will never affect the configuration of
* the new index writer created to replace it.
*
* @author Hardy Ferentschik
* @author Sanne Grinovero
*/
public class IndexWriterConfigSource {
public static IndexWriterConfigSource create(Similarity similarity, Analyzer analyzer,
ConfigurationPropertySource propertySource, EventContext eventContext) {
List<IndexWriterSettingValue<?>> values = IndexWriterSettings.extractAll( propertySource, eventContext );
return new IndexWriterConfigSource( similarity, analyzer, values );
}
private final Similarity similarity;
private final Analyzer analyzer;
private final List<IndexWriterSettingValue<?>> values;
private IndexWriterConfigSource(Similarity similarity, Analyzer analyzer, List<IndexWriterSettingValue<?>> values) {
this.similarity = similarity;
this.analyzer = analyzer;
this.values = values;
}
@Override
public String toString() {
return "IndexWriterConfigSource{" + analyzer + "," + values + '}';
}
/**
* Creates a new {@link IndexWriterConfig}.
* Undefined parameters are not set, leaving the lucene default.
*/
public IndexWriterConfig createIndexWriterConfig() {
IndexWriterConfig writerConfig = new IndexWriterConfig( analyzer );
writerConfig.setSimilarity( similarity );
for ( IndexWriterSettingValue<?> value : values ) {
value.applySetting( writerConfig );
}
writerConfig.setMergePolicy( createMergePolicy() );
return writerConfig;
}
private LogByteSizeMergePolicy createMergePolicy() {
LogByteSizeMergePolicy logByteSizeMergePolicy = new LogByteSizeMergePolicy();
for ( IndexWriterSettingValue<?> value : values ) {
value.applySetting( logByteSizeMergePolicy );
}
return logByteSizeMergePolicy;
}
}