various fixes after a review

This commit is contained in:
Colin Goodheart-Smithe 2015-09-15 09:41:13 +01:00
parent e73052bce2
commit eed6ab7b82
1 changed files with 179 additions and 25 deletions

View File

@ -64,7 +64,10 @@ import java.util.Objects;
*
* @see org.elasticsearch.action.search.SearchRequest#source(NewSearchSourceBuilder)
*/
public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeable<NewSearchSourceBuilder> {
/**
*
*/
public final class NewSearchSourceBuilder extends ToXContentToBytes implements Writeable<NewSearchSourceBuilder> {
public static final ParseField FROM_FIELD = new ParseField("from");
public static final ParseField SIZE_FIELD = new ParseField("size");
@ -97,13 +100,6 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return new NewSearchSourceBuilder();
}
/**
* A static factory method to construct new search highlights.
*/
public static HighlightBuilder highlight() {
return new HighlightBuilder();
}
private QueryBuilder<?> queryBuilder;
private QueryBuilder<?> postQueryBuilder;
@ -152,7 +148,7 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
}
/**
* Constructs a new search source builder with a search query.
* Sets the search query for this request.
*
* @see org.elasticsearch.index.query.QueryBuilders
*/
@ -161,6 +157,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the query for this request
*/
public QueryBuilder<?> query() {
return queryBuilder;
}
/**
* Sets a filter that will be executed after the query has been executed and
* only has affect on the search hits (not aggregations). This filter is
@ -171,6 +174,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the post filter for this request
*/
public QueryBuilder<?> postFilter() {
return postQueryBuilder;
}
/**
* From index to start the search from. Defaults to <tt>0</tt>.
*/
@ -179,6 +189,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the from index to start the search from.
**/
public int from() {
return from;
}
/**
* The number of search hits to return. Defaults to <tt>10</tt>.
*/
@ -187,6 +204,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the number of search hits to return.
*/
public int size() {
return size;
}
/**
* Sets the minimum score below which docs will be filtered out.
*/
@ -195,6 +219,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the minimum score below which docs will be filtered out.
*/
public float minScore() {
return minScore;
}
/**
* Should each {@link org.elasticsearch.search.SearchHit} be returned with
* an explanation of the hit (ranking).
@ -204,6 +235,14 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Indicates whether each search hit will be returned with an explanation of
* the hit (ranking)
*/
public Boolean explain() {
return explain;
}
/**
* Should each {@link org.elasticsearch.search.SearchHit} be returned with a
* version associated with it.
@ -213,6 +252,14 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Indicates whether the document's version will be included in the search
* hits.
*/
public Boolean version() {
return version;
}
/**
* An optional timeout to control how long search is allowed to take.
*/
@ -222,11 +269,10 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
}
/**
* An optional timeout to control how long search is allowed to take.
* Gets the timeout to control how long search is allowed to take.
*/
public NewSearchSourceBuilder timeout(String timeout) {
this.timeoutInMillis = TimeValue.parseTimeValue(timeout, null, getClass().getSimpleName() + ".timeout").millis();
return this;
public long timeoutInMillis() {
return timeoutInMillis;
}
/**
@ -241,6 +287,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the number of documents to terminate after collecting.
*/
public int terminateAfter() {
return terminateAfter;
}
/**
* Adds a sort against the given field name and the sort ordering.
*
@ -278,6 +331,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the bytes representing the sort builders for this request.
*/
public List<BytesReference> sorts() {
return sorts;
}
/**
* Applies when sorting, and controls if scores will be tracked as well.
* Defaults to <tt>false</tt>.
@ -288,7 +348,14 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
}
/**
* Add an get to perform as part of the search.
* Indicates whether scores will be tracked for this request.
*/
public boolean trackScores() {
return trackScores;
}
/**
* Add an aggregation to perform as part of the search.
*/
public NewSearchSourceBuilder aggregation(AbstractAggregationBuilder aggregation) throws IOException {
if (aggregations == null) {
@ -300,6 +367,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the bytes representing the aggregation builders for this request.
*/
public List<BytesReference> aggregations() {
return aggregations;
}
/**
* Set the rescore window size for rescores that don't specify their window.
*/
@ -308,6 +382,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Get the rescore window size for rescores that don't specify their window.
*/
public int defaultRescoreWindowSize() {
return defaultRescoreWindowSize;
}
/**
* Adds highlight to perform as part of the search.
*/
@ -318,6 +399,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the bytes representing the hightlighter builder for this request.
*/
public BytesReference highlight() {
return highlightBuilder;
}
public NewSearchSourceBuilder innerHits(InnerHitsBuilder innerHitsBuilder) throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
innerHitsBuilder.toXContent(builder, EMPTY_PARAMS);
@ -325,6 +413,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the bytes representing the inner hits builder for this request.
*/
public BytesReference innerHits() {
return innerHitsBuilder;
}
public NewSearchSourceBuilder suggest(SuggestBuilder suggestBuilder) throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
suggestBuilder.toXContent(builder, EMPTY_PARAMS);
@ -332,6 +427,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the bytes representing the suggester builder for this request.
*/
public BytesReference suggest() {
return suggestBuilder;
}
public NewSearchSourceBuilder addRescorer(RescoreBuilder rescoreBuilder) throws IOException {
if (rescoreBuilders == null) {
rescoreBuilders = new ArrayList<>();
@ -347,6 +449,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the bytes representing the rescore builders for this request.
*/
public List<BytesReference> rescores() {
return rescoreBuilders;
}
/**
* Indicates whether the response should contain the stored _source for
* every hit
@ -402,6 +511,14 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the {@link FetchSourceContext} which defines how the _source should
* be fetched.
*/
public FetchSourceContext fetchSource() {
return fetchSourceContext;
}
/**
* Sets no fields to be loaded, resulting in only id and type to be returned
* per field.
@ -433,16 +550,10 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
}
/**
* Adds a field to load and return (note, it must be stored) as part of the
* search request. If none are specified, the source of the document will be
* return.
* Gets the fields to load and return as part of the search request.
*/
public NewSearchSourceBuilder field(String name) {
if (fieldNames == null) {
fieldNames = new ArrayList<>();
}
fieldNames.add(name);
return this;
public List<String> fieldNames() {
return fieldNames;
}
/**
@ -457,6 +568,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the field-data fields.
*/
public List<String> fieldDataFields() {
return fieldDataFields;
}
/**
* Adds a script field under the given name with the provided script.
*
@ -473,6 +591,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the script fields.
*/
public List<ScriptField> scriptFields() {
return scriptFields;
}
/**
* Sets the boost a specific index will receive when the query is executeed
* against it.
@ -490,6 +615,14 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* Gets the boost a specific indices will receive when the query is
* executeed against them.
*/
public ObjectFloatHashMap<String> indexBoost() {
return indexBoost;
}
/**
* The stats groups this request will be aggregated under.
*/
@ -498,6 +631,13 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
return this;
}
/**
* The stats groups this request will be aggregated under.
*/
public String[] stats() {
return stats;
}
public NewSearchSourceBuilder fromXContent(XContentParser parser, QueryParseContext context) throws IOException {
NewSearchSourceBuilder builder = new NewSearchSourceBuilder();
XContentParser.Token token;
@ -579,7 +719,21 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
}
builder.indexBoost = indexBoost;
} else if (context.parseFieldMatcher().match(currentFieldName, AGGREGATIONS_FIELD)) {
// NOCOMMIT implement aggregations parsing
List<BytesReference> aggregations = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
currentFieldName = parser.currentName();
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType());
xContentBuilder.field(currentFieldName);
xContentBuilder.copyCurrentStructure(parser);
aggregations.add(xContentBuilder.bytes());
} else {
throw new QueryParsingException(context, "Unknown key for a " + token + " in [" + currentFieldName + "].",
parser.getTokenLocation());
}
}
builder.indexBoost = indexBoost;
} else if (context.parseFieldMatcher().match(currentFieldName, HIGHLIGHT_FIELD)) {
XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
builder.highlightBuilder = xContentBuilder.bytes();
@ -1032,7 +1186,7 @@ public class NewSearchSourceBuilder extends ToXContentToBytes implements Writeab
out.writeBytesReference(suggestBuilder);
}
out.writeVInt(terminateAfter);
out.writeVLong(timeoutInMillis);
out.writeLong(timeoutInMillis);
out.writeBoolean(trackScores);
out.writeBoolean(version);
}