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

Deprecate use of type in reindex request body #36823

Merged
merged 3 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 7 additions & 11 deletions docs/reference/docs/reindex.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,15 @@ POST _reindex
// CONSOLE
// TEST[setup:twitter]

You can limit the documents by adding a type to the `source` or by adding a
query. This will only copy tweets made by `kimchy` into `new_twitter`:
You can limit the documents by adding a query to the `source`.
This will only copy tweets made by `kimchy` into `new_twitter`:

[source,js]
--------------------------------------------------
POST _reindex
{
"source": {
"index": "twitter",
"type": "_doc",
"query": {
"term": {
"user": "kimchy"
Expand All @@ -162,21 +161,19 @@ POST _reindex
// CONSOLE
// TEST[setup:twitter]

`index` and `type` in `source` can both be lists, allowing you to copy from
lots of sources in one request. This will copy documents from the `_doc` and
`post` types in the `twitter` and `blog` indices.
`index` in `source` can be a list, allowing you to copy from lots
of sources in one request. This will copy documents from the
`twitter` and `blog` indices:

[source,js]
--------------------------------------------------
POST _reindex
{
"source": {
"index": ["twitter", "blog"],
"type": ["_doc", "post"]
"index": ["twitter", "blog"]
},
"dest": {
"index": "all_together",
"type": "_doc"
"index": "all_together"
}
}
--------------------------------------------------
Expand Down Expand Up @@ -299,7 +296,6 @@ Think of the possibilities! Just be careful; you are able to
change:

* `_id`
* `_type`
* `_index`
* `_version`
* `_routing`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

package org.elasticsearch.index.reindex;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ObjectParser;
Expand Down Expand Up @@ -56,6 +58,8 @@
*/
public class RestReindexAction extends AbstractBaseReindexRestHandler<ReindexRequest, ReindexAction> {
static final ObjectParser<ReindexRequest, Void> PARSER = new ObjectParser<>("reindex");
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in reindex requests is deprecated.";
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(RestReindexAction.class));

static {
ObjectParser.Parser<ReindexRequest, Void> sourceParser = (parser, request, context) -> {
Expand All @@ -67,6 +71,7 @@ public class RestReindexAction extends AbstractBaseReindexRestHandler<ReindexReq
}
String[] types = extractStringArray(source, "type");
if (types != null) {
deprecationLogger.deprecatedAndMaybeLog("reindex_with_types", TYPES_DEPRECATION_MESSAGE);
cbuescher marked this conversation as resolved.
Show resolved Hide resolved
request.getSearchRequest().types(types);
}
request.setRemoteInfo(buildRemoteInfo(source));
Expand All @@ -81,7 +86,10 @@ public class RestReindexAction extends AbstractBaseReindexRestHandler<ReindexReq

ObjectParser<IndexRequest, Void> destParser = new ObjectParser<>("dest");
destParser.declareString(IndexRequest::index, new ParseField("index"));
destParser.declareString(IndexRequest::type, new ParseField("type"));
destParser.declareString((request, type) -> {
deprecationLogger.deprecatedAndMaybeLog("reindex_with_types", TYPES_DEPRECATION_MESSAGE);
request.type(type);
}, new ParseField("type"));
destParser.declareString(IndexRequest::routing, new ParseField("routing"));
destParser.declareString(IndexRequest::opType, new ParseField("op_type"));
destParser.declareString(IndexRequest::setPipeline, new ParseField("pipeline"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.rest.RestRequest.Method;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.test.rest.RestActionTestCase;
import org.junit.Before;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.singletonMap;
import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;
import static org.mockito.Mockito.mock;

public class RestReindexActionTests extends ESTestCase {
public class RestReindexActionTests extends RestActionTestCase {

private RestReindexAction action;

@Before
public void setUpAction() {
action = new RestReindexAction(Settings.EMPTY, controller());
}

public void testBuildRemoteInfoNoRemote() throws IOException {
assertNull(RestReindexAction.buildRemoteInfo(new HashMap<>()));
}
Expand Down Expand Up @@ -160,8 +169,6 @@ public void testReindexFromRemoteRequestParsing() throws IOException {
}

public void testPipelineQueryParameterIsError() throws IOException {
RestReindexAction action = new RestReindexAction(Settings.EMPTY, mock(RestController.class));

FakeRestRequest.Builder request = new FakeRestRequest.Builder(xContentRegistry());
try (XContentBuilder body = JsonXContent.contentBuilder().prettyPrint()) {
body.startObject(); {
Expand All @@ -185,14 +192,12 @@ public void testPipelineQueryParameterIsError() throws IOException {

public void testSetScrollTimeout() throws IOException {
{
RestReindexAction action = new RestReindexAction(Settings.EMPTY, mock(RestController.class));
FakeRestRequest.Builder requestBuilder = new FakeRestRequest.Builder(xContentRegistry());
requestBuilder.withContent(new BytesArray("{}"), XContentType.JSON);
ReindexRequest request = action.buildRequest(requestBuilder.build());
assertEquals(AbstractBulkByScrollRequest.DEFAULT_SCROLL_TIMEOUT, request.getScrollTime());
}
{
RestReindexAction action = new RestReindexAction(Settings.EMPTY, mock(RestController.class));
FakeRestRequest.Builder requestBuilder = new FakeRestRequest.Builder(xContentRegistry());
requestBuilder.withParams(singletonMap("scroll", "10m"));
requestBuilder.withContent(new BytesArray("{}"), XContentType.JSON);
Expand All @@ -210,4 +215,46 @@ private RemoteInfo buildRemoteInfoHostTestCase(String hostInRest) throws IOExcep

return RestReindexAction.buildRemoteInfo(source);
}

/**
* test deprecation is logged if one or more types are used in source search request inside reindex
*/
public void testTypeInSource() throws IOException {
FakeRestRequest.Builder requestBuilder = new FakeRestRequest.Builder(xContentRegistry())
.withMethod(Method.POST)
.withPath("/_reindex");
XContentBuilder b = JsonXContent.contentBuilder().startObject();
{
b.startObject("source");
{
b.field("type", randomFrom(Arrays.asList("\"t1\"", "[\"t1\", \"t2\"]", "\"_doc\"")));
}
b.endObject();
}
b.endObject();
requestBuilder.withContent(new BytesArray(BytesReference.bytes(b).toBytesRef()), XContentType.JSON);
dispatchRequest(requestBuilder.build());
assertWarnings(RestReindexAction.TYPES_DEPRECATION_MESSAGE);
}

/**
* test deprecation is logged if a type is used in the destination index request inside reindex
*/
public void testTypeInDestination() throws IOException {
FakeRestRequest.Builder requestBuilder = new FakeRestRequest.Builder(xContentRegistry())
.withMethod(Method.POST)
.withPath("/_reindex");
XContentBuilder b = JsonXContent.contentBuilder().startObject();
{
b.startObject("dest");
{
b.field("type", (randomBoolean() ? "_doc" : randomAlphaOfLength(4)));
}
b.endObject();
}
b.endObject();
requestBuilder.withContent(new BytesArray(BytesReference.bytes(b).toBytesRef()), XContentType.JSON);
dispatchRequest(requestBuilder.build());
assertWarnings(RestReindexAction.TYPES_DEPRECATION_MESSAGE);
}
}