Skip to content

Commit

Permalink
Rewrote the percolate existing doc api.
Browse files Browse the repository at this point in the history
The percolate existing doc feature now reuses the get request instead of having a separate request body.
Relates to #3380
  • Loading branch information
martijnvg committed Jul 29, 2013
1 parent a9dd3c9 commit 3f6877e
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 297 deletions.
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.ElasticSearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.bytes.BytesArray;
Expand All @@ -46,11 +47,12 @@ public class PercolateRequest extends BroadcastOperationRequest<PercolateRequest
private String documentType;
private String routing;
private String preference;
private GetRequest getRequest;

private BytesReference source;
private boolean unsafe;

private BytesReference fetchedDoc;
private BytesReference docSource;

// Used internally in order to compute tookInMillis, TransportBroadcastOperationAction itself doesn't allow
// to hold it temporarily in an easy way
Expand All @@ -64,13 +66,13 @@ public PercolateRequest(String index, String documentType) {
this.documentType = documentType;
}

public PercolateRequest(PercolateRequest request, BytesReference fetchedDoc) {
public PercolateRequest(PercolateRequest request, BytesReference docSource) {
super(request.indices());
this.documentType = request.documentType();
this.routing = request.routing();
this.preference = request.preference();
this.source = request.source;
this.fetchedDoc = fetchedDoc;
this.docSource = docSource;
}

public String documentType() {
Expand Down Expand Up @@ -99,6 +101,14 @@ public PercolateRequest preference(String preference) {
return this;
}

public GetRequest getRequest() {
return getRequest;
}

public void getRequest(GetRequest getRequest) {
this.getRequest = getRequest;
}

/**
* Before we fork on a local thread, make sure we copy over the bytes if they are unsafe
*/
Expand Down Expand Up @@ -164,8 +174,8 @@ public PercolateRequest source(PercolateSourceBuilder sourceBuilder) {
return this;
}

BytesReference fetchedDoc() {
return fetchedDoc;
BytesReference docSource() {
return docSource;
}

@Override
Expand All @@ -177,30 +187,45 @@ public ActionRequestValidationException validate() {
if (documentType == null) {
validationException = addValidationError("type is missing", validationException);
}
if (source == null) {
validationException = addValidationError("source is missing", validationException);
if (source == null && getRequest == null) {
validationException = addValidationError("source or get is missing", validationException);
}
if (getRequest != null && getRequest.fields() != null) {
validationException = addValidationError("get fields option isn't supported via percolate request", validationException);
}
return validationException;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
startTime = in.readVLong();
documentType = in.readString();
routing = in.readOptionalString();
preference = in.readOptionalString();
unsafe = false;
source = in.readBytesReference();
startTime = in.readVLong();
docSource = in.readBytesReference();
if (in.readBoolean()) {
getRequest = new GetRequest(null);
getRequest.readFrom(in);
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVLong(startTime);
out.writeString(documentType);
out.writeOptionalString(routing);
out.writeOptionalString(preference);
out.writeBytesReference(source);
out.writeVLong(startTime);
out.writeBytesReference(docSource);
if (getRequest != null) {
out.writeBoolean(true);
getRequest.writeTo(out);
} else {
out.writeBoolean(false);
}
}
}
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.action.percolate;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.internal.InternalClient;
Expand Down Expand Up @@ -89,6 +90,15 @@ public PercolateRequestBuilder setPreference(String preference) {
return this;
}

/**
* Enables percolating an existing document. Instead of specifying the source of the document to percolate, define
* a get request that will fetch a document and use its source.
*/
public PercolateRequestBuilder setGetRequest(GetRequest getRequest) {
request.getRequest(getRequest);
return this;
}

public PercolateRequestBuilder setSource(PercolateSourceBuilder source) {
sourceBuilder = source;
return this;
Expand Down Expand Up @@ -144,11 +154,6 @@ public PercolateRequestBuilder setPercolateDoc(PercolateSourceBuilder.DocBuilder
return this;
}

public PercolateRequestBuilder setPercolateGet(PercolateSourceBuilder.GetBuilder getBuilder) {
sourceBuilder().setGet(getBuilder);
return this;
}

public PercolateRequestBuilder setPercolateQuery(QueryBuilder queryBuilder) {
sourceBuilder().setQueryBuilder(queryBuilder);
return this;
Expand Down
Expand Up @@ -13,7 +13,7 @@ public class PercolateShardRequest extends BroadcastShardOperationRequest {

private String documentType;
private BytesReference source;
private BytesReference fetchedDoc;
private BytesReference docSource;

public PercolateShardRequest() {
}
Expand All @@ -22,7 +22,7 @@ public PercolateShardRequest(String index, int shardId, PercolateRequest request
super(index, shardId, request);
this.documentType = request.documentType();
this.source = request.source();
this.fetchedDoc = request.fetchedDoc();
this.docSource = request.docSource();
}

public String documentType() {
Expand All @@ -33,31 +33,24 @@ public BytesReference source() {
return source;
}

public BytesReference fetchedDoc() {
return fetchedDoc;
public BytesReference docSource() {
return docSource;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
documentType = in.readString();
source = in.readBytesReference();
if (in.readBoolean()) {
fetchedDoc = in.readBytesReference();
}
docSource = in.readBytesReference();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(documentType);
out.writeBytesReference(source);
if (fetchedDoc != null) {
out.writeBoolean(true);
out.writeBytesReference(fetchedDoc);
} else {
out.writeBoolean(false);
}
out.writeBytesReference(docSource);
}

}
Expand Up @@ -35,13 +35,11 @@
public class PercolateSourceBuilder implements ToXContent {

private DocBuilder docBuilder;
private GetBuilder getBuilder;
private QueryBuilder queryBuilder;
private FilterBuilder filterBuilder;

public DocBuilder percolateDocument() {
if (docBuilder == null) {
getBuilder = null;
docBuilder = new DocBuilder();
}
return docBuilder;
Expand All @@ -55,22 +53,6 @@ public void setDoc(DocBuilder docBuilder) {
this.docBuilder = docBuilder;
}

public GetBuilder percolateGet() {
if (getBuilder == null) {
docBuilder = null;
getBuilder = new GetBuilder();
}
return getBuilder;
}

public GetBuilder getGet() {
return getBuilder;
}

public void setGet(GetBuilder getBuilder) {
this.getBuilder = getBuilder;
}

public QueryBuilder getQueryBuilder() {
return queryBuilder;
}
Expand Down Expand Up @@ -103,9 +85,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (docBuilder != null) {
docBuilder.toXContent(builder, params);
}
if (getBuilder != null) {
getBuilder.toXContent(builder, params);
}
if (queryBuilder != null) {
builder.field("query");
queryBuilder.toXContent(builder, params);
Expand Down Expand Up @@ -172,89 +151,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
}

public static GetBuilder getBuilder(String index, String type, String id) {
return new GetBuilder().setIndex(index).setType(type).setId(id);
}

public static class GetBuilder implements ToXContent {

private String index;
private String type;
private String id;
private Long version;
private String routing;
private String preference;

public String getIndex() {
return index;
}

public GetBuilder setIndex(String index) {
this.index = index;
return this;
}

public String getType() {
return type;
}

public GetBuilder setType(String type) {
this.type = type;
return this;
}

public String getId() {
return id;
}

public GetBuilder setId(String id) {
this.id = id;
return this;
}

public Long getVersion() {
return version;
}

public GetBuilder setVersion(Long version) {
this.version = version;
return this;
}

public String getRouting() {
return routing;
}

public GetBuilder setRouting(String routing) {
this.routing = routing;
return this;
}

public String getPreference() {
return preference;
}

public GetBuilder setPreference(String preference) {
this.preference = preference;
return this;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("get");
builder.field("index", index);
builder.field("type", type);
builder.field("id", id);
if (version != null) {
builder.field("version", version);
}
if (routing != null) {
builder.field("routing", routing);
}
builder.endObject();
return builder;
}

}

}

0 comments on commit 3f6877e

Please sign in to comment.