|
@@ -18,7 +18,11 @@
|
|
|
*/
|
|
|
package org.elasticsearch.index.query;
|
|
|
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+import org.elasticsearch.action.search.SearchRequest;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
+import org.elasticsearch.common.bytes.BytesArray;
|
|
|
+import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.settings.ImmutableSettings;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
|
@@ -30,6 +34,11 @@ import java.io.IOException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
|
|
|
+import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
|
|
+import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
|
|
+import static org.hamcrest.Matchers.is;
|
|
|
+
|
|
|
/**
|
|
|
* Full integration test of the template query plugin.
|
|
|
* */
|
|
@@ -37,17 +46,21 @@ import java.util.Map;
|
|
|
public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|
|
|
|
|
@Before
|
|
|
- public void setup() {
|
|
|
+ public void setup() throws IOException {
|
|
|
createIndex("test");
|
|
|
- ensureGreen();
|
|
|
+ ensureGreen("test");
|
|
|
|
|
|
- client().prepareIndex("test", "testtype").setId("1")
|
|
|
- .setSource("text", "value1").get();
|
|
|
- client().prepareIndex("test", "testtype").setId("2")
|
|
|
- .setSource("text", "value2").get();
|
|
|
+ index("test", "testtype", "1", jsonBuilder().startObject().field("text", "value1").endObject());
|
|
|
+ index("test", "testtype", "2", jsonBuilder().startObject().field("text", "value2").endObject());
|
|
|
refresh();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Settings nodeSettings(int nodeOrdinal) {
|
|
|
+ String scriptPath = this.getClass().getResource("config").getPath();
|
|
|
+ return settingsBuilder().put("path.conf", scriptPath).build();
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void testTemplateInBody() throws IOException {
|
|
|
Map<String, Object> vars = new HashMap<String, Object>();
|
|
@@ -57,7 +70,7 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|
|
"{\"match_{{template}}\": {}}\"", vars);
|
|
|
SearchResponse sr = client().prepareSearch().setQuery(builder)
|
|
|
.execute().actionGet();
|
|
|
- ElasticsearchAssertions.assertHitCount(sr, 2);
|
|
|
+ assertHitCount(sr, 2);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -68,7 +81,7 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|
|
"{\"match_all\": {}}\"", vars);
|
|
|
SearchResponse sr = client().prepareSearch().setQuery(builder)
|
|
|
.execute().actionGet();
|
|
|
- ElasticsearchAssertions.assertHitCount(sr, 2);
|
|
|
+ assertHitCount(sr, 2);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -80,7 +93,7 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|
|
"storedTemplate", vars);
|
|
|
SearchResponse sr = client().prepareSearch().setQuery(builder)
|
|
|
.execute().actionGet();
|
|
|
- ElasticsearchAssertions.assertHitCount(sr, 2);
|
|
|
+ assertHitCount(sr, 2);
|
|
|
|
|
|
}
|
|
|
|
|
@@ -88,37 +101,59 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|
|
public void testRawEscapedTemplate() throws IOException {
|
|
|
String query = "{\"template\": {\"query\": \"{\\\"match_{{template}}\\\": {}}\\\"\",\"params\" : {\"template\" : \"all\"}}}";
|
|
|
|
|
|
- SearchResponse sr = client().prepareSearch().setQuery(query)
|
|
|
- .execute().actionGet();
|
|
|
- ElasticsearchAssertions.assertHitCount(sr, 2);
|
|
|
+ SearchResponse sr = client().prepareSearch().setQuery(query).get();
|
|
|
+ assertHitCount(sr, 2);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testRawTemplate() throws IOException {
|
|
|
String query = "{\"template\": {\"query\": {\"match_{{template}}\": {}},\"params\" : {\"template\" : \"all\"}}}";
|
|
|
- SearchResponse sr = client().prepareSearch().setQuery(query)
|
|
|
- .execute().actionGet();
|
|
|
- ElasticsearchAssertions.assertHitCount(sr, 2);
|
|
|
+ SearchResponse sr = client().prepareSearch().setQuery(query).get();
|
|
|
+ assertHitCount(sr, 2);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testRawFSTemplate() throws IOException {
|
|
|
String query = "{\"template\": {\"query\": \"storedTemplate\",\"params\" : {\"template\" : \"all\"}}}";
|
|
|
|
|
|
- SearchResponse sr = client().prepareSearch().setQuery(query)
|
|
|
- .execute().actionGet();
|
|
|
- ElasticsearchAssertions.assertHitCount(sr, 2);
|
|
|
+ SearchResponse sr = client().prepareSearch().setQuery(query).get();
|
|
|
+ assertHitCount(sr, 2);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public Settings nodeSettings(int nodeOrdinal) {
|
|
|
- String scriptPath = this.getClass()
|
|
|
- .getResource("config").getPath();
|
|
|
+ @Test
|
|
|
+ public void testSearchRequestTemplateSource() throws Exception {
|
|
|
+ SearchRequest searchRequest = new SearchRequest();
|
|
|
+ searchRequest.indices("_all");
|
|
|
+
|
|
|
+ String query = "{ \"template\" : { \"query\": {\"match_{{template}}\": {} } }, \"params\" : { \"template\":\"all\" } }";
|
|
|
+ BytesReference bytesRef = new BytesArray(query);
|
|
|
+ searchRequest.templateSource(bytesRef, false);
|
|
|
+
|
|
|
+ SearchResponse searchResponse = client().search(searchRequest).get();
|
|
|
+ assertHitCount(searchResponse, 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testThatParametersCanBeSet() throws Exception {
|
|
|
+ index("test", "type", "1", jsonBuilder().startObject().field("theField", "foo").endObject());
|
|
|
+ index("test", "type", "2", jsonBuilder().startObject().field("theField", "foo 2").endObject());
|
|
|
+ index("test", "type", "3", jsonBuilder().startObject().field("theField", "foo 3").endObject());
|
|
|
+ index("test", "type", "4", jsonBuilder().startObject().field("theField", "foo 4").endObject());
|
|
|
+ index("test", "type", "5", jsonBuilder().startObject().field("otherField", "foo").endObject());
|
|
|
+ refresh();
|
|
|
+
|
|
|
+ Map<String, String> templateParams = Maps.newHashMap();
|
|
|
+ templateParams.put("mySize", "2");
|
|
|
+ templateParams.put("myField", "theField");
|
|
|
+ templateParams.put("myValue", "foo");
|
|
|
|
|
|
- Settings settings = ImmutableSettings
|
|
|
- .settingsBuilder()
|
|
|
- .put("path.conf", scriptPath).build();
|
|
|
+ SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").setTemplateName("full-query-template").setTemplateParams(templateParams).get();
|
|
|
+ assertHitCount(searchResponse, 4);
|
|
|
+ // size kicks in here...
|
|
|
+ assertThat(searchResponse.getHits().getHits().length, is(2));
|
|
|
|
|
|
- return settings;
|
|
|
+ templateParams.put("myField", "otherField");
|
|
|
+ searchResponse = client().prepareSearch("test").setTypes("type").setTemplateName("full-query-template").setTemplateParams(templateParams).get();
|
|
|
+ assertHitCount(searchResponse, 1);
|
|
|
}
|
|
|
}
|