Query DSL: Improve `or` and `and` filters to accept just array of filters, closes #336.
This commit is contained in:
parent
c7a36ed431
commit
9d615a4f0b
|
@ -57,7 +57,12 @@ public class AndFilterParser extends AbstractIndexComponent implements XContentF
|
||||||
boolean cache = true;
|
boolean cache = true;
|
||||||
|
|
||||||
String currentFieldName = null;
|
String currentFieldName = null;
|
||||||
XContentParser.Token token;
|
XContentParser.Token token = parser.currentToken();
|
||||||
|
if (token == XContentParser.Token.START_ARRAY) {
|
||||||
|
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||||
|
filters.add(parseContext.parseInnerFilter());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
if (token == XContentParser.Token.FIELD_NAME) {
|
if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
currentFieldName = parser.currentName();
|
currentFieldName = parser.currentName();
|
||||||
|
@ -66,6 +71,10 @@ public class AndFilterParser extends AbstractIndexComponent implements XContentF
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||||
filters.add(parseContext.parseInnerFilter());
|
filters.add(parseContext.parseInnerFilter());
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||||
|
filters.add(parseContext.parseInnerFilter());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (token.isValue()) {
|
} else if (token.isValue()) {
|
||||||
if ("cache".equals(currentFieldName)) {
|
if ("cache".equals(currentFieldName)) {
|
||||||
|
@ -73,6 +82,7 @@ public class AndFilterParser extends AbstractIndexComponent implements XContentF
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (filters.isEmpty()) {
|
if (filters.isEmpty()) {
|
||||||
throw new QueryParsingException(index, "[or] filter requires 'filters' to be set on it'");
|
throw new QueryParsingException(index, "[or] filter requires 'filters' to be set on it'");
|
||||||
|
|
|
@ -57,7 +57,12 @@ public class OrFilterParser extends AbstractIndexComponent implements XContentFi
|
||||||
boolean cache = true;
|
boolean cache = true;
|
||||||
|
|
||||||
String currentFieldName = null;
|
String currentFieldName = null;
|
||||||
XContentParser.Token token;
|
XContentParser.Token token = parser.currentToken();
|
||||||
|
if (token == XContentParser.Token.START_ARRAY) {
|
||||||
|
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||||
|
filters.add(parseContext.parseInnerFilter());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||||
if (token == XContentParser.Token.FIELD_NAME) {
|
if (token == XContentParser.Token.FIELD_NAME) {
|
||||||
currentFieldName = parser.currentName();
|
currentFieldName = parser.currentName();
|
||||||
|
@ -66,6 +71,10 @@ public class OrFilterParser extends AbstractIndexComponent implements XContentFi
|
||||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||||
filters.add(parseContext.parseInnerFilter());
|
filters.add(parseContext.parseInnerFilter());
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||||
|
filters.add(parseContext.parseInnerFilter());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (token.isValue()) {
|
} else if (token.isValue()) {
|
||||||
if ("cache".equals(currentFieldName)) {
|
if ("cache".equals(currentFieldName)) {
|
||||||
|
@ -73,6 +82,7 @@ public class OrFilterParser extends AbstractIndexComponent implements XContentFi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (filters.isEmpty()) {
|
if (filters.isEmpty()) {
|
||||||
throw new QueryParsingException(index, "[or] filter requires 'filters' to be set on it'");
|
throw new QueryParsingException(index, "[or] filter requires 'filters' to be set on it'");
|
||||||
|
|
|
@ -118,14 +118,14 @@ public class QueryParseContext {
|
||||||
String queryName = parser.currentName();
|
String queryName = parser.currentName();
|
||||||
// move to the next START_OBJECT
|
// move to the next START_OBJECT
|
||||||
token = parser.nextToken();
|
token = parser.nextToken();
|
||||||
assert token == XContentParser.Token.START_OBJECT;
|
assert token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY;
|
||||||
|
|
||||||
XContentQueryParser queryParser = queryParserRegistry.queryParser(queryName);
|
XContentQueryParser queryParser = queryParserRegistry.queryParser(queryName);
|
||||||
if (queryParser == null) {
|
if (queryParser == null) {
|
||||||
throw new QueryParsingException(index, "No query parser registered for [" + queryName + "]");
|
throw new QueryParsingException(index, "No query parser registered for [" + queryName + "]");
|
||||||
}
|
}
|
||||||
Query result = queryParser.parse(this);
|
Query result = queryParser.parse(this);
|
||||||
if (parser.currentToken() == XContentParser.Token.END_OBJECT) {
|
if (parser.currentToken() == XContentParser.Token.END_OBJECT || parser.currentToken() == XContentParser.Token.END_ARRAY) {
|
||||||
// if we are at END_OBJECT, move to the next one...
|
// if we are at END_OBJECT, move to the next one...
|
||||||
parser.nextToken();
|
parser.nextToken();
|
||||||
}
|
}
|
||||||
|
@ -142,16 +142,16 @@ public class QueryParseContext {
|
||||||
token = parser.nextToken();
|
token = parser.nextToken();
|
||||||
assert token == XContentParser.Token.FIELD_NAME;
|
assert token == XContentParser.Token.FIELD_NAME;
|
||||||
String queryName = parser.currentName();
|
String queryName = parser.currentName();
|
||||||
// move to the next START_OBJECT
|
// move to the next START_OBJECT or START_ARRAY
|
||||||
token = parser.nextToken();
|
token = parser.nextToken();
|
||||||
assert token == XContentParser.Token.START_OBJECT;
|
assert token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY;
|
||||||
|
|
||||||
XContentFilterParser filterParser = queryParserRegistry.filterParser(queryName);
|
XContentFilterParser filterParser = queryParserRegistry.filterParser(queryName);
|
||||||
if (filterParser == null) {
|
if (filterParser == null) {
|
||||||
throw new QueryParsingException(index, "No query parser registered for [" + queryName + "]");
|
throw new QueryParsingException(index, "No query parser registered for [" + queryName + "]");
|
||||||
}
|
}
|
||||||
Filter result = filterParser.parse(this);
|
Filter result = filterParser.parse(this);
|
||||||
if (parser.currentToken() == XContentParser.Token.END_OBJECT) {
|
if (parser.currentToken() == XContentParser.Token.END_OBJECT || parser.currentToken() == XContentParser.Token.END_ARRAY) {
|
||||||
// if we are at END_OBJECT, move to the next one...
|
// if we are at END_OBJECT, move to the next one...
|
||||||
parser.nextToken();
|
parser.nextToken();
|
||||||
}
|
}
|
||||||
|
|
|
@ -519,6 +519,19 @@ public class SimpleIndexQueryParserTests {
|
||||||
assertThat(((TermFilter) andFilter.filters().get(1)).getTerm(), equalTo(new Term("name.first", "shay4")));
|
assertThat(((TermFilter) andFilter.filters().get(1)).getTerm(), equalTo(new Term("name.first", "shay4")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void testAndFilteredQuery2() throws IOException {
|
||||||
|
IndexQueryParser queryParser = newQueryParser();
|
||||||
|
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/xcontent/and-filter2.json");
|
||||||
|
Query parsedQuery = queryParser.parse(query);
|
||||||
|
assertThat(parsedQuery, instanceOf(FilteredQuery.class));
|
||||||
|
FilteredQuery filteredQuery = (FilteredQuery) parsedQuery;
|
||||||
|
|
||||||
|
AndFilter andFilter = (AndFilter) filteredQuery.getFilter();
|
||||||
|
assertThat(andFilter.filters().size(), equalTo(2));
|
||||||
|
assertThat(((TermFilter) andFilter.filters().get(0)).getTerm(), equalTo(new Term("name.first", "shay1")));
|
||||||
|
assertThat(((TermFilter) andFilter.filters().get(1)).getTerm(), equalTo(new Term("name.first", "shay4")));
|
||||||
|
}
|
||||||
|
|
||||||
@Test public void testOrFilteredQueryBuilder() throws IOException {
|
@Test public void testOrFilteredQueryBuilder() throws IOException {
|
||||||
IndexQueryParser queryParser = newQueryParser();
|
IndexQueryParser queryParser = newQueryParser();
|
||||||
Query parsedQuery = queryParser.parse(filtered(matchAllQuery(), orFilter(termFilter("name.first", "shay1"), termFilter("name.first", "shay4"))));
|
Query parsedQuery = queryParser.parse(filtered(matchAllQuery(), orFilter(termFilter("name.first", "shay1"), termFilter("name.first", "shay4"))));
|
||||||
|
@ -544,6 +557,19 @@ public class SimpleIndexQueryParserTests {
|
||||||
assertThat(((TermFilter) orFilter.filters().get(1)).getTerm(), equalTo(new Term("name.first", "shay4")));
|
assertThat(((TermFilter) orFilter.filters().get(1)).getTerm(), equalTo(new Term("name.first", "shay4")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void testOrFilteredQuery2() throws IOException {
|
||||||
|
IndexQueryParser queryParser = newQueryParser();
|
||||||
|
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/xcontent/or-filter2.json");
|
||||||
|
Query parsedQuery = queryParser.parse(query);
|
||||||
|
assertThat(parsedQuery, instanceOf(FilteredQuery.class));
|
||||||
|
FilteredQuery filteredQuery = (FilteredQuery) parsedQuery;
|
||||||
|
|
||||||
|
OrFilter orFilter = (OrFilter) filteredQuery.getFilter();
|
||||||
|
assertThat(orFilter.filters().size(), equalTo(2));
|
||||||
|
assertThat(((TermFilter) orFilter.filters().get(0)).getTerm(), equalTo(new Term("name.first", "shay1")));
|
||||||
|
assertThat(((TermFilter) orFilter.filters().get(1)).getTerm(), equalTo(new Term("name.first", "shay4")));
|
||||||
|
}
|
||||||
|
|
||||||
@Test public void testNotFilteredQueryBuilder() throws IOException {
|
@Test public void testNotFilteredQueryBuilder() throws IOException {
|
||||||
IndexQueryParser queryParser = newQueryParser();
|
IndexQueryParser queryParser = newQueryParser();
|
||||||
Query parsedQuery = queryParser.parse(filtered(matchAllQuery(), notFilter(termFilter("name.first", "shay1"))));
|
Query parsedQuery = queryParser.parse(filtered(matchAllQuery(), notFilter(termFilter("name.first", "shay1"))));
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"filtered" : {
|
||||||
|
"query" : {
|
||||||
|
"term" : { "name.first" : "shay" }
|
||||||
|
},
|
||||||
|
"filter" : {
|
||||||
|
"and" : [
|
||||||
|
{
|
||||||
|
"term" : { "name.first" : "shay1" }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"term" : { "name.first" : "shay4" }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"filtered" : {
|
||||||
|
"query" : {
|
||||||
|
"term" : { "name.first" : "shay" }
|
||||||
|
},
|
||||||
|
"filter" : {
|
||||||
|
"or" : [
|
||||||
|
{
|
||||||
|
"term" : { "name.first" : "shay1" }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"term" : { "name.first" : "shay4" }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue