|
@@ -39,11 +39,14 @@ import org.joda.time.format.ISODateTimeFormat;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
|
|
|
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
|
|
|
import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;
|
|
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
|
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
|
|
|
+import static org.hamcrest.Matchers.allOf;
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.is;
|
|
@@ -252,6 +255,37 @@ public class SimpleValidateQueryIT extends ESIntegTestCase {
|
|
|
containsString("field:huge field:pidgin"), true);
|
|
|
}
|
|
|
|
|
|
+ public void testExplainWithRewriteValidateQueryAllShards() throws Exception {
|
|
|
+ client().admin().indices().prepareCreate("test")
|
|
|
+ .addMapping("type1", "field", "type=text,analyzer=whitespace")
|
|
|
+ .setSettings(SETTING_NUMBER_OF_SHARDS, 2).get();
|
|
|
+ // We are relying on specific routing behaviors for the result to be right, so
|
|
|
+ // we cannot randomize the number of shards or change ids here.
|
|
|
+ client().prepareIndex("test", "type1", "1")
|
|
|
+ .setSource("field", "quick lazy huge brown pidgin").get();
|
|
|
+ client().prepareIndex("test", "type1", "2")
|
|
|
+ .setSource("field", "the quick brown fox").get();
|
|
|
+ client().prepareIndex("test", "type1", "3")
|
|
|
+ .setSource("field", "the quick lazy huge brown fox jumps over the tree").get();
|
|
|
+ client().prepareIndex("test", "type1", "4")
|
|
|
+ .setSource("field", "the lazy dog quacks like a duck").get();
|
|
|
+ refresh();
|
|
|
+
|
|
|
+ // prefix queries
|
|
|
+ assertExplanations(QueryBuilders.matchPhrasePrefixQuery("field", "qu"),
|
|
|
+ Arrays.asList(
|
|
|
+ equalTo("field:quick"),
|
|
|
+ allOf(containsString("field:quick"), containsString("field:quacks"))
|
|
|
+ ), true, true);
|
|
|
+ assertExplanations(QueryBuilders.matchPhrasePrefixQuery("field", "ju"),
|
|
|
+ Arrays.asList(
|
|
|
+ equalTo("field:jumps"),
|
|
|
+ equalTo("+MatchNoDocsQuery(\"empty MultiPhraseQuery\") +MatchNoDocsQuery(\"No " +
|
|
|
+ "terms supplied for org.elasticsearch.common.lucene.search." +
|
|
|
+ "MultiPhrasePrefixQuery\")")
|
|
|
+ ), true, true);
|
|
|
+ }
|
|
|
+
|
|
|
public void testIrrelevantPropertiesBeforeQuery() throws IOException {
|
|
|
createIndex("test");
|
|
|
ensureGreen();
|
|
@@ -280,4 +314,22 @@ public class SimpleValidateQueryIT extends ESIntegTestCase {
|
|
|
assertThat(response.getQueryExplanation().get(0).getExplanation(), matcher);
|
|
|
assertThat(response.isValid(), equalTo(true));
|
|
|
}
|
|
|
+
|
|
|
+ private static void assertExplanations(QueryBuilder queryBuilder,
|
|
|
+ List<Matcher<String>> matchers, boolean withRewrite,
|
|
|
+ boolean allShards) {
|
|
|
+ ValidateQueryResponse response = client().admin().indices().prepareValidateQuery("test")
|
|
|
+ .setTypes("type1")
|
|
|
+ .setQuery(queryBuilder)
|
|
|
+ .setExplain(true)
|
|
|
+ .setRewrite(withRewrite)
|
|
|
+ .setAllShards(allShards)
|
|
|
+ .execute().actionGet();
|
|
|
+ assertThat(response.getQueryExplanation().size(), equalTo(matchers.size()));
|
|
|
+ for (int i = 0; i < matchers.size(); i++) {
|
|
|
+ assertThat(response.getQueryExplanation().get(i).getError(), nullValue());
|
|
|
+ assertThat(response.getQueryExplanation().get(i).getExplanation(), matchers.get(i));
|
|
|
+ assertThat(response.isValid(), equalTo(true));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|