Set shardSize according to the total size if not explicitly specified. Closes #2729

This commit is contained in:
Simon Willnauer 2013-03-05 09:22:01 +01:00
parent 3e264f6b95
commit 315744be55
3 changed files with 67 additions and 2 deletions

View File

@ -270,6 +270,9 @@ public final class SuggestUtils {
if (suggestion.getAnalyzer() == null) {
suggestion.setAnalyzer(context.mapperService().searchAnalyzer());
}
if (suggestion.getShardSize() == -1) {
suggestion.setShardSize(Math.max(suggestion.getSize(), 5));
}
}

View File

@ -45,7 +45,7 @@ public class SuggestionSearchContext {
private String field;
private Analyzer analyzer;
private int size = 5;
private int shardSize = 5;
private int shardSize = -1;
public BytesRef getText() {
return text;
@ -85,7 +85,7 @@ public class SuggestionSearchContext {
public void setSize(int size) {
if (size <= 0) {
throw new ElasticSearchIllegalArgumentException("Size must be positive");
throw new ElasticSearchIllegalArgumentException("Size must be positive but was: " + size);
}
this.size = size;
}
@ -95,6 +95,9 @@ public class SuggestionSearchContext {
}
public void setShardSize(int shardSize) {
if (shardSize <= 0) {
throw new ElasticSearchIllegalArgumentException("ShardSize must be positive but was: " + shardSize);
}
this.shardSize = shardSize;
}
}

View File

@ -78,6 +78,65 @@ public class SuggestSearchTests extends AbstractNodesTests {
return client("server1");
}
@Test // see #2729
public void testSizeOneShard() throws Exception {
client.admin().indices().prepareDelete().execute().actionGet();
client.admin().indices().prepareCreate("test")
.setSettings(settingsBuilder()
.put(SETTING_NUMBER_OF_SHARDS, 1)
.put(SETTING_NUMBER_OF_REPLICAS, 0))
.execute().actionGet();
client.admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();
for (int i = 0; i < 15; i++) {
client.prepareIndex("test", "type1")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("text", "abc" + i)
.endObject()
)
.execute().actionGet();
}
client.admin().indices().prepareRefresh().execute().actionGet();
SearchResponse search = client.prepareSearch()
.setQuery(matchQuery("text", "spellchecker")).execute().actionGet();
assertThat("didn't ask for suggestions but got some", search.getSuggest(), nullValue());
search = client.prepareSearch()
.setQuery(matchQuery("text", "spellchecker"))
.addSuggestion(
termSuggestion("test").suggestMode("always") // Always, otherwise the results can vary between requests.
.text("abcd")
.field("text").size(10))
.execute().actionGet();
assertThat(Arrays.toString(search.getShardFailures()), search.getFailedShards(), equalTo(0));
assertThat(search.getSuggest(), notNullValue());
assertThat(search.getSuggest().size(), equalTo(1));
assertThat(search.getSuggest().getSuggestion("test").getName(), equalTo("test"));
assertThat(search.getSuggest().getSuggestion("test").getEntries().size(), equalTo(1));
assertThat(search.getSuggest().getSuggestion("test").getEntries().get(0).getText().string(), equalTo("abcd"));
assertThat(search.getSuggest().getSuggestion("test").getEntries().get(0).getOptions().size(), equalTo(10));
search = client.prepareSearch()
.setQuery(matchQuery("text", "spellchecker"))
.addSuggestion(
termSuggestion("test").suggestMode("always") // Always, otherwise the results can vary between requests.
.text("abcd")
.field("text").size(10).shardSize(5))
.execute().actionGet();
assertThat(Arrays.toString(search.getShardFailures()), search.getFailedShards(), equalTo(0));
assertThat(search.getSuggest(), notNullValue());
assertThat(search.getSuggest().size(), equalTo(1));
assertThat(search.getSuggest().getSuggestion("test").getName(), equalTo("test"));
assertThat(search.getSuggest().getSuggestion("test").getEntries().size(), equalTo(1));
assertThat(search.getSuggest().getSuggestion("test").getEntries().get(0).getText().string(), equalTo("abcd"));
assertThat(search.getSuggest().getSuggestion("test").getEntries().get(0).getOptions().size(), equalTo(5));
}
@Test
public void testSimple() throws Exception {
client.admin().indices().prepareDelete().execute().actionGet();