2010-02-08 15:30:06 +02:00
|
|
|
/*
|
2011-12-06 02:42:25 +02:00
|
|
|
* Licensed to ElasticSearch and Shay Banon under one
|
2010-02-08 15:30:06 +02:00
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
2011-12-06 02:42:25 +02:00
|
|
|
* regarding copyright ownership. ElasticSearch licenses this
|
2010-02-08 15:30:06 +02:00
|
|
|
* file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.elasticsearch.action.get;
|
|
|
|
|
2011-06-26 16:33:44 +03:00
|
|
|
import org.elasticsearch.action.ActionRequestValidationException;
|
2012-01-15 12:09:18 +02:00
|
|
|
import org.elasticsearch.action.ValidateActions;
|
2010-11-21 13:26:36 +02:00
|
|
|
import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest;
|
2011-06-24 13:43:37 +03:00
|
|
|
import org.elasticsearch.common.Nullable;
|
2010-06-15 17:59:04 +03:00
|
|
|
import org.elasticsearch.common.Required;
|
2010-06-15 17:28:05 +03:00
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
/**
|
2011-06-24 13:43:37 +03:00
|
|
|
* A request to get a document (its source) from an index based on its type (optional) and id. Best created using
|
2010-02-27 16:36:20 +02:00
|
|
|
* {@link org.elasticsearch.client.Requests#getRequest(String)}.
|
2011-12-06 02:42:25 +02:00
|
|
|
* <p/>
|
2013-02-23 08:28:24 +01:00
|
|
|
* <p>The operation requires the {@link #index()}, {@link #type(String)} and {@link #id(String)}
|
2010-02-27 16:36:20 +02:00
|
|
|
* to be set.
|
|
|
|
*
|
|
|
|
* @see org.elasticsearch.action.get.GetResponse
|
|
|
|
* @see org.elasticsearch.client.Requests#getRequest(String)
|
|
|
|
* @see org.elasticsearch.client.Client#get(GetRequest)
|
2010-02-08 15:30:06 +02:00
|
|
|
*/
|
2012-09-26 23:46:27 +02:00
|
|
|
public class GetRequest extends SingleShardOperationRequest<GetRequest> {
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2011-06-26 16:33:44 +03:00
|
|
|
protected String type;
|
|
|
|
protected String id;
|
|
|
|
protected String routing;
|
|
|
|
protected String preference;
|
|
|
|
|
2010-03-17 20:03:32 +02:00
|
|
|
private String[] fields;
|
|
|
|
|
2010-11-07 23:50:48 +02:00
|
|
|
private boolean refresh = false;
|
|
|
|
|
2011-06-24 09:39:37 +03:00
|
|
|
Boolean realtime;
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
GetRequest() {
|
2011-06-24 13:43:37 +03:00
|
|
|
type = "_all";
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2010-02-27 16:36:20 +02:00
|
|
|
/**
|
2013-02-23 08:28:24 +01:00
|
|
|
* Constructs a new get request against the specified index. The {@link #type(String)} and {@link #id(String)}
|
2010-02-27 16:36:20 +02:00
|
|
|
* must be set.
|
|
|
|
*/
|
2010-02-08 15:30:06 +02:00
|
|
|
public GetRequest(String index) {
|
2011-06-26 16:33:44 +03:00
|
|
|
super(index);
|
|
|
|
this.type = "_all";
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2010-02-27 16:36:20 +02:00
|
|
|
/**
|
|
|
|
* Constructs a new get request against the specified index with the type and id.
|
|
|
|
*
|
|
|
|
* @param index The index to get the document from
|
|
|
|
* @param type The type of the document
|
|
|
|
* @param id The id of the document
|
|
|
|
*/
|
2010-02-08 15:30:06 +02:00
|
|
|
public GetRequest(String index, String type, String id) {
|
2011-06-26 16:33:44 +03:00
|
|
|
super(index);
|
|
|
|
this.type = type;
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public ActionRequestValidationException validate() {
|
2011-06-26 16:33:44 +03:00
|
|
|
ActionRequestValidationException validationException = super.validate();
|
|
|
|
if (type == null) {
|
2012-01-15 12:09:18 +02:00
|
|
|
validationException = ValidateActions.addValidationError("type is missing", validationException);
|
2011-06-26 16:33:44 +03:00
|
|
|
}
|
|
|
|
if (id == null) {
|
2012-01-15 12:09:18 +02:00
|
|
|
validationException = ValidateActions.addValidationError("id is missing", validationException);
|
2011-06-26 16:33:44 +03:00
|
|
|
}
|
|
|
|
return validationException;
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
2010-05-29 05:50:17 +03:00
|
|
|
|
2010-02-27 16:36:20 +02:00
|
|
|
/**
|
|
|
|
* Sets the type of the document to fetch.
|
|
|
|
*/
|
2013-02-23 08:28:24 +01:00
|
|
|
public GetRequest type(@Nullable String type) {
|
2011-06-24 13:43:37 +03:00
|
|
|
if (type == null) {
|
|
|
|
type = "_all";
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
this.type = type;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-02-27 16:36:20 +02:00
|
|
|
/**
|
|
|
|
* Sets the id of the document to fetch.
|
|
|
|
*/
|
2011-12-06 02:42:25 +02:00
|
|
|
@Required
|
2013-02-23 08:28:24 +01:00
|
|
|
public GetRequest id(String id) {
|
2010-02-08 15:30:06 +02:00
|
|
|
this.id = id;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-05-17 01:40:40 +03:00
|
|
|
/**
|
|
|
|
* Sets the parent id of this document. Will simply set the routing to this value, as it is only
|
|
|
|
* used for routing with delete requests.
|
|
|
|
*/
|
2013-02-23 08:28:24 +01:00
|
|
|
public GetRequest parent(String parent) {
|
2012-05-17 01:40:40 +03:00
|
|
|
if (routing == null) {
|
|
|
|
routing = parent;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-11-02 19:56:18 +02:00
|
|
|
/**
|
|
|
|
* Controls the shard routing of the request. Using this value to hash the shard
|
|
|
|
* and not the id.
|
|
|
|
*/
|
2013-02-23 08:28:24 +01:00
|
|
|
public GetRequest routing(String routing) {
|
2010-11-02 19:56:18 +02:00
|
|
|
this.routing = routing;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2011-03-14 11:30:01 +02:00
|
|
|
/**
|
|
|
|
* Sets the preference to execute the search. Defaults to randomize across shards. Can be set to
|
|
|
|
* <tt>_local</tt> to prefer local shards, <tt>_primary</tt> to execute only on primary shards, or
|
|
|
|
* a custom value, which guarantees that the same order will be used across different requests.
|
|
|
|
*/
|
2013-02-23 08:28:24 +01:00
|
|
|
public GetRequest preference(String preference) {
|
2011-03-14 11:30:01 +02:00
|
|
|
this.preference = preference;
|
|
|
|
return this;
|
|
|
|
}
|
2010-11-02 19:56:18 +02:00
|
|
|
|
2013-02-23 08:28:24 +01:00
|
|
|
public String type() {
|
2011-06-26 16:33:44 +03:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2013-02-23 08:28:24 +01:00
|
|
|
public String id() {
|
2011-06-26 16:33:44 +03:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2013-02-23 08:28:24 +01:00
|
|
|
public String routing() {
|
2011-06-26 16:33:44 +03:00
|
|
|
return this.routing;
|
|
|
|
}
|
|
|
|
|
2013-02-23 08:28:24 +01:00
|
|
|
public String preference() {
|
2011-06-26 16:33:44 +03:00
|
|
|
return this.preference;
|
|
|
|
}
|
|
|
|
|
2010-03-17 20:03:32 +02:00
|
|
|
/**
|
|
|
|
* Explicitly specify the fields that will be returned. By default, the <tt>_source</tt>
|
|
|
|
* field will be returned.
|
|
|
|
*/
|
2013-02-23 08:28:24 +01:00
|
|
|
public GetRequest fields(String... fields) {
|
2010-03-17 20:03:32 +02:00
|
|
|
this.fields = fields;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Explicitly specify the fields that will be returned. By default, the <tt>_source</tt>
|
|
|
|
* field will be returned.
|
|
|
|
*/
|
2013-02-23 08:28:24 +01:00
|
|
|
public String[] fields() {
|
2010-03-17 20:03:32 +02:00
|
|
|
return this.fields;
|
|
|
|
}
|
|
|
|
|
2010-11-07 23:50:48 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* to <tt>false</tt>.
|
|
|
|
*/
|
2013-02-23 08:28:24 +01:00
|
|
|
public GetRequest refresh(boolean refresh) {
|
2010-11-07 23:50:48 +02:00
|
|
|
this.refresh = refresh;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-02-23 08:28:24 +01:00
|
|
|
public boolean refresh() {
|
2010-11-07 23:50:48 +02:00
|
|
|
return this.refresh;
|
|
|
|
}
|
|
|
|
|
2013-02-23 08:28:24 +01:00
|
|
|
public boolean realtime() {
|
2011-06-24 09:39:37 +03:00
|
|
|
return this.realtime == null ? true : this.realtime;
|
|
|
|
}
|
|
|
|
|
2013-02-23 08:28:24 +01:00
|
|
|
public GetRequest realtime(Boolean realtime) {
|
2011-06-24 09:39:37 +03:00
|
|
|
this.realtime = realtime;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void readFrom(StreamInput in) throws IOException {
|
2010-02-08 15:30:06 +02:00
|
|
|
super.readFrom(in);
|
2013-07-19 16:17:22 +02:00
|
|
|
type = in.readSharedString();
|
2012-09-26 23:46:27 +02:00
|
|
|
id = in.readString();
|
|
|
|
routing = in.readOptionalString();
|
|
|
|
preference = in.readOptionalString();
|
2010-11-07 23:51:18 +02:00
|
|
|
refresh = in.readBoolean();
|
2010-03-17 20:03:32 +02:00
|
|
|
int size = in.readInt();
|
|
|
|
if (size >= 0) {
|
|
|
|
fields = new String[size];
|
|
|
|
for (int i = 0; i < size; i++) {
|
2012-09-26 23:46:27 +02:00
|
|
|
fields[i] = in.readString();
|
2010-03-17 20:03:32 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-24 09:39:37 +03:00
|
|
|
byte realtime = in.readByte();
|
|
|
|
if (realtime == 0) {
|
|
|
|
this.realtime = false;
|
|
|
|
} else if (realtime == 1) {
|
|
|
|
this.realtime = true;
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void writeTo(StreamOutput out) throws IOException {
|
2010-02-08 15:30:06 +02:00
|
|
|
super.writeTo(out);
|
2013-07-19 16:17:22 +02:00
|
|
|
out.writeSharedString(type);
|
2012-09-26 23:46:27 +02:00
|
|
|
out.writeString(id);
|
|
|
|
out.writeOptionalString(routing);
|
|
|
|
out.writeOptionalString(preference);
|
2011-06-26 16:33:44 +03:00
|
|
|
|
2010-11-07 23:51:18 +02:00
|
|
|
out.writeBoolean(refresh);
|
2010-03-17 20:03:32 +02:00
|
|
|
if (fields == null) {
|
|
|
|
out.writeInt(-1);
|
|
|
|
} else {
|
|
|
|
out.writeInt(fields.length);
|
|
|
|
for (String field : fields) {
|
2012-09-26 23:46:27 +02:00
|
|
|
out.writeString(field);
|
2010-03-17 20:03:32 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-24 09:39:37 +03:00
|
|
|
if (realtime == null) {
|
|
|
|
out.writeByte((byte) -1);
|
|
|
|
} else if (realtime == false) {
|
|
|
|
out.writeByte((byte) 0);
|
|
|
|
} else {
|
|
|
|
out.writeByte((byte) 1);
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
2010-02-27 19:27:07 +02:00
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2011-06-26 16:33:44 +03:00
|
|
|
return "[" + index + "][" + type + "][" + id + "]: routing [" + routing + "]";
|
2010-02-27 19:27:07 +02:00
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|