Skip to content
Browse files

Query DSL: indices query to allow to set a `no_match_query`, closes #…

  • Loading branch information...
1 parent 531161e commit 97a92151058e4beb02c88581162681e606aa0421 @kimchy kimchy committed
View
27 modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java
@@ -25,7 +25,7 @@
/**
* A query that will execute the wrapped query only for the specified indices, and "match_all" when
- * it does not match those indices.
+ * it does not match those indices (by default).
*/
public class IndicesQueryBuilder extends BaseQueryBuilder {
@@ -33,16 +33,41 @@
private final String[] indices;
+ private String sNoMatchQuery;
+ private QueryBuilder noMatchQuery;
+
public IndicesQueryBuilder(QueryBuilder queryBuilder, String... indices) {
this.queryBuilder = queryBuilder;
this.indices = indices;
}
+ /**
+ * Sets the no match query, can either be <tt>all</tt> or <tt>none</tt>.
+ */
+ public IndicesQueryBuilder noMatchQuery(String type) {
+ this.sNoMatchQuery = type;
+ return this;
+ }
+
+ /**
+ * Sets the query to use when it executes on an index that does not match the indices provided.
+ */
+ public IndicesQueryBuilder noMatchQuery(QueryBuilder noMatchQuery) {
+ this.noMatchQuery = noMatchQuery;
+ return this;
+ }
+
@Override protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(IndicesQueryParser.NAME);
builder.field("query");
queryBuilder.toXContent(builder, params);
builder.field("indices", indices);
+ if (noMatchQuery != null) {
+ builder.field("no_match_query");
+ noMatchQuery.toXContent(builder, params);
+ } else if (sNoMatchQuery != null) {
+ builder.field("no_match_query", sNoMatchQuery);
+ }
builder.endObject();
}
}
View
13 modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryParser.java
@@ -22,6 +22,7 @@
import org.apache.lucene.search.Query;
import org.elasticsearch.common.collect.Sets;
import org.elasticsearch.common.inject.Inject;
+import org.elasticsearch.common.lucene.search.MatchNoDocsQuery;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.xcontent.XContentParser;
@@ -50,12 +51,15 @@
String currentFieldName = null;
XContentParser.Token token;
+ Query noMatchQuery = Queries.MATCH_ALL_QUERY;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if ("query".equals(currentFieldName)) {
query = parseContext.parseInnerQuery();
+ } else if ("no_match_query".equals(currentFieldName)) {
+ noMatchQuery = parseContext.parseInnerQuery();
}
} else if (token == XContentParser.Token.START_ARRAY) {
if ("indices".equals(currentFieldName)) {
@@ -70,6 +74,13 @@
} else if (token.isValue()) {
if ("index".equals(currentFieldName)) {
indices.add(parser.text());
+ } else if ("no_match_query".equals(currentFieldName)) {
+ String type = parser.text();
+ if ("all".equals(type)) {
+ noMatchQuery = Queries.MATCH_ALL_QUERY;
+ } else if ("none".equals(type)) {
+ noMatchQuery = MatchNoDocsQuery.INSTANCE;
+ }
}
}
}
@@ -84,6 +95,6 @@
return query;
}
}
- return Queries.MATCH_ALL_QUERY;
+ return noMatchQuery;
}
}

0 comments on commit 97a9215

Please sign in to comment.
Something went wrong with that request. Please try again.