Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added source fetching and filtering parameters to search, get, multi-get, get-source and explain requests #3302

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.fetch.source.FetchSourceContext;

import java.io.IOException;

Expand All @@ -43,8 +45,9 @@ public class ExplainRequest extends SingleShardOperationRequest<ExplainRequest>
private String routing;
private String preference;
private BytesReference source;
private String[] fields;
private boolean sourceUnsafe;
private String[] fields;
private FetchSourceContext fetchSourceContext;

private String[] filteringAlias = Strings.EMPTY_ARRAY;

Expand Down Expand Up @@ -121,6 +124,19 @@ public ExplainRequest source(BytesReference source, boolean unsafe) {
return this;
}

/**
* Allows setting the {@link FetchSourceContext} for this request, controlling if and how _source should be returned.
*/
public ExplainRequest fetchSourceContext(FetchSourceContext context) {
this.fetchSourceContext = context;
return this;
}

public FetchSourceContext fetchSourceContext() {
return fetchSourceContext;
}


public String[] fields() {
return fields;
}
Expand Down Expand Up @@ -178,6 +194,8 @@ public void readFrom(StreamInput in) throws IOException {
if (in.readBoolean()) {
fields = in.readStringArray();
}

fetchSourceContext = FetchSourceContext.optionalReadFromStream(in);
}

@Override
Expand All @@ -195,5 +213,7 @@ public void writeTo(StreamOutput out) throws IOException {
} else {
out.writeBoolean(false);
}

FetchSourceContext.optionalWriteToStream(fetchSourceContext, out);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import org.elasticsearch.action.support.single.shard.SingleShardOperationRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.internal.InternalClient;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.fetch.source.FetchSourceContext;

/**
* A builder for {@link ExplainRequest}.
Expand Down Expand Up @@ -105,6 +108,57 @@ public ExplainRequestBuilder setFields(String... fields) {
return this;
}

/**
* Indicates whether the response should contain the stored _source
*
*
* @param fetch
* @return
*/
public ExplainRequestBuilder setFetchSource(boolean fetch) {
FetchSourceContext context = request.fetchSourceContext();
if (context == null) {
request.fetchSourceContext(new FetchSourceContext(fetch));
}
else {
context.fetchSource(fetch);
}
return this;
}

/**
* Indicate that _source should be returned, with an "include" and/or "exclude" set which can include simple wildcard
* elements.
*
* @param include An optional include (optionally wildcarded) pattern to filter the returned _source
* @param exclude An optional exclude (optionally wildcarded) pattern to filter the returned _source
*/
public ExplainRequestBuilder setFetchSource(@Nullable String include, @Nullable String exclude) {
return setFetchSource(
include == null? Strings.EMPTY_ARRAY : new String[] {include},
exclude == null? Strings.EMPTY_ARRAY : new String[] {exclude});
}

/**
* Indicate that _source should be returned, with an "include" and/or "exclude" set which can include simple wildcard
* elements.
*
* @param includes An optional list of include (optionally wildcarded) pattern to filter the returned _source
* @param excludes An optional list of exclude (optionally wildcarded) pattern to filter the returned _source
*/
public ExplainRequestBuilder setFetchSource(@Nullable String[] includes, @Nullable String[] excludes) {
FetchSourceContext context = request.fetchSourceContext();
if (context == null) {
request.fetchSourceContext(new FetchSourceContext(includes, excludes));
}
else {
context.fetchSource(true);
context.includes(includes);
context.excludes(excludes);
}
return this;
}

/**
* Sets the full source of the explain request (for example, wrapping an actual query).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,11 @@ protected ExplainResponse shardOperation(ExplainRequest request, int shardId) th
} else {
explanation = context.searcher().explain(context.query(), topLevelDocId);
}
if (request.fields() != null) {
if (request.fields().length == 1 && "_source".equals(request.fields()[0])) {
request.fields(null); // Load the _source field
}
if (request.fields() != null || (request.fetchSourceContext() != null && request.fetchSourceContext().fetchSource())) {
// Advantage is that we're not opening a second searcher to retrieve the _source. Also
// because we are working in the same searcher in engineGetResult we can be sure that a
// doc isn't deleted between the initial get and this call.
GetResult getResult = indexShard.getService().get(result, request.id(), request.type(), request.fields());
GetResult getResult = indexShard.getService().get(result, request.id(), request.type(), request.fields(), request.fetchSourceContext());
return new ExplainResponse(true, explanation, getResult);
} else {
return new ExplainResponse(true, explanation);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/elasticsearch/action/get/GetRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.search.fetch.source.FetchSourceContext;

import java.io.IOException;

Expand All @@ -51,6 +52,8 @@ public class GetRequest extends SingleShardOperationRequest<GetRequest> {

private String[] fields;

private FetchSourceContext fetchSourceContext;

private boolean refresh = false;

Boolean realtime;
Expand Down Expand Up @@ -162,6 +165,18 @@ public String preference() {
return this.preference;
}

/**
* Allows setting the {@link FetchSourceContext} for this request, controlling if and how _source should be returned.
*/
public GetRequest fetchSourceContext(FetchSourceContext context) {
this.fetchSourceContext = context;
return this;
}

public FetchSourceContext fetchSourceContext() {
return fetchSourceContext;
}

/**
* Explicitly specify the fields that will be returned. By default, the <tt>_source</tt>
* field will be returned.
Expand Down Expand Up @@ -248,8 +263,11 @@ public void readFrom(StreamInput in) throws IOException {
} else if (realtime == 1) {
this.realtime = true;
}

this.versionType = VersionType.fromValue(in.readByte());
this.version = in.readVLong();

fetchSourceContext = FetchSourceContext.optionalReadFromStream(in);
}

@Override
Expand Down Expand Up @@ -279,6 +297,8 @@ public void writeTo(StreamOutput out) throws IOException {

out.writeByte(versionType.getValue());
out.writeVLong(version);

FetchSourceContext.optionalWriteToStream(fetchSourceContext, out);
}

@Override
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/org/elasticsearch/action/get/GetRequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.elasticsearch.client.internal.InternalClient;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.common.Strings;
import org.elasticsearch.search.fetch.source.FetchSourceContext;

/**
* A get document action request builder.
Expand Down Expand Up @@ -93,6 +95,56 @@ public GetRequestBuilder setFields(String... fields) {
return this;
}

/**
* Indicates whether the response should contain the stored _source
*
* @param fetch
* @return
*/
public GetRequestBuilder setFetchSource(boolean fetch) {
FetchSourceContext context = request.fetchSourceContext();
if (context == null) {
request.fetchSourceContext(new FetchSourceContext(fetch));
}
else {
context.fetchSource(fetch);
}
return this;
}

/**
* Indicate that _source should be returned, with an "include" and/or "exclude" set which can include simple wildcard
* elements.
*
* @param include An optional include (optionally wildcarded) pattern to filter the returned _source
* @param exclude An optional exclude (optionally wildcarded) pattern to filter the returned _source
*/
public GetRequestBuilder setFetchSource(@Nullable String include, @Nullable String exclude) {
return setFetchSource(
include == null? Strings.EMPTY_ARRAY : new String[] {include},
exclude == null? Strings.EMPTY_ARRAY : new String[] {exclude});
}

/**
* Indicate that _source should be returned, with an "include" and/or "exclude" set which can include simple wildcard
* elements.
*
* @param includes An optional list of include (optionally wildcarded) pattern to filter the returned _source
* @param excludes An optional list of exclude (optionally wildcarded) pattern to filter the returned _source
*/
public GetRequestBuilder setFetchSource(@Nullable String[] includes, @Nullable String[] excludes) {
FetchSourceContext context = request.fetchSourceContext();
if (context == null) {
request.fetchSourceContext(new FetchSourceContext(includes, excludes));
}
else {
context.fetchSource(true);
context.includes(includes);
context.excludes(excludes);
}
return this;
}

/**
* Should a refresh be executed before this get operation causing the operation to
* return the latest value. Note, heavy get should not set this to <tt>true</tt>. Defaults
Expand Down Expand Up @@ -129,4 +181,6 @@ public GetRequestBuilder setVersionType(VersionType versionType) {
protected void doExecute(ActionListener<GetResponse> listener) {
((Client) client).get(request, listener);
}


}
Loading