Get API: Make type optional, closes #1061.
This commit is contained in:
parent
89a46d1c87
commit
7ed5e9e79a
|
@ -20,6 +20,7 @@
|
||||||
package org.elasticsearch.action.get;
|
package org.elasticsearch.action.get;
|
||||||
|
|
||||||
import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest;
|
import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest;
|
||||||
|
import org.elasticsearch.common.Nullable;
|
||||||
import org.elasticsearch.common.Required;
|
import org.elasticsearch.common.Required;
|
||||||
import org.elasticsearch.common.io.stream.StreamInput;
|
import org.elasticsearch.common.io.stream.StreamInput;
|
||||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||||
|
@ -27,7 +28,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A request to get a document (its source) from an index based on its type and id. Best created using
|
* A request to get a document (its source) from an index based on its type (optional) and id. Best created using
|
||||||
* {@link org.elasticsearch.client.Requests#getRequest(String)}.
|
* {@link org.elasticsearch.client.Requests#getRequest(String)}.
|
||||||
*
|
*
|
||||||
* <p>The operation requires the {@link #index()}, {@link #type(String)} and {@link #id(String)}
|
* <p>The operation requires the {@link #index()}, {@link #type(String)} and {@link #id(String)}
|
||||||
|
@ -47,6 +48,7 @@ public class GetRequest extends SingleShardOperationRequest {
|
||||||
Boolean realtime;
|
Boolean realtime;
|
||||||
|
|
||||||
GetRequest() {
|
GetRequest() {
|
||||||
|
type = "_all";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,7 +56,7 @@ public class GetRequest extends SingleShardOperationRequest {
|
||||||
* must be set.
|
* must be set.
|
||||||
*/
|
*/
|
||||||
public GetRequest(String index) {
|
public GetRequest(String index) {
|
||||||
super(index, null, null);
|
super(index, "_all", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,7 +81,10 @@ public class GetRequest extends SingleShardOperationRequest {
|
||||||
/**
|
/**
|
||||||
* Sets the type of the document to fetch.
|
* Sets the type of the document to fetch.
|
||||||
*/
|
*/
|
||||||
@Required public GetRequest type(String type) {
|
public GetRequest type(@Nullable String type) {
|
||||||
|
if (type == null) {
|
||||||
|
type = "_all";
|
||||||
|
}
|
||||||
this.type = type;
|
this.type = type;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@ import org.elasticsearch.index.engine.Engine;
|
||||||
import org.elasticsearch.index.mapper.DocumentMapper;
|
import org.elasticsearch.index.mapper.DocumentMapper;
|
||||||
import org.elasticsearch.index.mapper.FieldMapper;
|
import org.elasticsearch.index.mapper.FieldMapper;
|
||||||
import org.elasticsearch.index.mapper.FieldMappers;
|
import org.elasticsearch.index.mapper.FieldMappers;
|
||||||
|
import org.elasticsearch.index.mapper.Uid;
|
||||||
import org.elasticsearch.index.mapper.internal.UidFieldMapper;
|
import org.elasticsearch.index.mapper.internal.UidFieldMapper;
|
||||||
import org.elasticsearch.index.mapper.selector.FieldMappersFieldSelector;
|
import org.elasticsearch.index.mapper.selector.FieldMappersFieldSelector;
|
||||||
import org.elasticsearch.index.service.IndexService;
|
import org.elasticsearch.index.service.IndexService;
|
||||||
|
@ -110,7 +111,28 @@ public class TransportGetAction extends TransportShardSingleOperationAction<GetR
|
||||||
IndexService indexService = indicesService.indexServiceSafe(request.index());
|
IndexService indexService = indicesService.indexServiceSafe(request.index());
|
||||||
IndexShard indexShard = indexService.shardSafe(shardId);
|
IndexShard indexShard = indexService.shardSafe(shardId);
|
||||||
|
|
||||||
DocumentMapper docMapper = indexService.mapperService().documentMapper(request.type());
|
String type = null;
|
||||||
|
Engine.GetResult get = null;
|
||||||
|
if (request.type() == null || request.type().equals("_all")) {
|
||||||
|
for (String typeX : indexService.mapperService().types()) {
|
||||||
|
get = indexShard.get(new Engine.Get(request.realtime(), UidFieldMapper.TERM_FACTORY.createTerm(Uid.createUid(typeX, request.id()))));
|
||||||
|
if (get.exists()) {
|
||||||
|
type = typeX;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (get == null || !get.exists()) {
|
||||||
|
return new GetResponse(request.index(), request.type(), request.id(), -1, false, null, null);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
type = request.type();
|
||||||
|
get = indexShard.get(new Engine.Get(request.realtime(), UidFieldMapper.TERM_FACTORY.createTerm(Uid.createUid(type, request.id()))));
|
||||||
|
if (!get.exists()) {
|
||||||
|
return new GetResponse(request.index(), request.type(), request.id(), -1, false, null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DocumentMapper docMapper = indexService.mapperService().documentMapper(type);
|
||||||
if (docMapper == null) {
|
if (docMapper == null) {
|
||||||
return new GetResponse(request.index(), request.type(), request.id(), -1, false, null, null);
|
return new GetResponse(request.index(), request.type(), request.id(), -1, false, null, null);
|
||||||
}
|
}
|
||||||
|
@ -119,12 +141,8 @@ public class TransportGetAction extends TransportShardSingleOperationAction<GetR
|
||||||
indexShard.refresh(new Engine.Refresh(false));
|
indexShard.refresh(new Engine.Refresh(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine.GetResult get = indexShard.get(new Engine.Get(request.realtime(), docMapper.uidMapper().term(request.type(), request.id())));
|
|
||||||
try {
|
|
||||||
if (!get.exists()) {
|
|
||||||
return new GetResponse(request.index(), request.type(), request.id(), -1, false, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
try {
|
||||||
// break between having loaded it from translog (so we only have _source), and having a document to load
|
// break between having loaded it from translog (so we only have _source), and having a document to load
|
||||||
if (get.docIdAndVersion() != null) {
|
if (get.docIdAndVersion() != null) {
|
||||||
Map<String, GetField> fields = null;
|
Map<String, GetField> fields = null;
|
||||||
|
|
|
@ -228,9 +228,9 @@ public interface Client {
|
||||||
GetRequestBuilder prepareGet();
|
GetRequestBuilder prepareGet();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the document that was indexed from an index with a type and id.
|
* Gets the document that was indexed from an index with a type (optional) and id.
|
||||||
*/
|
*/
|
||||||
GetRequestBuilder prepareGet(String index, String type, String id);
|
GetRequestBuilder prepareGet(String index, @Nullable String type, String id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A count of all the documents matching a specific query.
|
* A count of all the documents matching a specific query.
|
||||||
|
|
|
@ -46,9 +46,10 @@ public class GetRequestBuilder extends BaseRequestBuilder<GetRequest, GetRespons
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the type of the document to fetch.
|
* Sets the type of the document to fetch. If set to <tt>null</tt>, will use just the id to fetch the
|
||||||
|
* first document matching it.
|
||||||
*/
|
*/
|
||||||
public GetRequestBuilder setType(String type) {
|
public GetRequestBuilder setType(@Nullable String type) {
|
||||||
request.type(type);
|
request.type(type);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,6 +72,12 @@ public class GetActionTests extends AbstractNodesTests {
|
||||||
assertThat(response.sourceAsMap().get("field1").toString(), equalTo("value1"));
|
assertThat(response.sourceAsMap().get("field1").toString(), equalTo("value1"));
|
||||||
assertThat(response.sourceAsMap().get("field2").toString(), equalTo("value2"));
|
assertThat(response.sourceAsMap().get("field2").toString(), equalTo("value2"));
|
||||||
|
|
||||||
|
logger.info("--> realtime get 1 (no type)");
|
||||||
|
response = client.prepareGet("test", null, "1").execute().actionGet();
|
||||||
|
assertThat(response.exists(), equalTo(true));
|
||||||
|
assertThat(response.sourceAsMap().get("field1").toString(), equalTo("value1"));
|
||||||
|
assertThat(response.sourceAsMap().get("field2").toString(), equalTo("value2"));
|
||||||
|
|
||||||
logger.info("--> non realtime get 1");
|
logger.info("--> non realtime get 1");
|
||||||
response = client.prepareGet("test", "type1", "1").setRealtime(false).execute().actionGet();
|
response = client.prepareGet("test", "type1", "1").setRealtime(false).execute().actionGet();
|
||||||
assertThat(response.exists(), equalTo(false));
|
assertThat(response.exists(), equalTo(false));
|
||||||
|
|
Loading…
Reference in New Issue