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

This commit is contained in:
Shay Banon 2011-11-23 19:01:14 +02:00
parent e8d91a66b6
commit 2c0662e18e
2 changed files with 38 additions and 2 deletions

View File

@ -25,7 +25,7 @@ import java.io.IOException;
/**
* 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 @@ public class IndicesQueryBuilder extends BaseQueryBuilder {
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 File

@ -22,6 +22,7 @@ package org.elasticsearch.index.query;
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 @@ 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)) {
@ -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;
}
}
}
}
@ -84,6 +95,6 @@ public class IndicesQueryParser implements QueryParser {
return query;
}
}
return Queries.MATCH_ALL_QUERY;
return noMatchQuery;
}
}