Skip to content

Commit

Permalink
[Query] QueryParser can return null from a query
Browse files Browse the repository at this point in the history
This causes a NPE since XContentStructure checks if the query is null
and takes this as the condition to parse from the byte source which is
actually null in that case.

Closes #6722
  • Loading branch information
s1monw committed Jul 4, 2014
1 parent da1a4cf commit 5ca1715
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Expand Up @@ -127,13 +127,14 @@ public Query asFilter(String... types) throws IOException {
*/
public static class InnerQuery extends XContentStructure {
private Query query = null;

private boolean queryParsed = false;
public InnerQuery(QueryParseContext parseContext1, @Nullable String... types) throws IOException {
super(parseContext1);
if (types != null) {
String[] origTypes = QueryParseContext.setTypesWithPrevious(types);
try {
query = parseContext1.parseInnerQuery();
queryParsed = true;
} finally {
QueryParseContext.setTypes(origTypes);
}
Expand All @@ -150,7 +151,7 @@ public InnerQuery(QueryParseContext parseContext1, @Nullable String... types) th
*/
@Override
public Query asQuery(String... types) throws IOException {
if (this.query == null) {
if (!queryParsed) { // query can be null
this.query = super.asQuery(types);
}
return this.query;
Expand All @@ -164,6 +165,8 @@ public Query asQuery(String... types) throws IOException {
*/
public static class InnerFilter extends XContentStructure {
private Query query = null;
private boolean queryParsed = false;


public InnerFilter(QueryParseContext parseContext1, @Nullable String... types) throws IOException {
super(parseContext1);
Expand All @@ -172,6 +175,7 @@ public InnerFilter(QueryParseContext parseContext1, @Nullable String... types) t
try {
Filter innerFilter = parseContext1.parseInnerFilter();
query = new XConstantScoreQuery(innerFilter);
queryParsed = true;
} finally {
QueryParseContext.setTypes(origTypes);
}
Expand All @@ -190,7 +194,7 @@ public InnerFilter(QueryParseContext parseContext1, @Nullable String... types) t
*/
@Override
public Query asFilter(String... types) throws IOException {
if (this.query == null) {
if (!queryParsed) { // query can be null
this.query = super.asFilter(types);
}
return this.query;
Expand Down
Expand Up @@ -125,6 +125,24 @@ public void multiLevelChild() throws Exception {
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("gc1"));
}

@Test
// see #6722
public void test6722() throws ElasticsearchException, IOException {
assertAcked(prepareCreate("test")
.addMapping("foo")
.addMapping("test", "_parent", "type=foo"));
ensureGreen();

// index simple data
client().prepareIndex("test", "foo", "1").setSource("foo", 1).get();
client().prepareIndex("test", "test").setSource("foo", 1).setParent("1").get();
refresh();

SearchResponse searchResponse = client().prepareSearch("test").setSource("{\"query\":{\"filtered\":{\"filter\":{\"has_parent\":{\"type\":\"test\",\"query\":{\"bool\":{\"must\":[],\"must_not\":[],\"should\":[]}}},\"query\":[]}}}}").get();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().totalHits(), equalTo(2l));
}

@Test
// see #2744
public void test2744() throws ElasticsearchException, IOException {
Expand Down

0 comments on commit 5ca1715

Please sign in to comment.