Skip to content

Commit

Permalink
Query DSL: indices query to allow to set a no_match_query, closes e…
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Nov 23, 2011
1 parent 7523f6e commit b46cc0d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Expand Up @@ -25,24 +25,49 @@

/**
* 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 {

private final QueryBuilder queryBuilder;

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();
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -50,12 +51,15 @@ public class IndicesQueryParser implements QueryParser {

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)) {
Expand All @@ -70,6 +74,13 @@ public class IndicesQueryParser implements QueryParser {
} 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;
}
}
}
}
Expand All @@ -84,6 +95,6 @@ public class IndicesQueryParser implements QueryParser {
return query;
}
}
return Queries.MATCH_ALL_QUERY;
return noMatchQuery;
}
}

0 comments on commit b46cc0d

Please sign in to comment.