Java API Query DSL: Add wrapper filter similar to wrapper query accepting a json filter in raw format, closes #1844.

This commit is contained in:
Shay Banon 2012-04-04 19:53:17 +03:00
parent fb93bd13dd
commit b78680c7ae
6 changed files with 149 additions and 2 deletions

View File

@ -387,6 +387,14 @@ public abstract class FilterBuilders {
return new IndicesFilterBuilder(filter, indices);
}
public static WrapperFilterBuilder wrapperFilter(String filter) {
return new WrapperFilterBuilder(filter);
}
public static WrapperFilterBuilder wrapperFilter(byte[] data, int offset, int length) {
return new WrapperFilterBuilder(data, offset, length);
}
private FilterBuilders() {
}

View File

@ -0,0 +1,62 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch 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;
/**
* Created by IntelliJ IDEA.
* User: cedric
* Date: 12/07/11
* Time: 11:30
*/
import com.google.common.base.Charsets;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
/**
* A Filter builder which allows building a filter thanks to a JSON string or binary data. This is useful when you want
* to use the Java Builder API but still have JSON filter strings at hand that you want to combine with other
* query builders.
*/
public class WrapperFilterBuilder extends BaseFilterBuilder {
private final byte[] source;
private final int offset;
private final int length;
public WrapperFilterBuilder(String source) {
this.source = source.getBytes(Charsets.UTF_8);
this.offset = 0;
this.length = this.source.length;
}
public WrapperFilterBuilder(byte[] source, int offset, int length) {
this.source = source;
this.offset = offset;
this.length = length;
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(WrapperFilterParser.NAME);
builder.field("filter", source, offset, length);
builder.endObject();
}
}

View File

@ -0,0 +1,71 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch 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.Filter;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException;
/**
* Filter parser for embedded filter.
*/
public class WrapperFilterParser implements FilterParser {
public static final String NAME = "wrapper";
@Inject
public WrapperFilterParser() {
}
@Override
public String[] names() {
return new String[]{NAME};
}
@Override
public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
XContentParser.Token token = parser.nextToken();
if (token != XContentParser.Token.FIELD_NAME) {
throw new QueryParsingException(parseContext.index(), "[wrapper] filter malformed");
}
String fieldName = parser.currentName();
if (!fieldName.equals("filter")) {
throw new QueryParsingException(parseContext.index(), "[wrapper] filter malformed");
}
parser.nextToken();
byte[] querySource = parser.binaryValue();
XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource);
try {
final QueryParseContext context = new QueryParseContext(parseContext.index(), parseContext.indexQueryParser);
context.reset(qSourceParser);
Filter result = context.parseInnerFilter();
parser.nextToken();
return result;
} finally {
qSourceParser.close();
}
}
}

View File

@ -56,7 +56,7 @@ public class WrapperQueryBuilder extends BaseQueryBuilder {
public WrapperQueryBuilder(String source) {
this.source = source.getBytes(Charsets.UTF_8);
this.offset = 0;
this.length = source.length();
this.length = this.source.length;
}
public WrapperQueryBuilder(byte[] source, int offset, int length) {

View File

@ -101,6 +101,7 @@ public class IndicesQueriesRegistry {
addFilterParser(filterParsers, new ExistsFilterParser());
addFilterParser(filterParsers, new MissingFilterParser());
addFilterParser(filterParsers, new IndicesFilterParser(clusterService));
addFilterParser(filterParsers, new WrapperFilterParser());
this.filterParsers = ImmutableMap.copyOf(filterParsers);
}

View File

@ -24,6 +24,7 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WrapperFilterBuilder;
import org.elasticsearch.index.query.WrapperQueryBuilder;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterClass;
@ -294,7 +295,7 @@ public class SimpleQueryTests extends AbstractNodesTests {
}
@Test
public void passQueryAsJSONStringTest() throws Exception {
public void passQueryOrFilterAsJSONStringTest() throws Exception {
try {
client.admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
@ -316,6 +317,10 @@ public class SimpleQueryTests extends AbstractNodesTests {
searchResponse = client.prepareSearch().setQuery(wrapper).execute().actionGet();
assertThat(searchResponse.hits().totalHits(), equalTo(1l));
WrapperFilterBuilder wrapperFilter = new WrapperFilterBuilder("{ \"term\" : { \"field1\" : \"value1_1\" } }");
searchResponse = client.prepareSearch().setFilter(wrapperFilter).execute().actionGet();
assertThat(searchResponse.hits().totalHits(), equalTo(1l));
}
@Test