|
@@ -1890,4 +1890,178 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest {
|
|
|
.setQuery(QueryBuilders.multiMatchQuery("value2", "field2^2").lenient(true)).get();
|
|
|
assertHitCount(searchResponse, 1l);
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testIndicesQuery() throws Exception {
|
|
|
+ createIndex("index1", "index2");
|
|
|
+ ensureGreen();
|
|
|
+
|
|
|
+ client().prepareIndex("index1", "type1").setId("1").setSource("text", "value").get();
|
|
|
+ client().prepareIndex("index2", "type2").setId("2").setSource("text", "value").get();
|
|
|
+ refresh();
|
|
|
+
|
|
|
+ SearchResponse response = client().prepareSearch("index1", "index2")
|
|
|
+ .setQuery(indicesQuery(matchQuery("text", "value"), "index1")
|
|
|
+ .noMatchQuery(matchQuery("text", "value"))).get();
|
|
|
+ assertHitCount(response, 2l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ assertThat(response.getHits().getAt(1).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+
|
|
|
+ response = client().prepareSearch("index1", "index2")
|
|
|
+ .setQuery(indicesQuery(matchQuery("text", "value"), "index1")).get();
|
|
|
+ assertHitCount(response, 2l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ assertThat(response.getHits().getAt(1).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+
|
|
|
+ response = client().prepareSearch("index1", "index2")
|
|
|
+ .setQuery(indicesQuery(matchQuery("text", "value"), "index1")
|
|
|
+ .noMatchQuery("all")).get();
|
|
|
+ assertHitCount(response, 2l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ assertThat(response.getHits().getAt(1).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+
|
|
|
+ response = client().prepareSearch("index1", "index2")
|
|
|
+ .setQuery(indicesQuery(matchQuery("text", "value"), "index1")
|
|
|
+ .noMatchQuery("none")).get();
|
|
|
+ assertHitCount(response, 1l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testIndicesFilter() throws Exception {
|
|
|
+ createIndex("index1", "index2");
|
|
|
+ ensureGreen();
|
|
|
+
|
|
|
+ client().prepareIndex("index1", "type1").setId("1").setSource("text", "value").get();
|
|
|
+ client().prepareIndex("index2", "type2").setId("2").setSource("text", "value").get();
|
|
|
+ refresh();
|
|
|
+
|
|
|
+ SearchResponse response = client().prepareSearch("index1", "index2")
|
|
|
+ .setFilter(indicesFilter(termFilter("text", "value"), "index1")
|
|
|
+ .noMatchFilter(termFilter("text", "value"))).get();
|
|
|
+ assertHitCount(response, 2l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ assertThat(response.getHits().getAt(1).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+
|
|
|
+ response = client().prepareSearch("index1", "index2")
|
|
|
+ .setFilter(indicesFilter(termFilter("text", "value"), "index1")).get();
|
|
|
+ assertHitCount(response, 2l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ assertThat(response.getHits().getAt(1).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+
|
|
|
+ response = client().prepareSearch("index1", "index2")
|
|
|
+ .setFilter(indicesFilter(termFilter("text", "value"), "index1")
|
|
|
+ .noMatchFilter("all")).get();
|
|
|
+ assertHitCount(response, 2l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ assertThat(response.getHits().getAt(1).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+
|
|
|
+ response = client().prepareSearch("index1", "index2")
|
|
|
+ .setFilter(indicesFilter(termFilter("text", "value"), "index1")
|
|
|
+ .noMatchFilter("none")).get();
|
|
|
+ assertHitCount(response, 1l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test // https://github.com/elasticsearch/elasticsearch/issues/2416
|
|
|
+ public void testIndicesQueryHideParsingExceptions() throws Exception {
|
|
|
+ client().admin().indices().prepareCreate("simple")
|
|
|
+ .addMapping("lone", jsonBuilder().startObject().startObject("lone").endObject().endObject())
|
|
|
+ .get();
|
|
|
+ client().admin().indices().prepareCreate("related")
|
|
|
+ .addMapping("parent", jsonBuilder().startObject().startObject("parent").endObject().endObject())
|
|
|
+ .addMapping("child", jsonBuilder().startObject().startObject("child").startObject("_parent").field("type", "parent")
|
|
|
+ .endObject().endObject().endObject())
|
|
|
+ .get();
|
|
|
+ ensureGreen();
|
|
|
+
|
|
|
+ client().prepareIndex("simple", "lone").setId("1").setSource("text", "value").get();
|
|
|
+ client().prepareIndex("related", "parent").setId("2").setSource("text", "parent").get();
|
|
|
+ client().prepareIndex("related", "child").setId("3").setParent("2").setSource("text", "value").get();
|
|
|
+ refresh();
|
|
|
+
|
|
|
+ SearchResponse response = client().prepareSearch("related")
|
|
|
+ .setQuery(hasChildQuery("child", matchQuery("text", "value"))).get();
|
|
|
+ assertHitCount(response, 1l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), equalTo("2"));
|
|
|
+
|
|
|
+ response = client().prepareSearch("simple")
|
|
|
+ .setQuery(matchQuery("text", "value")).get();
|
|
|
+ assertHitCount(response, 1l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
|
|
|
+
|
|
|
+ try {
|
|
|
+ client().prepareSearch("simple")
|
|
|
+ .setQuery(hasChildQuery("child", matchQuery("text", "value"))).get();
|
|
|
+ fail("Should have failed with a SearchPhaseExecutionException because all shards failed with a nested QueryParsingException");
|
|
|
+ // If no failure happens, the HasChildQuery may have changed behavior when provided with wrong types
|
|
|
+ } catch (SearchPhaseExecutionException e) {
|
|
|
+ // There is no easy way to ensure we got a QueryParsingException
|
|
|
+ }
|
|
|
+
|
|
|
+ response = client().prepareSearch("related", "simple")
|
|
|
+ .setQuery(indicesQuery(matchQuery("text", "parent"), "related")
|
|
|
+ .noMatchQuery(matchQuery("text", "value"))).get();
|
|
|
+ assertHitCount(response, 2l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ assertThat(response.getHits().getAt(1).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+
|
|
|
+ response = client().prepareSearch("related", "simple")
|
|
|
+ .setQuery(indicesQuery(hasChildQuery("child", matchQuery("text", "value")), "related")
|
|
|
+ .noMatchQuery(matchQuery("text", "value"))).get();
|
|
|
+ assertHitCount(response, 2l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ assertThat(response.getHits().getAt(1).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test // https://github.com/elasticsearch/elasticsearch/issues/2416
|
|
|
+ public void testIndicesFilterHideParsingExceptions() throws Exception {
|
|
|
+ client().admin().indices().prepareCreate("simple")
|
|
|
+ .addMapping("lone", jsonBuilder().startObject().startObject("lone").endObject().endObject())
|
|
|
+ .get();
|
|
|
+ client().admin().indices().prepareCreate("related")
|
|
|
+ .addMapping("parent", jsonBuilder().startObject().startObject("parent").endObject().endObject())
|
|
|
+ .addMapping("child", jsonBuilder().startObject().startObject("child").startObject("_parent").field("type", "parent")
|
|
|
+ .endObject().endObject().endObject())
|
|
|
+ .get();
|
|
|
+ ensureGreen();
|
|
|
+
|
|
|
+ client().prepareIndex("simple", "lone").setId("1").setSource("text", "value").get();
|
|
|
+ client().prepareIndex("related", "parent").setId("2").setSource("text", "parent").get();
|
|
|
+ client().prepareIndex("related", "child").setId("3").setParent("2").setSource("text", "value").get();
|
|
|
+ refresh();
|
|
|
+
|
|
|
+ SearchResponse response = client().prepareSearch("related")
|
|
|
+ .setFilter(hasChildFilter("child", termFilter("text", "value"))).get();
|
|
|
+ assertHitCount(response, 1l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), equalTo("2"));
|
|
|
+
|
|
|
+ response = client().prepareSearch("simple")
|
|
|
+ .setFilter(termFilter("text", "value")).get();
|
|
|
+ assertHitCount(response, 1l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
|
|
|
+
|
|
|
+ try {
|
|
|
+ client().prepareSearch("simple")
|
|
|
+ .setFilter(hasChildFilter("child", termFilter("text", "value"))).get();
|
|
|
+ fail("Should have failed with a SearchPhaseExecutionException because all shards failed with a nested QueryParsingException");
|
|
|
+ // If no failure happens, the HasChildQuery may have changed behavior when provided with wrong types
|
|
|
+ } catch (SearchPhaseExecutionException e) {
|
|
|
+ // There is no easy way to ensure we got a QueryParsingException
|
|
|
+ }
|
|
|
+
|
|
|
+ response = client().prepareSearch("related", "simple")
|
|
|
+ .setFilter(indicesFilter(termFilter("text", "parent"), "related")
|
|
|
+ .noMatchFilter(termFilter("text", "value"))).get();
|
|
|
+ assertHitCount(response, 2l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ assertThat(response.getHits().getAt(1).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+
|
|
|
+ response = client().prepareSearch("related", "simple")
|
|
|
+ .setFilter(indicesFilter(hasChildFilter("child", termFilter("text", "value")), "related")
|
|
|
+ .noMatchFilter(termFilter("text", "value"))).get();
|
|
|
+ assertHitCount(response, 2l);
|
|
|
+ assertThat(response.getHits().getAt(0).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ assertThat(response.getHits().getAt(1).getId(), either(equalTo("1")).or(equalTo("2")));
|
|
|
+ }
|
|
|
}
|