|
26 | 26 | import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
|
27 | 27 | import org.apache.lucene.analysis.reverse.ReverseStringFilter;
|
28 | 28 | import org.apache.lucene.analysis.shingle.ShingleFilter;
|
| 29 | +import org.apache.lucene.analysis.standard.StandardAnalyzer; |
29 | 30 | import org.apache.lucene.analysis.standard.StandardTokenizer;
|
30 | 31 | import org.apache.lucene.analysis.synonym.SolrSynonymParser;
|
31 | 32 | import org.apache.lucene.analysis.synonym.SynonymFilter;
|
|
38 | 39 | import org.apache.lucene.index.MultiFields;
|
39 | 40 | import org.apache.lucene.search.spell.DirectSpellChecker;
|
40 | 41 | import org.apache.lucene.search.spell.SuggestMode;
|
| 42 | +import org.apache.lucene.store.Directory; |
41 | 43 | import org.apache.lucene.store.RAMDirectory;
|
42 | 44 | import org.apache.lucene.util.BytesRef;
|
43 | 45 | import org.elasticsearch.search.suggest.phrase.NoisyChannelSpellChecker.Result;
|
44 | 46 | import org.elasticsearch.test.ESTestCase;
|
45 | 47 |
|
46 |
| -import java.io.BufferedReader; |
47 | 48 | import java.io.IOException;
|
48 |
| -import java.io.InputStreamReader; |
49 | 49 | import java.io.StringReader;
|
50 |
| -import java.nio.charset.StandardCharsets; |
51 | 50 | import java.util.HashMap;
|
52 | 51 | import java.util.Map;
|
53 | 52 |
|
@@ -439,4 +438,29 @@ protected TokenStreamComponents createComponents(String fieldName) {
|
439 | 438 | assertThat(corrections[0].join(new BytesRef(" ")).utf8ToString(), equalTo("xorr the god jewel"));
|
440 | 439 | assertThat(corrections[1].join(new BytesRef(" ")).utf8ToString(), equalTo("xor the god jewel"));
|
441 | 440 | }
|
| 441 | + |
| 442 | + public void testFewDocsEgdeCase() throws Exception { |
| 443 | + try (Directory dir = newDirectory()) { |
| 444 | + try (IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig())) { |
| 445 | + Document document = new Document(); |
| 446 | + document.add(new TextField("field", "value", Field.Store.NO)); |
| 447 | + iw.addDocument(document); |
| 448 | + iw.commit(); |
| 449 | + document = new Document(); |
| 450 | + document.add(new TextField("other_field", "value", Field.Store.NO)); |
| 451 | + iw.addDocument(document); |
| 452 | + } |
| 453 | + |
| 454 | + try (DirectoryReader ir = DirectoryReader.open(dir)) { |
| 455 | + WordScorer wordScorer = new StupidBackoffScorer(ir, MultiFields.getTerms(ir, "field"), "field", 0.95d, new BytesRef(" "), 0.4f); |
| 456 | + NoisyChannelSpellChecker suggester = new NoisyChannelSpellChecker(); |
| 457 | + DirectSpellChecker spellchecker = new DirectSpellChecker(); |
| 458 | + DirectCandidateGenerator generator = new DirectCandidateGenerator(spellchecker, "field", SuggestMode.SUGGEST_MORE_POPULAR, ir, 0.95, 5); |
| 459 | + Result result = suggester.getCorrections(new StandardAnalyzer(), new BytesRef("valeu"), generator, 1, 1, ir, "field", wordScorer, 1, 2); |
| 460 | + assertThat(result.corrections.length, equalTo(1)); |
| 461 | + assertThat(result.corrections[0].join(space).utf8ToString(), equalTo("value")); |
| 462 | + } |
| 463 | + } |
| 464 | + } |
| 465 | + |
442 | 466 | }
|
0 commit comments