Skip to content

Commit

Permalink
Remove doc type specific indexing APIs and relevant changes
Browse files Browse the repository at this point in the history
Signed-off-by: Suraj Singh <[email protected]>
  • Loading branch information
dreamer-89 committed Feb 9, 2022
1 parent bb4d06c commit 5d6864c
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@
import org.opensearch.index.get.GetResult;
import org.opensearch.rest.RestStatus;
import org.opensearch.rest.action.document.RestBulkAction;
import org.opensearch.rest.action.document.RestDeleteAction;
import org.opensearch.rest.action.document.RestGetAction;
import org.opensearch.rest.action.document.RestIndexAction;
import org.opensearch.rest.action.document.RestMultiGetAction;
import org.opensearch.rest.action.document.RestUpdateAction;
import org.opensearch.script.Script;
import org.opensearch.script.ScriptType;
import org.opensearch.search.fetch.subphase.FetchSourceContext;
Expand Down Expand Up @@ -206,31 +202,6 @@ public void testDelete() throws IOException {
}
}

public void testDeleteWithTypes() throws IOException {
String docId = "id";
IndexRequest indexRequest = new IndexRequest("index", "type", docId);
indexRequest.source(Collections.singletonMap("foo", "bar"));
execute(
indexRequest,
highLevelClient()::index,
highLevelClient()::indexAsync,
expectWarningsOnce(RestIndexAction.TYPES_DEPRECATION_MESSAGE)
);

DeleteRequest deleteRequest = new DeleteRequest("index", "type", docId);
DeleteResponse deleteResponse = execute(
deleteRequest,
highLevelClient()::delete,
highLevelClient()::deleteAsync,
expectWarningsOnce(RestDeleteAction.TYPES_DEPRECATION_MESSAGE)
);

assertEquals("index", deleteResponse.getIndex());
assertEquals("type", deleteResponse.getType());
assertEquals(docId, deleteResponse.getId());
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
}

public void testExists() throws IOException {
{
GetRequest getRequest = new GetRequest("index", "id");
Expand Down Expand Up @@ -416,36 +387,6 @@ public void testGet() throws IOException {
}
}

public void testGetWithTypes() throws IOException {
String document = "{\"field\":\"value\"}";
IndexRequest indexRequest = new IndexRequest("index", "type", "id");
indexRequest.source(document, XContentType.JSON);
indexRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
execute(
indexRequest,
highLevelClient()::index,
highLevelClient()::indexAsync,
expectWarningsOnce(RestIndexAction.TYPES_DEPRECATION_MESSAGE)
);

GetRequest getRequest = new GetRequest("index", "type", "id");
GetResponse getResponse = execute(
getRequest,
highLevelClient()::get,
highLevelClient()::getAsync,
expectWarningsOnce(RestGetAction.TYPES_DEPRECATION_MESSAGE)
);

assertEquals("index", getResponse.getIndex());
assertEquals("type", getResponse.getType());
assertEquals("id", getResponse.getId());

assertTrue(getResponse.isExists());
assertFalse(getResponse.isSourceEmpty());
assertEquals(1L, getResponse.getVersion());
assertEquals(document, getResponse.getSourceAsString());
}

public void testMultiGet() throws IOException {
{
MultiGetRequest multiGetRequest = new MultiGetRequest();
Expand Down Expand Up @@ -739,22 +680,6 @@ public void testIndex() throws IOException {
}
}

public void testIndexWithTypes() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
IndexRequest indexRequest = new IndexRequest("index", "some_type", "some_id");
indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("test", "test").endObject());
IndexResponse indexResponse = execute(
indexRequest,
highLevelClient()::index,
highLevelClient()::indexAsync,
expectWarningsOnce(RestIndexAction.TYPES_DEPRECATION_MESSAGE)
);
assertEquals(RestStatus.CREATED, indexResponse.status());
assertEquals("index", indexResponse.getIndex());
assertEquals("some_type", indexResponse.getType());
assertEquals("some_id", indexResponse.getId());
}

public void testUpdate() throws IOException {
{
UpdateRequest updateRequest = new UpdateRequest("index", "does_not_exist");
Expand Down Expand Up @@ -955,29 +880,6 @@ public void testUpdate() throws IOException {
}
}

public void testUpdateWithTypes() throws IOException {
IndexRequest indexRequest = new IndexRequest("index", "type", "id");
indexRequest.source(singletonMap("field", "value"));
IndexResponse indexResponse = execute(
indexRequest,
highLevelClient()::index,
highLevelClient()::indexAsync,
expectWarningsOnce(RestIndexAction.TYPES_DEPRECATION_MESSAGE)
);

UpdateRequest updateRequest = new UpdateRequest("index", "type", "id");
updateRequest.doc(singletonMap("field", "updated"), randomFrom(XContentType.values()));
UpdateResponse updateResponse = execute(
updateRequest,
highLevelClient()::update,
highLevelClient()::updateAsync,
expectWarningsOnce(RestUpdateAction.TYPES_DEPRECATION_MESSAGE)
);

assertEquals(RestStatus.OK, updateResponse.status());
assertEquals(indexResponse.getVersion() + 1, updateResponse.getVersion());
}

public void testBulk() throws IOException {
int nbItems = randomIntBetween(10, 100);
boolean[] errors = new boolean[nbItems];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import org.opensearch.join.aggregations.Children;
import org.opensearch.join.aggregations.ChildrenAggregationBuilder;
import org.opensearch.rest.RestStatus;
import org.opensearch.rest.action.document.RestIndexAction;
import org.opensearch.script.Script;
import org.opensearch.script.ScriptType;
import org.opensearch.script.mustache.MultiSearchTemplateRequest;
Expand Down Expand Up @@ -125,24 +124,19 @@ public class SearchIT extends OpenSearchRestHighLevelClientTestCase {
@Before
public void indexDocuments() throws IOException {
{
Request doc1 = new Request(HttpPut.METHOD_NAME, "/index/type/1");
doc1.setOptions(expectWarningsOnce(RestIndexAction.TYPES_DEPRECATION_MESSAGE));
Request doc1 = new Request(HttpPut.METHOD_NAME, "/index/_doc/1");
doc1.setJsonEntity("{\"type\":\"type1\", \"id\":1, \"num\":10, \"num2\":50}");
client().performRequest(doc1);
Request doc2 = new Request(HttpPut.METHOD_NAME, "/index/type/2");
doc2.setOptions(expectWarningsOnce(RestIndexAction.TYPES_DEPRECATION_MESSAGE));
Request doc2 = new Request(HttpPut.METHOD_NAME, "/index/_doc/2");
doc2.setJsonEntity("{\"type\":\"type1\", \"id\":2, \"num\":20, \"num2\":40}");
client().performRequest(doc2);
Request doc3 = new Request(HttpPut.METHOD_NAME, "/index/type/3");
doc3.setOptions(expectWarningsOnce(RestIndexAction.TYPES_DEPRECATION_MESSAGE));
Request doc3 = new Request(HttpPut.METHOD_NAME, "/index/_doc/3");
doc3.setJsonEntity("{\"type\":\"type1\", \"id\":3, \"num\":50, \"num2\":35}");
client().performRequest(doc3);
Request doc4 = new Request(HttpPut.METHOD_NAME, "/index/type/4");
doc4.setOptions(expectWarningsOnce(RestIndexAction.TYPES_DEPRECATION_MESSAGE));
Request doc4 = new Request(HttpPut.METHOD_NAME, "/index/_doc/4");
doc4.setJsonEntity("{\"type\":\"type2\", \"id\":4, \"num\":100, \"num2\":10}");
client().performRequest(doc4);
Request doc5 = new Request(HttpPut.METHOD_NAME, "/index/type/5");
doc5.setOptions(expectWarningsOnce(RestIndexAction.TYPES_DEPRECATION_MESSAGE));
Request doc5 = new Request(HttpPut.METHOD_NAME, "/index/_doc/5");
doc5.setJsonEntity("{\"type\":\"type2\", \"id\":5, \"num\":100, \"num2\":10}");
client().performRequest(doc5);
}
Expand Down Expand Up @@ -241,13 +235,11 @@ public void testSearchNoQuery() throws IOException {
assertEquals(5, searchResponse.getHits().getHits().length);
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
assertEquals("index", searchHit.getIndex());
assertEquals("type", searchHit.getType());
assertThat(Integer.valueOf(searchHit.getId()), both(greaterThan(0)).and(lessThan(6)));
assertEquals(1.0f, searchHit.getScore(), 0);
assertEquals(-1L, searchHit.getVersion());
assertNotNull(searchHit.getSourceAsMap());
assertEquals(4, searchHit.getSourceAsMap().size());
assertTrue(searchHit.getSourceAsMap().containsKey("type"));
assertTrue(searchHit.getSourceAsMap().containsKey("num"));
assertTrue(searchHit.getSourceAsMap().containsKey("num2"));
}
Expand All @@ -266,7 +258,6 @@ public void testSearchMatchQuery() throws IOException {
assertThat(searchResponse.getHits().getMaxScore(), greaterThan(0f));
SearchHit searchHit = searchResponse.getHits().getHits()[0];
assertEquals("index", searchHit.getIndex());
assertEquals("type", searchHit.getType());
assertEquals("1", searchHit.getId());
assertThat(searchHit.getScore(), greaterThan(0f));
assertEquals(-1L, searchHit.getVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.opensearch.common.xcontent.support.XContentMapValues;
import org.opensearch.index.IndexSettings;
import org.opensearch.rest.action.document.RestBulkAction;
import org.opensearch.rest.action.document.RestIndexAction;
import org.opensearch.rest.action.document.RestUpdateAction;
import org.opensearch.rest.action.search.RestExplainAction;
import org.opensearch.test.NotEqualMessageBuilder;
Expand Down Expand Up @@ -989,9 +988,6 @@ public void testSoftDeletes() throws Exception {
for (int i = 0; i < numDocs; i++) {
String doc = Strings.toString(JsonXContent.contentBuilder().startObject().field("field", "v1").endObject());
Request request = new Request("POST", "/" + index + "/" + type + "/" + i);
if (isRunningAgainstAncientCluster() == false) {
request.setOptions(expectWarnings(RestIndexAction.TYPES_DEPRECATION_MESSAGE));
}
request.setJsonEntity(doc);
client().performRequest(request);
refresh();
Expand Down Expand Up @@ -1258,9 +1254,6 @@ private void saveInfoDocument(String type, String value) throws IOException {
Request request = new Request("PUT", "/info/" + this.type + "/" + index + "_" + type);
request.addParameter("op_type", "create");
request.setJsonEntity(Strings.toString(infoDoc));
if (isRunningAgainstAncientCluster() == false) {
request.setOptions(expectWarnings(RestIndexAction.TYPES_DEPRECATION_MESSAGE));
}
client().performRequest(request);
}

Expand Down Expand Up @@ -1518,9 +1511,6 @@ public void testSystemIndexMetadataIsUpgraded() throws Exception {
bulk.addParameter("refresh", "true");
bulk.setJsonEntity("{\"index\": {\"_index\": \"test_index_old\", \"_type\" : \"" + type + "\"}}\n" +
"{\"f1\": \"v1\", \"f2\": \"v2\"}\n");
if (isRunningAgainstAncientCluster() == false) {
bulk.setOptions(expectWarnings(RestBulkAction.TYPES_DEPRECATION_MESSAGE));
}
client().performRequest(bulk);

// start a async reindex job
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ private int indexDocs(String index, final int idStart, final int numDocs) throws
final int id = idStart + i;
Request request = new Request("PUT", index + "/doc/" + id);
request.setJsonEntity("{\"test\": \"test_" + randomAlphaOfLength(2) + "\"}");
request.setOptions(expectWarnings(RestIndexAction.TYPES_DEPRECATION_MESSAGE));
assertOK(client().performRequest(request));
}
return numDocs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ private int indexDocs(String index, final int idStart, final int numDocs) throws
final int id = idStart + i;
Request indexDoc = new Request("PUT", index + "/test/" + id);
indexDoc.setJsonEntity("{\"test\": \"test_" + randomAsciiOfLength(2) + "\"}");
indexDoc.setOptions(expectWarnings(RestIndexAction.TYPES_DEPRECATION_MESSAGE));
client().performRequest(indexDoc);
}
return numDocs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
import org.opensearch.action.support.ActiveShardCount;
import org.opensearch.client.node.NodeClient;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.common.logging.DeprecationLogger;
import org.opensearch.index.VersionType;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.action.RestActions;
Expand All @@ -57,21 +55,10 @@
import static org.opensearch.rest.RestRequest.Method.PUT;

public class RestIndexAction extends BaseRestHandler {
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestDeleteAction.class);
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in document "
+ "index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, "
+ "or /{index}/_create/{id}).";

@Override
public List<Route> routes() {
return unmodifiableList(
asList(
new Route(POST, "/{index}/_doc/{id}"),
new Route(PUT, "/{index}/_doc/{id}"),
new Route(POST, "/{index}/{type}/{id}"),
new Route(PUT, "/{index}/{type}/{id}")
)
);
return unmodifiableList(asList(new Route(POST, "/{index}/_doc/{id}"), new Route(PUT, "/{index}/_doc/{id}")));
}

@Override
Expand All @@ -88,14 +75,7 @@ public String getName() {

@Override
public List<Route> routes() {
return unmodifiableList(
asList(
new Route(POST, "/{index}/_create/{id}"),
new Route(PUT, "/{index}/_create/{id}"),
new Route(POST, "/{index}/{type}/{id}/_create"),
new Route(PUT, "/{index}/{type}/{id}/_create")
)
);
return unmodifiableList(asList(new Route(POST, "/{index}/_create/{id}"), new Route(PUT, "/{index}/_create/{id}")));
}

@Override
Expand Down Expand Up @@ -127,7 +107,7 @@ public String getName() {

@Override
public List<Route> routes() {
return unmodifiableList(asList(new Route(POST, "/{index}/_doc"), new Route(POST, "/{index}/{type}")));
return unmodifiableList(asList(new Route(POST, "/{index}/_doc")));
}

@Override
Expand All @@ -145,13 +125,8 @@ public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
IndexRequest indexRequest;
final String type = request.param("type");
if (type != null && type.equals(MapperService.SINGLE_MAPPING_NAME) == false) {
deprecationLogger.deprecate("index_with_types", TYPES_DEPRECATION_MESSAGE);
indexRequest = new IndexRequest(request.param("index"), type, request.param("id"));
} else {
indexRequest = new IndexRequest(request.param("index"));
indexRequest.id(request.param("id"));
}
indexRequest = new IndexRequest(request.param("index"));
indexRequest.id(request.param("id"));
indexRequest.routing(request.param("routing"));
indexRequest.setPipeline(request.param("pipeline"));
indexRequest.source(request.requiredContent(), request.getXContentType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,14 @@ public void setUpAction() {
controller().registerHandler(new AutoIdHandler(() -> clusterStateSupplier.get().nodes()));
}

public void testTypeInPath() {
RestRequest deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT)
.withPath("/some_index/some_type/some_id")
.build();
dispatchRequest(deprecatedRequest);
assertWarnings(RestIndexAction.TYPES_DEPRECATION_MESSAGE);

public void testPath() {
RestRequest validRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT)
.withPath("/some_index/_doc/some_id")
.build();
dispatchRequest(validRequest);
}

public void testCreateWithTypeInPath() {
RestRequest deprecatedRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT)
.withPath("/some_index/some_type/some_id/_create")
.build();
dispatchRequest(deprecatedRequest);
assertWarnings(RestIndexAction.TYPES_DEPRECATION_MESSAGE);

public void testCreatePath() {
RestRequest validRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT)
.withPath("/some_index/_create/some_id")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,8 @@ private static void deleteAllSLMPolicies() throws IOException {
Response response = adminClient().performRequest(new Request("GET", "/_slm/policy"));
policies = entityAsMap(response);
} catch (ResponseException e) {
if (RestStatus.METHOD_NOT_ALLOWED.getStatus() == e.getResponse().getStatusLine().getStatusCode()) {
if (RestStatus.METHOD_NOT_ALLOWED.getStatus() == e.getResponse().getStatusLine().getStatusCode()
|| RestStatus.BAD_REQUEST.getStatus() == e.getResponse().getStatusLine().getStatusCode()) {
// If bad request returned, SLM is not enabled.
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

/**
* Runs a suite of yaml tests shared with all the official OpenSearch
* clients against against an opensearch cluster.
* clients against an opensearch cluster.
*
* The suite timeout is extended to account for projects with a large number of tests.
*/
Expand Down

0 comments on commit 5d6864c

Please sign in to comment.