Java api: remove redundant BytesQueryBuilder in favour of using WrapperQueryBuilder internally
BytesQueryBuilder was introduced to be used internally by the phrase suggester and its collate feature. It ended up being exposed via Java api but the existing WrapperQueryBuilder could be used instead. Added WrapperQueryBuilder constructor that accepts a BytesReference as argument. One other reason why this filter builder should be removed is that it gets on the way of the query parsers refactoring, given that it's the only query builder that allows to build a query through java api without having a respective query parser. Closes #10919
This commit is contained in:
parent
ba243e7a9d
commit
9e01dedef5
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch 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.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.xcontent.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* QueryBuilder that constructs queries from {@link org.elasticsearch.common.bytes.BytesReference}
|
||||
* source
|
||||
*/
|
||||
public class BytesQueryBuilder extends BaseQueryBuilder {
|
||||
|
||||
private final BytesReference source;
|
||||
|
||||
public BytesQueryBuilder(BytesReference source) {
|
||||
this.source = source;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
try (XContentParser parser = XContentFactory.xContent(source).createParser(source)) {
|
||||
// unwrap the first layer of json dictionary
|
||||
parser.nextToken();
|
||||
parser.nextToken();
|
||||
builder.copyCurrentStructure(parser);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -263,7 +263,7 @@ public abstract class QueryBuilders {
|
|||
/**
|
||||
* A query that acts similar to a query_string query, but won't throw
|
||||
* exceptions for any weird string syntax. See
|
||||
* {@link org.apache.lucene.queryparser.XSimpleQueryParser} for the full
|
||||
* {@link org.apache.lucene.queryparser.simple.SimpleQueryParser} for the full
|
||||
* supported syntax.
|
||||
*/
|
||||
public static SimpleQueryStringBuilder simpleQueryStringQuery(String queryString) {
|
||||
|
@ -546,6 +546,13 @@ public abstract class QueryBuilders {
|
|||
return new WrapperQueryBuilder(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Query builder which allows building a query thanks to a JSON string or binary data.
|
||||
*/
|
||||
public static WrapperQueryBuilder wrapperQuery(BytesReference source) {
|
||||
return new WrapperQueryBuilder(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Query builder which allows building a query thanks to a JSON string or binary data.
|
||||
*/
|
||||
|
@ -769,15 +776,6 @@ public abstract class QueryBuilders {
|
|||
return new NotQueryBuilder(filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a bytes filter to generate a filter from a {@link BytesReference} source
|
||||
*
|
||||
* @param source The filter source
|
||||
*/
|
||||
public static BytesQueryBuilder bytesQuery(BytesReference source) {
|
||||
return new BytesQueryBuilder(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link OrQueryBuilder} composed of the given filters.
|
||||
* @deprecated Use {@link #boolQuery()} instead
|
||||
|
|
|
@ -18,20 +18,15 @@
|
|||
*/
|
||||
|
||||
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.bytes.BytesReference;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A Query builder which allows building a query thanks to a JSON string or binary data. This is useful when you want
|
||||
* A Query builder which allows building a query given JSON string or binary data provided as input. This is useful when you want
|
||||
* to use the Java Builder API but still have JSON query strings at hand that you want to combine with other
|
||||
* query builders.
|
||||
* <p/>
|
||||
|
@ -51,7 +46,7 @@ public class WrapperQueryBuilder extends BaseQueryBuilder {
|
|||
private final int length;
|
||||
|
||||
/**
|
||||
* Builds a JSONQueryBuilder using the provided JSON query string.
|
||||
* Creates a query builder given a query provided as a string
|
||||
*/
|
||||
public WrapperQueryBuilder(String source) {
|
||||
this.source = source.getBytes(Charsets.UTF_8);
|
||||
|
@ -59,12 +54,24 @@ public class WrapperQueryBuilder extends BaseQueryBuilder {
|
|||
this.length = this.source.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a query builder given a query provided as a bytes array
|
||||
*/
|
||||
public WrapperQueryBuilder(byte[] source, int offset, int length) {
|
||||
this.source = source;
|
||||
this.offset = offset;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a query builder given a query provided as a {@link BytesReference}
|
||||
*/
|
||||
public WrapperQueryBuilder(BytesReference source) {
|
||||
this.source = source.array();
|
||||
this.offset = source.arrayOffset();
|
||||
this.length = source.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject(WrapperQueryParser.NAME);
|
||||
|
|
|
@ -28,7 +28,10 @@ import org.apache.lucene.util.BytesRef;
|
|||
import org.apache.lucene.util.BytesRefBuilder;
|
||||
import org.apache.lucene.util.CharsRefBuilder;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.search.*;
|
||||
import org.elasticsearch.action.search.MultiSearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.MultiSearchResponse;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
|
@ -42,7 +45,9 @@ import org.elasticsearch.script.ScriptService;
|
|||
import org.elasticsearch.search.suggest.Suggest.Suggestion;
|
||||
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry;
|
||||
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option;
|
||||
import org.elasticsearch.search.suggest.*;
|
||||
import org.elasticsearch.search.suggest.SuggestContextParser;
|
||||
import org.elasticsearch.search.suggest.SuggestUtils;
|
||||
import org.elasticsearch.search.suggest.Suggester;
|
||||
import org.elasticsearch.search.suggest.phrase.NoisyChannelSpellChecker.Result;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -168,7 +173,7 @@ public final class PhraseSuggester extends Suggester<PhraseSuggestionContext> {
|
|||
if (isFilter) {
|
||||
req = client.prepareSearch()
|
||||
.setPreference(suggestions.getPreference())
|
||||
.setQuery(QueryBuilders.constantScoreQuery(QueryBuilders.bytesQuery(querySource)))
|
||||
.setQuery(QueryBuilders.constantScoreQuery(QueryBuilders.wrapperQuery(querySource)))
|
||||
.setSize(0)
|
||||
.setTerminateAfter(1);
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue