Skip to content

Commit

Permalink
Java api: remove redundant BytesFilterBuilder in favour of using Wrap…
Browse files Browse the repository at this point in the history
…perFilterBuilder internally

BytesFilterBuilder 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 WrapperFilterBuilder could be used instead. Added WrapperFilterBuilder 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 filter builder that allows to build a filter through java api without having a respective filter parser.
  • Loading branch information
javanna committed May 1, 2015
1 parent c165afb commit c2e28dc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 70 deletions.
4 changes: 4 additions & 0 deletions docs/reference/migration/migrate_2_0.asciidoc
Expand Up @@ -116,6 +116,10 @@ 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 accepts a `BytesReference`dde
too as constructor argument.

=== Aggregations

The `date_histogram` aggregation now returns a `Histogram` object in the response, and the `DateHistogram` class has been removed. Similarly
Expand Down

This file was deleted.

23 changes: 13 additions & 10 deletions src/main/java/org/elasticsearch/index/query/FilterBuilders.java
Expand Up @@ -29,7 +29,10 @@
/**
* A static factory for simple "import static" usage.
*/
public abstract class FilterBuilders {
public final class FilterBuilders {

private FilterBuilders() {
}

/**
* A filter that matches all documents.
Expand Down Expand Up @@ -548,24 +551,24 @@ public static IndicesFilterBuilder indicesFilter(FilterBuilder filter, String...
return new IndicesFilterBuilder(filter, indices);
}

/**
* Creates a {@link WrapperFilterBuilder} given a filter provided as a string
*/
public static WrapperFilterBuilder wrapperFilter(String filter) {
return new WrapperFilterBuilder(filter);
}

/**
* Creates a {@link WrapperFilterBuilder} given a filter provided as a bytes array
*/
public static WrapperFilterBuilder wrapperFilter(byte[] data, int offset, int length) {
return new WrapperFilterBuilder(data, offset, length);
}

/**
* Constructs a bytes filter to generate a filter from a {@link BytesReference} source
*
* @param source The filter source
* Creates a {@link WrapperFilterBuilder} given a filter provided as a {@link BytesReference}
*/
public static BytesFilterBuilder bytesFilter(BytesReference source) {
return new BytesFilterBuilder(source);
}

private FilterBuilders() {

public static WrapperFilterBuilder wrapperFilter(BytesReference source) {
return new WrapperFilterBuilder(source);
}
}
Expand Up @@ -18,21 +18,16 @@
*/

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 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
* A Filter builder which allows building a filter given JSON string or binary data provided as input. This is useful
* when you want to use the Java API but still have JSON filter strings at hand that you want to combine with other
* query builders.
*/
public class WrapperFilterBuilder extends BaseFilterBuilder {
Expand All @@ -41,18 +36,33 @@ public class WrapperFilterBuilder extends BaseFilterBuilder {
private final int offset;
private final int length;

/**
* Creates a filter builder given a filter provided as a string
*/
public WrapperFilterBuilder(String source) {
this.source = source.getBytes(Charsets.UTF_8);
this.offset = 0;
this.length = this.source.length;
}

/**
* Creates a filter builder given a byte provided as a bytes array
*/
public WrapperFilterBuilder(byte[] source, int offset, int length) {
this.source = source;
this.offset = offset;
this.length = length;
}

/**
* Creates a filter builder given a filter provided as a {@link BytesReference}
*/
public WrapperFilterBuilder(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(WrapperFilterParser.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(FilterBuilders.bytesFilter(querySource)))
.setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.wrapperFilter(querySource)))
.setSize(0)
.setTerminateAfter(1);
} else {
Expand Down

0 comments on commit c2e28dc

Please sign in to comment.