From 7ed5e9e79a185422fe1d4766c7a0a3dc04a7d111 Mon Sep 17 00:00:00 2001 From: kimchy Date: Fri, 24 Jun 2011 13:43:37 +0300 Subject: [PATCH] Get API: Make type optional, closes #1061. --- .../elasticsearch/action/get/GetRequest.java | 11 +++++-- .../action/get/TransportGetAction.java | 30 +++++++++++++++---- .../java/org/elasticsearch/client/Client.java | 4 +-- .../client/action/get/GetRequestBuilder.java | 5 ++-- .../integration/document/GetActionTests.java | 6 ++++ 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/get/GetRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/get/GetRequest.java index 2221e818c23..48bc1b032f8 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/get/GetRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/get/GetRequest.java @@ -20,6 +20,7 @@ package org.elasticsearch.action.get; import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Required; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -27,7 +28,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; 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)}. * *

The operation requires the {@link #index()}, {@link #type(String)} and {@link #id(String)} @@ -47,6 +48,7 @@ public class GetRequest extends SingleShardOperationRequest { Boolean realtime; GetRequest() { + type = "_all"; } /** @@ -54,7 +56,7 @@ public class GetRequest extends SingleShardOperationRequest { * must be set. */ 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. */ - @Required public GetRequest type(String type) { + public GetRequest type(@Nullable String type) { + if (type == null) { + type = "_all"; + } this.type = type; return this; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/get/TransportGetAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/get/TransportGetAction.java index d0d852f7deb..f3790cdc3bd 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/get/TransportGetAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/get/TransportGetAction.java @@ -43,6 +43,7 @@ import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.FieldMappers; +import org.elasticsearch.index.mapper.Uid; import org.elasticsearch.index.mapper.internal.UidFieldMapper; import org.elasticsearch.index.mapper.selector.FieldMappersFieldSelector; import org.elasticsearch.index.service.IndexService; @@ -110,7 +111,28 @@ public class TransportGetAction extends TransportShardSingleOperationAction fields = null; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/Client.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/Client.java index 9c8e5e34f6e..1906bf8f5f4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/Client.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/Client.java @@ -228,9 +228,9 @@ public interface Client { 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. diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/get/GetRequestBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/get/GetRequestBuilder.java index 7e90744891a..eaa2d1359c8 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/get/GetRequestBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/get/GetRequestBuilder.java @@ -46,9 +46,10 @@ public class GetRequestBuilder extends BaseRequestBuildernull, 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); return this; } diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/GetActionTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/GetActionTests.java index 1f3915746ee..a3da4e8154d 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/GetActionTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/document/GetActionTests.java @@ -72,6 +72,12 @@ public class GetActionTests extends AbstractNodesTests { assertThat(response.sourceAsMap().get("field1").toString(), equalTo("value1")); 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"); response = client.prepareGet("test", "type1", "1").setRealtime(false).execute().actionGet(); assertThat(response.exists(), equalTo(false));