From 2c0662e18eff57f2354dc356196d5eb67d414908 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Wed, 23 Nov 2011 19:01:14 +0200 Subject: [PATCH] Query DSL: indices query to allow to set a `no_match_query`, closes #1492. --- .../index/query/IndicesQueryBuilder.java | 27 ++++++++++++++++++- .../index/query/IndicesQueryParser.java | 13 ++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java index de87123bac2..07d5e4755e8 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java @@ -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 all or none. + */ + 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(); } } \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryParser.java index 34ee03a81ec..5a2e3c3ec12 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryParser.java @@ -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; } }