Skip to content

Commit

Permalink
Deprecate types in term vector requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtibshirani committed Dec 3, 2018
1 parent 05d52b2 commit 9516344
Show file tree
Hide file tree
Showing 28 changed files with 619 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,18 @@ static Request analyze(AnalyzeRequest request) throws IOException {
}

static Request termVectors(TermVectorsRequest tvrequest) throws IOException {
String endpoint = new EndpointBuilder().addPathPart(
tvrequest.getIndex(), tvrequest.getType(), tvrequest.getId()).addPathPartAsIs("_termvectors").build();
String endpoint;
if (tvrequest.getType() != null) {
endpoint = new EndpointBuilder().addPathPart(tvrequest.getIndex(), tvrequest.getType(), tvrequest.getId())
.addPathPartAsIs("_termvectors")
.build();
} else {
endpoint = new EndpointBuilder().addPathPart(tvrequest.getIndex())
.addPathPartAsIs("_termvectors")
.addPathPart(tvrequest.getId())
.build();
}

Request request = new Request(HttpGet.METHOD_NAME, endpoint);
Params params = new Params(request);
params.withRouting(tvrequest.getRouting());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.client.core;

import org.elasticsearch.client.Validatable;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -31,7 +32,7 @@
public class TermVectorsRequest implements ToXContentObject, Validatable {

private final String index;
private final String type;
@Nullable private final String type;
private String id = null;
private XContentBuilder docBuilder = null;

Expand All @@ -47,25 +48,57 @@ public class TermVectorsRequest implements ToXContentObject, Validatable {
private Map<String, String> perFieldAnalyzer = null;
private Map<String, Integer> filterSettings = null;

/**
* Constructs TermVectorRequest for the given document
*
* @param index - index of the document
* @param docId - id of the document
*/
public TermVectorsRequest(String index, String docId) {
this.index = index;
this.type = null;
this.id = docId;
}

/**
* Constructs TermVectorRequest for the given document
*
* @param index - index of the document
* @param type - type of the document
* @param docId - id of the document
*
* @deprecated Types are in the process of being removed, use
* {@link #TermVectorsRequest(String, String)} instead.
*/
@Deprecated
public TermVectorsRequest(String index, String type, String docId) {
this.index = index;
this.type = type;
this.id = docId;
}

/**
* Constructs TermVectorRequest for an artificial document
*
* @param index - index of the document
* @param docBuilder - an artificial document
*/
public TermVectorsRequest(String index, XContentBuilder docBuilder) {
this.index = index;
this.type = null;
this.docBuilder = docBuilder;
}

/**
* Constructs TermVectorRequest for an artificial document
* @param index - index of the document
* @param type - type of the document
* @param docBuilder - an artificial document
*
* @deprecated Types are in the process of being removed, use
* {@link TermVectorsRequest(String, XContentBuilder)} instead.
*/
@Deprecated
public TermVectorsRequest(String index, String type, XContentBuilder docBuilder) {
this.index = index;
this.type = type;
Expand Down Expand Up @@ -104,7 +137,10 @@ public String getIndex() {

/**
* Returns the type of the request
*
* @deprecated Types are in the process of being removed.
*/
@Deprecated
public String getType() {
return type;
}
Expand Down Expand Up @@ -218,7 +254,9 @@ public boolean getRealtime() {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("_index", index);
builder.field("_type", type);
if (type != null) {
builder.field("_type", type);
}
if (id != null) builder.field("_id", id);
// set values only when different from defaults
if (requestPositions == false) builder.field("positions", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public String getIndex() {

/**
* Returns the type for the response
*
* @deprecated Types are in the process of being removed.
*/
@Deprecated
public String getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ public void testTermvectors() throws IOException {
}
{
// test _termvectors on real documents
TermVectorsRequest tvRequest = new TermVectorsRequest(sourceIndex, "_doc", "1");
TermVectorsRequest tvRequest = new TermVectorsRequest(sourceIndex, "1");
tvRequest.setFields("field");
TermVectorsResponse tvResponse = execute(tvRequest, highLevelClient()::termvectors, highLevelClient()::termvectorsAsync);

Expand All @@ -1160,7 +1160,7 @@ public void testTermvectors() throws IOException {
XContentBuilder docBuilder = XContentFactory.jsonBuilder();
docBuilder.startObject().field("field", "valuex").endObject();

TermVectorsRequest tvRequest = new TermVectorsRequest(sourceIndex, "_doc", docBuilder);
TermVectorsRequest tvRequest = new TermVectorsRequest(sourceIndex, docBuilder);
TermVectorsResponse tvResponse = execute(tvRequest, highLevelClient()::termvectors, highLevelClient()::termvectorsAsync);

TermVectorsResponse.TermVector.Token expectedToken = new TermVectorsResponse.TermVector.Token(0, 6, 0, null);
Expand All @@ -1180,7 +1180,7 @@ public void testTermvectors() throws IOException {

// Not entirely sure if _termvectors belongs to CRUD, and in the absence of a better place, will have it here
public void testTermvectorsWithNonExistentIndex() {
TermVectorsRequest request = new TermVectorsRequest("non-existent", "non-existent", "non-existent");
TermVectorsRequest request = new TermVectorsRequest("non-existent", "non-existent");

ElasticsearchException exception = expectThrows(ElasticsearchException.class,
() -> execute(request, highLevelClient()::termvectors, highLevelClient()::termvectorsAsync));
Expand Down Expand Up @@ -1214,7 +1214,7 @@ public void testMultiTermvectors() throws IOException {
{
// test _mtermvectors where MultiTermVectorsRequest is constructed with ids and a template
String[] expectedIds = {"1", "2"};
TermVectorsRequest tvRequestTemplate = new TermVectorsRequest(sourceIndex, "_doc", "fake_id");
TermVectorsRequest tvRequestTemplate = new TermVectorsRequest(sourceIndex, "fake_id");
tvRequestTemplate.setFields("field");
MultiTermVectorsRequest mtvRequest = new MultiTermVectorsRequest(expectedIds, tvRequestTemplate);

Expand All @@ -1233,13 +1233,13 @@ public void testMultiTermvectors() throws IOException {
{
// test _mtermvectors where MultiTermVectorsRequest constructed with adding each separate request
MultiTermVectorsRequest mtvRequest = new MultiTermVectorsRequest();
TermVectorsRequest tvRequest1 = new TermVectorsRequest(sourceIndex, "_doc", "1");
TermVectorsRequest tvRequest1 = new TermVectorsRequest(sourceIndex, "1");
tvRequest1.setFields("field");
mtvRequest.add(tvRequest1);

XContentBuilder docBuilder = XContentFactory.jsonBuilder();
docBuilder.startObject().field("field", "valuex").endObject();
TermVectorsRequest tvRequest2 = new TermVectorsRequest(sourceIndex, "_doc", docBuilder);
TermVectorsRequest tvRequest2 = new TermVectorsRequest(sourceIndex, docBuilder);
mtvRequest.add(tvRequest2);

MultiTermVectorsResponse mtvResponse =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
import org.elasticsearch.action.support.master.MasterNodeReadRequest;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.action.support.replication.ReplicationRequest;
import org.elasticsearch.client.core.MultiTermVectorsRequest;
import org.elasticsearch.client.core.TermVectorsRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestConverters.EndpointBuilder;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.MultiTermVectorsRequest;
import org.elasticsearch.client.core.TermVectorsRequest;
import org.elasticsearch.common.CheckedBiConsumer;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
Expand Down Expand Up @@ -1266,9 +1266,9 @@ public void testExplain() throws IOException {

public void testTermVectors() throws IOException {
String index = randomAlphaOfLengthBetween(3, 10);
String type = randomAlphaOfLengthBetween(3, 10);
String id = randomAlphaOfLengthBetween(3, 10);
TermVectorsRequest tvRequest = new TermVectorsRequest(index, type, id);

TermVectorsRequest tvRequest = new TermVectorsRequest(index, id);
Map<String, String> expectedParams = new HashMap<>();
String[] fields;
if (randomBoolean()) {
Expand All @@ -1289,7 +1289,7 @@ public void testTermVectors() throws IOException {

Request request = RequestConverters.termVectors(tvRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
endpoint.add(index).add(type).add(id).add("_termvectors");
endpoint.add(index).add("_termvectors").add(id);

assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals(endpoint.toString(), request.getEndpoint());
Expand All @@ -1304,13 +1304,27 @@ public void testTermVectors() throws IOException {
assertToXContentBody(tvRequest, request.getEntity());
}

public void testTermVectorsWithType() throws IOException {
String index = randomAlphaOfLengthBetween(3, 10);
String type = randomAlphaOfLengthBetween(3, 10);
String id = randomAlphaOfLengthBetween(3, 10);
TermVectorsRequest tvRequest = new TermVectorsRequest(index, type, id);

Request request = RequestConverters.termVectors(tvRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
endpoint.add(index).add(type).add(id).add("_termvectors");

assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals(endpoint.toString(), request.getEndpoint());
}

public void testMultiTermVectors() throws IOException {
MultiTermVectorsRequest mtvRequest = new MultiTermVectorsRequest();

int numberOfRequests = randomIntBetween(0, 5);
for (int i = 0; i < numberOfRequests; i++) {
String index = randomAlphaOfLengthBetween(3, 10);
String type = randomAlphaOfLengthBetween(3, 10);
String type = randomFrom(null, randomAlphaOfLengthBetween(3, 10));
String id = randomAlphaOfLengthBetween(3, 10);
TermVectorsRequest tvRequest = new TermVectorsRequest(index, type, id);
String[] fields = generateRandomStringArray(10, 5, false, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1558,18 +1558,16 @@ public void testTermVectors() throws Exception {

{
// tag::term-vectors-request
TermVectorsRequest request = new TermVectorsRequest("authors", "_doc", "1");
TermVectorsRequest request = new TermVectorsRequest("authors", "1");
request.setFields("user");
// end::term-vectors-request
}

{
// tag::term-vectors-request-artificial

XContentBuilder docBuilder = XContentFactory.jsonBuilder();
docBuilder.startObject().field("user", "guest-user").endObject();
TermVectorsRequest request = new TermVectorsRequest("authors",
"_doc",
docBuilder); // <1>
// end::term-vectors-request-artificial

Expand Down Expand Up @@ -1600,7 +1598,7 @@ public void testTermVectors() throws Exception {
// end::term-vectors-request-optional-arguments
}

TermVectorsRequest request = new TermVectorsRequest("authors", "_doc", "1");
TermVectorsRequest request = new TermVectorsRequest("authors", "1");
request.setFields("user");

// tag::term-vectors-execute
Expand Down Expand Up @@ -1687,21 +1685,21 @@ public void testMultiTermVectors() throws Exception {
// tag::multi-term-vectors-request
MultiTermVectorsRequest request = new MultiTermVectorsRequest(); // <1>
TermVectorsRequest tvrequest1 =
new TermVectorsRequest("authors", "_doc", "1");
new TermVectorsRequest("authors", "1");
tvrequest1.setFields("user");
request.add(tvrequest1); // <2>

XContentBuilder docBuilder = XContentFactory.jsonBuilder();
docBuilder.startObject().field("user", "guest-user").endObject();
TermVectorsRequest tvrequest2 =
new TermVectorsRequest("authors", "_doc", docBuilder);
new TermVectorsRequest("authors", docBuilder);
request.add(tvrequest2); // <3>
// end::multi-term-vectors-request
}

// tag::multi-term-vectors-request-template
TermVectorsRequest tvrequestTemplate =
new TermVectorsRequest("authors", "_doc", "fake_id"); // <1>
new TermVectorsRequest("authors", "fake_id"); // <1>
tvrequestTemplate.setFields("user");
String[] ids = {"1", "2"};
MultiTermVectorsRequest request =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ include-tagged::{doc-tests-file}[{api}-request]


The second way can be used when all term vectors requests share the same
arguments, such as index, type, and other settings. In this case, a template
arguments, such as index and other settings. In this case, a template
+{tvrequest}+ can be created with all necessary settings set, and
this template request can be passed to +{request}+ along with all
documents' ids for which to execute these requests.
Expand Down
Loading

0 comments on commit 9516344

Please sign in to comment.