Beautify SuggestSearchTests.

SuggestSearchTests had tons of duplicate code and didn't use all of the
fancy new integration test helper method.  I've removed a ton of duplicate
code and used as many of the nice test helper method I could think of.

Closes #3611
This commit is contained in:
Nik Everett 2013-09-03 10:05:31 -04:00 committed by Simon Willnauer
parent 3c5dd43928
commit da4c58d853
4 changed files with 608 additions and 1099 deletions

View File

@ -18,19 +18,15 @@
*/ */
package org.elasticsearch.search.suggest.phrase; package org.elasticsearch.search.suggest.phrase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.suggest.SuggestBuilder.SuggestionBuilder; import org.elasticsearch.search.suggest.SuggestBuilder.SuggestionBuilder;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
/** /**
* Defines the actual suggest command for phrase suggestions ( <tt>phrase</tt>). * Defines the actual suggest command for phrase suggestions ( <tt>phrase</tt>).
*/ */
@ -125,6 +121,14 @@ public final class PhraseSuggestionBuilder extends SuggestionBuilder<PhraseSugge
list.add(generator); list.add(generator);
return this; return this;
} }
/**
* Clear the candidate generators.
*/
public PhraseSuggestionBuilder clearCandidateGenerators() {
this.generators.clear();
return this;
}
/** /**
* If set to <code>true</code> the phrase suggester will fail if the analyzer only * If set to <code>true</code> the phrase suggester will fail if the analyzer only
@ -154,8 +158,8 @@ public final class PhraseSuggestionBuilder extends SuggestionBuilder<PhraseSugge
* is returned with suggestions wrapping changed tokens with preTag and postTag. * is returned with suggestions wrapping changed tokens with preTag and postTag.
*/ */
public PhraseSuggestionBuilder highlight(String preTag, String postTag) { public PhraseSuggestionBuilder highlight(String preTag, String postTag) {
if (preTag == null || postTag == null) { if ((preTag == null) != (postTag == null)) {
throw new ElasticSearchIllegalArgumentException("Pre and post tag must not be null."); throw new ElasticSearchIllegalArgumentException("Pre and post tag must both be null or both not be null.");
} }
this.preTag = preTag; this.preTag = preTag;
this.postTag = postTag; this.postTag = postTag;

View File

@ -179,6 +179,23 @@ public class ElasticsearchAssertions {
assertThat(searchSuggest.getSuggestion(key).getEntries().get(entry).getOptions().get(ord).getText().string(), equalTo(text)); assertThat(searchSuggest.getSuggestion(key).getEntries().get(entry).getOptions().get(ord).getText().string(), equalTo(text));
} }
/**
* Assert suggestion returns exactly the provided text.
*/
public static void assertSuggestion(Suggest searchSuggest, int entry, String key, String... text) {
assertSuggestion(searchSuggest, entry, key, text.length, text);
}
/**
* Assert suggestion returns size suggestions and the first are the provided text.
*/
public static void assertSuggestion(Suggest searchSuggest, int entry, String key, int size, String... text) {
assertSuggestionSize(searchSuggest, entry, size, key);
for( int i = 0; i < text.length; i++) {
assertSuggestion(searchSuggest, entry, i, key, text[i]);
}
}
/* /*
* matchers * matchers
*/ */

View File

@ -217,8 +217,24 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase {
public CreateIndexRequestBuilder addMapping(CreateIndexRequestBuilder builder, String type, Object[]... mapping) throws IOException { public CreateIndexRequestBuilder addMapping(CreateIndexRequestBuilder builder, String type, Object[]... mapping) throws IOException {
XContentBuilder mappingBuilder = jsonBuilder(); XContentBuilder mappingBuilder = jsonBuilder();
mappingBuilder.startObject().startObject(type).startObject("properties"); mappingBuilder.startObject().startObject(type);
for (Object[] objects : mapping) { for (Object[] objects : mapping) {
if (!objects[0].toString().equals("_all")) {
continue;
}
mappingBuilder.startObject("_all");
for (int i = 1; i < objects.length; i++) {
String name = objects[i++].toString();
Object value = objects[i];
mappingBuilder.field(name, value);
}
mappingBuilder.endObject();
}
mappingBuilder.startObject("properties");
for (Object[] objects : mapping) {
if (objects[0].toString().equals("_all")) {
continue;
}
mappingBuilder.startObject(objects[0].toString()); mappingBuilder.startObject(objects[0].toString());
for (int i = 1; i < objects.length; i++) { for (int i = 1; i < objects.length; i++) {
String name = objects[i++].toString(); String name = objects[i++].toString();
@ -323,7 +339,7 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase {
return client().prepareIndex(index, type).setSource(source).execute().actionGet(); return client().prepareIndex(index, type).setSource(source).execute().actionGet();
} }
protected IndexResponse index(String index, String type, String id, Map<String, Object> source) { protected IndexResponse index(String index, String type, String id, Map<String, ? extends Object> source) {
return client().prepareIndex(index, type, id).setSource(source).execute().actionGet(); return client().prepareIndex(index, type, id).setSource(source).execute().actionGet();
} }
@ -336,8 +352,8 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase {
return client().prepareIndex(index, type, id).setSource(source).execute().actionGet(); return client().prepareIndex(index, type, id).setSource(source).execute().actionGet();
} }
protected IndexResponse index(String index, String type, String id, String field, Object value) { protected IndexResponse index(String index, String type, String id, Object... source) {
return client().prepareIndex(index, type, id).setSource(field, value).execute().actionGet(); return client().prepareIndex(index, type, id).setSource(source).execute().actionGet();
} }
protected RefreshResponse refresh() { protected RefreshResponse refresh() {