Skip to content

Commit

Permalink
Java api: remove redundant BytesQueryBuilder in favour of using Wrapp…
Browse files Browse the repository at this point in the history
…erQueryBuilder 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 elastic#10919
  • Loading branch information
javanna committed May 8, 2015
1 parent a536bd5 commit 56ef4e5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 70 deletions.
4 changes: 4 additions & 0 deletions docs/reference/migration/migrate_2_0.asciidoc
Expand Up @@ -123,6 +123,10 @@ In addition some query builders have been removed or renamed:
* `filtered(...)` removed. Use `filteredQuery(...)` instead.
* `inQuery(...)` removed.

`BytesFilterBuilder` and the corresponding `FilterBuilders#bytesFilter` method have been
removed in favour of the existing `WrapperFilterBuilder`, which has now an additional
constructor that accepts a `BytesReference` as argument.

=== Aggregations

The `date_histogram` aggregation now returns a `Histogram` object in the response, and the `DateHistogram` class has been removed. Similarly
Expand Down
49 changes: 0 additions & 49 deletions src/main/java/org/elasticsearch/index/query/BytesQueryBuilder.java

This file was deleted.

18 changes: 8 additions & 10 deletions src/main/java/org/elasticsearch/index/query/QueryBuilders.java
Expand Up @@ -263,7 +263,7 @@ public static QueryStringQueryBuilder queryStringQuery(String queryString) {
/**
* 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) {
Expand Down Expand Up @@ -546,6 +546,13 @@ public static WrapperQueryBuilder wrapperQuery(String source) {
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.
*/
Expand Down Expand Up @@ -769,15 +776,6 @@ public static NotQueryBuilder notQuery(QueryBuilder filter) {
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
Expand Down
Expand Up @@ -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/>
Expand All @@ -51,20 +46,32 @@ 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);
this.offset = 0;
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);
Expand Down
Expand Up @@ -28,7 +28,10 @@
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;
Expand All @@ -42,7 +45,9 @@
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;
Expand Down Expand Up @@ -168,7 +173,7 @@ private MultiSearchResponse fetchMatchingDocCountResponses(Correction[] correcti
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 {
Expand Down

0 comments on commit 56ef4e5

Please sign in to comment.