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 new file mode 100644 index 00000000000..0a6187132c2 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java @@ -0,0 +1,48 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.query; + +import org.elasticsearch.common.xcontent.XContentBuilder; + +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. + */ +public class IndicesQueryBuilder extends BaseQueryBuilder { + + private final QueryBuilder queryBuilder; + + private final String[] indices; + + public IndicesQueryBuilder(QueryBuilder queryBuilder, String... indices) { + this.queryBuilder = queryBuilder; + this.indices = indices; + } + + @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(CustomBoostFactorQueryParser.NAME); + builder.field("query"); + queryBuilder.toXContent(builder, params); + builder.field("indices", indices); + 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 new file mode 100644 index 00000000000..34ee03a81ec --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/IndicesQueryParser.java @@ -0,0 +1,89 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +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.Queries; +import org.elasticsearch.common.regex.Regex; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; +import java.util.Set; + +/** + */ +public class IndicesQueryParser implements QueryParser { + + public static final String NAME = "indices"; + + @Inject public IndicesQueryParser() { + } + + @Override public String[] names() { + return new String[]{NAME}; + } + + @Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { + XContentParser parser = parseContext.parser(); + + Query query = null; + Set indices = Sets.newHashSet(); + + 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_OBJECT) { + if ("query".equals(currentFieldName)) { + query = parseContext.parseInnerQuery(); + } + } else if (token == XContentParser.Token.START_ARRAY) { + if ("indices".equals(currentFieldName)) { + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + String value = parser.textOrNull(); + if (value == null) { + throw new QueryParsingException(parseContext.index(), "No value specified for term filter"); + } + indices.add(value); + } + } + } else if (token.isValue()) { + if ("index".equals(currentFieldName)) { + indices.add(parser.text()); + } + } + } + if (query == null) { + throw new QueryParsingException(parseContext.index(), "[indices] requires 'query' element"); + } + if (indices.isEmpty()) { + throw new QueryParsingException(parseContext.index(), "[indices] requires 'indices' element"); + } + for (String index : indices) { + if (Regex.simpleMatch(index, parseContext.index().name())) { + return query; + } + } + return Queries.MATCH_ALL_QUERY; + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/QueryBuilders.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/QueryBuilders.java index 7901bf4ee63..a7055de3d2c 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/QueryBuilders.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/QueryBuilders.java @@ -638,14 +638,11 @@ public abstract class QueryBuilders { } /** - * A filter that restricts search results to values that have a matching prefix in a given - * field. - * - * @param name The field name - * @param prefix The prefix + * A query that will execute the wrapped query only for the specified indices, and "match_all" when + * it does not match those indices. */ - public static PrefixFilterBuilder inQuery(String name, String prefix) { - return new PrefixFilterBuilder(name, prefix); + public static IndicesQueryBuilder indicesQuery(QueryBuilder queryBuilder, String... indices) { + return new IndicesQueryBuilder(queryBuilder, indices); } /** diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java b/modules/elasticsearch/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java index ecf2393967e..a24f3ed98d5 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java @@ -69,6 +69,7 @@ public class IndicesQueriesRegistry { addQueryParser(queryParsers, new FuzzyLikeThisQueryParser()); addQueryParser(queryParsers, new FuzzyLikeThisFieldQueryParser()); addQueryParser(queryParsers, new WrapperQueryParser()); + addQueryParser(queryParsers, new IndicesQueryParser()); this.queryParsers = ImmutableMap.copyOf(queryParsers); Map filterParsers = Maps.newHashMap();