thecoop commented on code in PR #15134:
URL: https://github.com/apache/lucene/pull/15134#discussion_r2306709652
##########
lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/FreeTextSuggester.java:
##########
@@ -286,70 +282,64 @@ public void build(InputIterator iterator, double
ramBufferSizeMB) throws IOExcep
writer.addDocument(doc);
newCount++;
}
- reader = DirectoryReader.open(writer);
+ try (IndexReader reader = DirectoryReader.open(writer)) {
- Terms terms = MultiTerms.getTerms(reader, "body");
- if (terms == null) {
- throw new IllegalArgumentException("need at least one suggestion");
- }
+ Terms terms = MultiTerms.getTerms(reader, "body");
+ if (terms == null) {
+ throw new IllegalArgumentException("need at least one suggestion");
+ }
- // Move all ngrams into an FST:
- TermsEnum termsEnum = terms.iterator();
+ // Move all ngrams into an FST:
+ TermsEnum termsEnum = terms.iterator();
- Outputs<Long> outputs = PositiveIntOutputs.getSingleton();
- FSTCompiler<Long> fstCompiler =
- new FSTCompiler.Builder<>(FST.INPUT_TYPE.BYTE1, outputs).build();
+ Outputs<Long> outputs = PositiveIntOutputs.getSingleton();
+ FSTCompiler<Long> fstCompiler =
+ new FSTCompiler.Builder<>(FST.INPUT_TYPE.BYTE1, outputs).build();
- IntsRefBuilder scratchInts = new IntsRefBuilder();
- while (true) {
- BytesRef term = termsEnum.next();
- if (term == null) {
- break;
- }
- int ngramCount = countGrams(term);
- if (ngramCount > grams) {
- throw new IllegalArgumentException(
- "tokens must not contain separator byte; got token="
- + term
- + " but gramCount="
- + ngramCount
- + ", which is greater than expected max ngram size="
- + grams);
+ IntsRefBuilder scratchInts = new IntsRefBuilder();
+ while (true) {
+ BytesRef term = termsEnum.next();
+ if (term == null) {
+ break;
+ }
+ int ngramCount = countGrams(term);
+ if (ngramCount > grams) {
+ throw new IllegalArgumentException(
+ "tokens must not contain separator byte; got token="
+ + term
+ + " but gramCount="
+ + ngramCount
+ + ", which is greater than expected max ngram size="
+ + grams);
+ }
+ if (ngramCount == 1) {
+ totTokens += termsEnum.totalTermFreq();
+ }
+
+ fstCompiler.add(
+ Util.toIntsRef(term, scratchInts),
encodeWeight(termsEnum.totalTermFreq()));
}
- if (ngramCount == 1) {
- totTokens += termsEnum.totalTermFreq();
+
+ final FST<Long> newFst =
+ FST.fromFSTReader(fstCompiler.compile(),
fstCompiler.getFSTReader());
+ if (newFst == null) {
+ throw new IllegalArgumentException("need at least one suggestion");
}
+ fst = newFst;
+ count = newCount;
- fstCompiler.add(Util.toIntsRef(term, scratchInts),
encodeWeight(termsEnum.totalTermFreq()));
- }
+ // System.out.println("FST: " + fst.getNodeCount() + " nodes");
- final FST<Long> newFst = FST.fromFSTReader(fstCompiler.compile(),
fstCompiler.getFSTReader());
- if (newFst == null) {
- throw new IllegalArgumentException("need at least one suggestion");
- }
- fst = newFst;
- count = newCount;
-
- // System.out.println("FST: " + fst.getNodeCount() + " nodes");
-
- /*
- PrintWriter pw = new PrintWriter("/x/tmp/out.dot");
- Util.toDot(fst, pw, true, true);
- pw.close();
- */
-
- // Writer was only temporary, to count up bigrams,
- // which we transferred to the FST, so now we
- // rollback:
- writer.rollback();
- success = true;
- } finally {
- try {
- if (success) {
- IOUtils.close(reader, dir);
- } else {
- IOUtils.closeWhileHandlingException(reader, writer, dir);
Review Comment:
The new code implicitly calls `close()` on `writer` after `rollback()` has
been called, but `IndexWriter` can handle that
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]