Terminate early when no terms left in the suggest string.

Closes #2817
This commit is contained in:
Simon Willnauer 2013-03-26 17:44:34 +01:00
parent 9ae421a8b2
commit 17f83f33bb
4 changed files with 31 additions and 0 deletions

View File

@ -36,6 +36,9 @@ final class CandidateScorer {
public Correction[] findBestCandiates(CandidateSet[] sets, float errorFraction, double cutoffScore) throws IOException { public Correction[] findBestCandiates(CandidateSet[] sets, float errorFraction, double cutoffScore) throws IOException {
if (sets.length == 0) {
return Correction.EMPTY;
}
PriorityQueue<Correction> corrections = new PriorityQueue<Correction>(maxNumCorrections) { PriorityQueue<Correction> corrections = new PriorityQueue<Correction>(maxNumCorrections) {
@Override @Override
protected boolean lessThan(Correction a, Correction b) { protected boolean lessThan(Correction a, Correction b) {

View File

@ -26,6 +26,7 @@ import org.elasticsearch.search.suggest.phrase.DirectCandidateGenerator.Candidat
//TODO public for tests //TODO public for tests
public final class Correction { public final class Correction {
public static final Correction[] EMPTY = new Correction[0];
public double score; public double score;
public final Candidate[] candidates; public final Candidate[] candidates;

View File

@ -104,6 +104,10 @@ public final class NoisyChannelSpellChecker {
} }
}); });
if (candidateSetsList.isEmpty()) {
return Correction.EMPTY;
}
for (CandidateSet candidateSet : candidateSetsList) { for (CandidateSet candidateSet : candidateSetsList) {
generator.drawCandidates(candidateSet); generator.drawCandidates(candidateSet);
} }

View File

@ -405,6 +405,29 @@ public class SuggestSearchTests extends AbstractNodesTests {
// assertThat(search.suggest().suggestions().get(3).getSuggestedWords().get("prefix_abcd").get(4).getTerm(), equalTo("prefix_accd")); // assertThat(search.suggest().suggestions().get(3).getSuggestedWords().get("prefix_abcd").get(4).getTerm(), equalTo("prefix_accd"));
} }
@Test // see #2817
public void testStopwordsOnlyPhraseSuggest() throws ElasticSearchException, IOException {
client.admin().indices().prepareDelete().execute().actionGet();
Builder builder = ImmutableSettings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0);
client.admin().indices().prepareCreate("test").setSettings(builder.build()).execute().actionGet();
client.admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();
client.prepareIndex("test", "type1")
.setSource(XContentFactory.jsonBuilder().startObject().field("body", "this is a test").endObject()).execute().actionGet();
client.admin().indices().prepareRefresh().execute().actionGet();
Suggest searchSuggest = searchSuggest(
client,
"a an the",
phraseSuggestion("simple_phrase").field("body").gramSize(1)
.addCandidateGenerator(PhraseSuggestionBuilder.candidateGenerator("body").minWordLength(1).suggestMode("always"))
.size(1));
assertThat(searchSuggest, notNullValue());
assertThat(searchSuggest.size(), equalTo(1));
assertThat(searchSuggest.getSuggestion("simple_phrase").getName(), equalTo("simple_phrase"));
assertThat(searchSuggest.getSuggestion("simple_phrase").getEntries().size(), equalTo(1));
assertThat(searchSuggest.getSuggestion("simple_phrase").getEntries().get(0).getOptions().size(), equalTo(0));
}
@Test @Test
public void testMarvelHerosPhraseSuggest() throws ElasticSearchException, IOException { public void testMarvelHerosPhraseSuggest() throws ElasticSearchException, IOException {