Query DSL: Indices query type, closes #1416.

This commit is contained in:
Shay Banon 2011-10-23 01:35:09 +02:00
parent 721f315210
commit b2b608f9dc
4 changed files with 142 additions and 7 deletions

View File

@ -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();
}
}

View File

@ -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<String> 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;
}
}

View File

@ -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);
}
/**

View File

@ -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<String, FilterParser> filterParsers = Maps.newHashMap();