Skip to content

Commit

Permalink
Terminate early when no terms left in the suggest string.
Browse files Browse the repository at this point in the history
Closes #2817
  • Loading branch information
s1monw committed Mar 26, 2013
1 parent 9ae421a commit 17f83f3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
Expand Up @@ -36,6 +36,9 @@ public CandidateScorer(WordScorer scorer, int maxNumCorrections, int gramSize) {


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) {
@Override
protected boolean lessThan(Correction a, Correction b) {
Expand Down
Expand Up @@ -26,6 +26,7 @@
//TODO public for tests
public final class Correction {

public static final Correction[] EMPTY = new Correction[0];
public double score;
public final Candidate[] candidates;

Expand Down
Expand Up @@ -104,6 +104,10 @@ public void end() {
}
});

if (candidateSetsList.isEmpty()) {
return Correction.EMPTY;
}

for (CandidateSet candidateSet : candidateSetsList) {
generator.drawCandidates(candidateSet);
}
Expand Down
Expand Up @@ -405,6 +405,29 @@ public void testSizeAndSort() throws Exception {
// 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
public void testMarvelHerosPhraseSuggest() throws ElasticSearchException, IOException {
Expand Down

0 comments on commit 17f83f3

Please sign in to comment.