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,19 +57,29 @@ public class AndFilterParser extends AbstractIndexComponent implements XContentF
|
|||
boolean cache = true;
|
||||
|
||||
String currentFieldName = null;
|
||||
XContentParser.Token token;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||
if ("filters".equals(currentFieldName)) {
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
filters.add(parseContext.parseInnerFilter());
|
||||
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) {
|
||||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||
if ("filters".equals(currentFieldName)) {
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
filters.add(parseContext.parseInnerFilter());
|
||||
}
|
||||
} else {
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
filters.add(parseContext.parseInnerFilter());
|
||||
}
|
||||
}
|
||||
} else if (token.isValue()) {
|
||||
if ("cache".equals(currentFieldName)) {
|
||||
cache = parser.booleanValue();
|
||||
}
|
||||
}
|
||||
} else if (token.isValue()) {
|
||||
if ("cache".equals(currentFieldName)) {
|
||||
cache = parser.booleanValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,19 +57,29 @@ public class OrFilterParser extends AbstractIndexComponent implements XContentFi
|
|||
boolean cache = true;
|
||||
|
||||
String currentFieldName = null;
|
||||
XContentParser.Token token;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||
if ("filters".equals(currentFieldName)) {
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
filters.add(parseContext.parseInnerFilter());
|
||||
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) {
|
||||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||
if ("filters".equals(currentFieldName)) {
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
filters.add(parseContext.parseInnerFilter());
|
||||
}
|
||||
} else {
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
filters.add(parseContext.parseInnerFilter());
|
||||
}
|
||||
}
|
||||
} else if (token.isValue()) {
|
||||
if ("cache".equals(currentFieldName)) {
|
||||
cache = parser.booleanValue();
|
||||
}
|
||||
}
|
||||
} else if (token.isValue()) {
|
||||
if ("cache".equals(currentFieldName)) {
|
||||
cache = parser.booleanValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -118,14 +118,14 @@ public class QueryParseContext {
|
|||
String queryName = parser.currentName();
|
||||
// move to the next START_OBJECT
|
||||
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);
|
||||
if (queryParser == null) {
|
||||
throw new QueryParsingException(index, "No query parser registered for [" + queryName + "]");
|
||||
}
|
||||
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...
|
||||
parser.nextToken();
|
||||
}
|
||||
|
@ -142,16 +142,16 @@ public class QueryParseContext {
|
|||
token = parser.nextToken();
|
||||
assert token == XContentParser.Token.FIELD_NAME;
|
||||
String queryName = parser.currentName();
|
||||
// move to the next START_OBJECT
|
||||
// move to the next START_OBJECT or START_ARRAY
|
||||
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);
|
||||
if (filterParser == null) {
|
||||
throw new QueryParsingException(index, "No query parser registered for [" + queryName + "]");
|
||||
}
|
||||
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...
|
||||
parser.nextToken();
|
||||
}
|
||||
|
|
|
@ -519,6 +519,19 @@ public class SimpleIndexQueryParserTests {
|
|||
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 {
|
||||
IndexQueryParser queryParser = newQueryParser();
|
||||
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")));
|
||||
}
|
||||
|
||||
@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 {
|
||||
IndexQueryParser queryParser = newQueryParser();
|
||||
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