Skip to content

Commit f0cb1e6

Browse files
ahornaceVladimir Kotal
authored andcommitted
Suggester rebuild does not reflect fields change
1 parent 924de7a commit f0cb1e6

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

suggester/src/main/java/org/opengrok/suggest/SuggesterProjectData.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class SuggesterProjectData implements Closeable {
9898

9999
private final ReadWriteLock lock = new ReentrantReadWriteLock();
100100

101+
private final Set<String> allowedFields;
102+
101103
private Set<String> fields;
102104

103105
private final Directory tempDir;
@@ -106,34 +108,35 @@ class SuggesterProjectData implements Closeable {
106108
final Directory indexDir,
107109
final Path suggesterDir,
108110
final boolean allowMostPopular,
109-
final Set<String> fields
111+
final Set<String> allowedFields
110112
) throws IOException {
111113
this.indexDir = indexDir;
112114
this.suggesterDir = suggesterDir;
113115
this.allowMostPopular = allowMostPopular;
116+
this.allowedFields = allowedFields;
114117

115118
tempDir = FSDirectory.open(Paths.get(System.getProperty(TMP_DIR_PROPERTY)));
116119

117-
initFields(fields);
120+
initFields();
118121
}
119122

120-
private void initFields(final Set<String> fields) throws IOException {
123+
private void initFields() throws IOException {
121124
try (IndexReader indexReader = DirectoryReader.open(indexDir)) {
122125
Collection<String> indexedFields = FieldInfos.getIndexedFields(indexReader);
123-
if (fields == null) {
126+
if (allowedFields == null) {
124127
this.fields = new HashSet<>(indexedFields);
125-
} else if (!indexedFields.containsAll(fields)) {
126-
Set<String> copy = new HashSet<>(fields);
128+
} else if (!indexedFields.containsAll(allowedFields)) {
129+
Set<String> copy = new HashSet<>(allowedFields);
127130
copy.removeAll(indexedFields);
128131
logger.log(Level.WARNING,
129132
"Fields {0} will be ignored because they were not found in index directory {1}",
130133
new Object[] {copy, indexDir});
131134

132-
copy = new HashSet<>(fields);
135+
copy = new HashSet<>(allowedFields);
133136
copy.retainAll(indexedFields);
134137
this.fields = copy;
135138
} else {
136-
this.fields = new HashSet<>(fields);
139+
this.fields = new HashSet<>(allowedFields);
137140
}
138141
}
139142
}
@@ -231,6 +234,7 @@ private File getFile(final String fileName) {
231234
public void rebuild() throws IOException {
232235
lock.writeLock().lock();
233236
try {
237+
initFields();
234238
build();
235239

236240
if (allowMostPopular) {

suggester/src/test/java/org/opengrok/suggest/SuggesterProjectDataTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,22 @@ public void testGetSearchCountMapSorted() throws IOException {
308308
assertThat(searchCounts, contains(new SimpleEntry<>(t1.bytes(), 10), new SimpleEntry<>(t2.bytes(), 5)));
309309
}
310310

311+
@Test
312+
public void testRebuildPicksUpNewFields() throws IOException {
313+
// create dummy index
314+
try (IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig())) {
315+
iw.isOpen(); // dummy operation
316+
}
317+
init(false);
318+
319+
// add new field after suggester data were initialized
320+
addText(FIELD, "term1 term2");
321+
322+
assertTrue(getSuggestions(FIELD, "t", 10).isEmpty());
323+
324+
data.rebuild();
325+
326+
assertFalse(getSuggestions(FIELD, "t", 10).isEmpty());
327+
}
328+
311329
}

0 commit comments

Comments
 (0)