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

HLRC for _mtermvectors #35266

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.MultiTermVectorsRequest;
import org.elasticsearch.client.security.RefreshPolicy;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.Nullable;
Expand Down Expand Up @@ -617,6 +618,13 @@ static Request termVectors(TermVectorsRequest tvrequest) throws IOException {
return request;
}

static Request mtermVectors(MultiTermVectorsRequest mtvrequest) throws IOException {
String endpoint = "_mtermvectors";
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
request.setEntity(createEntity(mtvrequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}

static Request getScript(GetStoredScriptRequest getStoredScriptRequest) {
String endpoint = new EndpointBuilder().addPathPartAsIs("_scripts").addPathPart(getStoredScriptRequest.id()).build();
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.client.core.TermVectorsResponse;
import org.elasticsearch.client.core.TermVectorsRequest;
import org.elasticsearch.client.core.MultiTermVectorsRequest;
import org.elasticsearch.client.core.MultiTermVectorsResponse;
import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.CheckedFunction;
import org.elasticsearch.common.ParseField;
Expand Down Expand Up @@ -1128,6 +1130,37 @@ public final void termvectorsAsync(TermVectorsRequest request, RequestOptions op
}


/**
* Calls the Multi Term Vectors API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-termvectors.html">Multi Term Vectors API
* on elastic.co</a>
*
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
*/
public final MultiTermVectorsResponse mtermvectors(MultiTermVectorsRequest request, RequestOptions options) throws IOException {
return performRequestAndParseEntity(
request, RequestConverters::mtermVectors, options, MultiTermVectorsResponse::fromXContent, emptySet());
}


/**
* Asynchronously calls the Multi Term Vectors API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-termvectors.html">Multi Term Vectors API
* on elastic.co</a>
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*/
public final void mtermvectorsAsync(MultiTermVectorsRequest request, RequestOptions options,
ActionListener<MultiTermVectorsResponse> listener) {
performRequestAsyncAndParseEntity(
request, RequestConverters::mtermVectors, options, MultiTermVectorsResponse::fromXContent, listener, emptySet());
}


/**
* Executes a request using the Ranking Evaluation API.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-rank-eval.html">Ranking Evaluation API
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.core;

import org.elasticsearch.client.Validatable;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.elasticsearch.client.core.TermVectorsRequest.createFromTemplate;

public class MultiTermVectorsRequest implements ToXContentObject, Validatable {

private List<TermVectorsRequest> requests = new ArrayList<>();

/**
* Constructs an empty MultiTermVectorsRequest
* After that use {@code add} method to add individual {@code TermVectorsRequest} to it.
*/
public MultiTermVectorsRequest() {};

/**
* Constructs a MultiTermVectorsRequest from the given document ids
* and a template {@code TermVectorsRequest}.
* Used when individual requests share the same index, type and other settings.
* @param ids - ids of documents for which term vectors are requested
* @param template - a template {@code TermVectorsRequest} that allows to set all
* settings only once for all requests.
*/
public MultiTermVectorsRequest(String[] ids, TermVectorsRequest template) {
for (String id : ids) {
TermVectorsRequest request = createFromTemplate(template, id);
requests.add(request);
}
}

/**
* Adds another {@code TermVectorsRequest} to this {@code MultiTermVectorsRequest}
* @param request - {@code TermVectorsRequest} to add
*/
public void add(TermVectorsRequest request) {
requests.add(request);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.startArray("docs");
for (TermVectorsRequest request : requests) {
request.toXContent(builder, params);
}
builder.endArray();
builder.endObject();
return builder;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.core;


import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.util.List;
import java.util.Objects;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;

public class MultiTermVectorsResponse {
private final List<TermVectorsResponse> responses;

public MultiTermVectorsResponse(List<TermVectorsResponse> responses) {
this.responses = responses;
}

private static ConstructingObjectParser<MultiTermVectorsResponse, Void> PARSER =
new ConstructingObjectParser<>("multi_term_vectors", true,
args -> {
// as the response comes from server, we are sure that args[0] will be a list of TermVectorsResponse
@SuppressWarnings("unchecked") List<TermVectorsResponse> termVectorsResponsesList = (List<TermVectorsResponse>) args[0];
return new MultiTermVectorsResponse(termVectorsResponsesList);
}
);

static {
PARSER.declareObjectArray(constructorArg(), (p,c) -> TermVectorsResponse.fromXContent(p), new ParseField("docs"));
}

public static MultiTermVectorsResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}

/**
* Returns the list of {@code TermVectorsResponse} for this {@code MultiTermVectorsResponse}
*/
public List<TermVectorsResponse> getTermVectorsResponses() {
return responses;
}


@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof MultiTermVectorsResponse)) return false;
MultiTermVectorsResponse other = (MultiTermVectorsResponse) obj;
return Objects.equals(responses, other.responses);
}

@Override
public int hashCode() {
return Objects.hash(responses);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ public TermVectorsRequest(String index, String type) {
this.type = type;
}


/**
* Constructs a new TermVectorRequest from a template
* using the provided document id
* @param template - a term vector request served as a template
* @param id - id of the requested document
*/
static TermVectorsRequest createFromTemplate(TermVectorsRequest template, String id) {
TermVectorsRequest request = new TermVectorsRequest(template.getIndex(), template.getType(), id);
request.realtime = template.getRealtime();
request.requestPositions = template.requestPositions;
request.requestPayloads = template.requestPayloads;
request.requestOffsets = template.requestOffsets;
request.requestFieldStatistics = template.requestFieldStatistics;
request.requestTermStatistics = template.requestTermStatistics;
if (template.routing != null) request.setRouting(template.getRouting());
if (template.preference != null) request.setPreference(template.getPreference());
if (template.fields != null) request.setFields(template.getFields());
if (template.perFieldAnalyzer != null) request.setPerFieldAnalyzer(template.perFieldAnalyzer);
if (template.filterSettings != null) request.setFilterSettings(template.filterSettings);
return request;
}

/**
* Returns the index of the request
*/
Expand Down Expand Up @@ -197,6 +220,9 @@ public boolean getRealtime() {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("_index", index);
builder.field("_type", type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know @jtibshirani has done some work on removing types from 7.0, and I dont know how this may or may not be impacted. @jtibshirani does this need to be different at all based on the work you did in #35421 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the ping! I don't think we need to worry about it in this PR. We'll do a separate pass through each affected API to add the right deprecations (including the Java HLRC).

if (id != null) builder.field("_id", id);
// set values only when different from defaults
if (requestPositions == false) builder.field("positions", false);
if (requestPayloads == false) builder.field("payloads", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.elasticsearch.action.support.WriteRequest.RefreshPolicy;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.core.MultiTermVectorsRequest;
import org.elasticsearch.client.core.MultiTermVectorsResponse;
import org.elasticsearch.client.core.TermVectorsRequest;
import org.elasticsearch.client.core.TermVectorsResponse;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -75,6 +77,7 @@
import org.joda.time.format.DateTimeFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1290,4 +1293,69 @@ public void testTermvectorsWithNonExistentIndex() {
() -> execute(request, highLevelClient()::termvectors, highLevelClient()::termvectorsAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
}

// Not entirely sure if _mtermvectors belongs to CRUD, and in the absence of a better place, will have it here
public void testMultiTermvectors() throws IOException {
final String sourceIndex = "index1";
{
// prepare : index docs
Settings settings = Settings.builder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
String mappings = "\"_doc\":{\"properties\":{\"field\":{\"type\":\"text\"}}}";
createIndex(sourceIndex, settings, mappings);
assertEquals(
RestStatus.OK,
highLevelClient().bulk(
new BulkRequest()
.add(new IndexRequest(sourceIndex, "_doc", "1")
.source(Collections.singletonMap("field", "value1"), XContentType.JSON))
.add(new IndexRequest(sourceIndex, "_doc", "2")
.source(Collections.singletonMap("field", "value2"), XContentType.JSON))
.setRefreshPolicy(RefreshPolicy.IMMEDIATE),
RequestOptions.DEFAULT
).status()
);
}
{
// test _mtermvectors where MultiTermVectorsRequest is constructed with ids and a template
String[] expectedIds = {"1", "2"};
TermVectorsRequest tvRequestTemplate = new TermVectorsRequest(sourceIndex, "_doc");
tvRequestTemplate.setFields("field");
MultiTermVectorsRequest mtvRequest = new MultiTermVectorsRequest(expectedIds, tvRequestTemplate);

MultiTermVectorsResponse mtvResponse =
execute(mtvRequest, highLevelClient()::mtermvectors, highLevelClient()::mtermvectorsAsync);

List<String> ids = new ArrayList<>();
for (TermVectorsResponse tvResponse: mtvResponse.getTermVectorsResponses()) {
assertThat(tvResponse.getIndex(), equalTo(sourceIndex));
assertTrue(tvResponse.getFound());
ids.add(tvResponse.getId());
}
assertArrayEquals(expectedIds, ids.toArray());
}

{
// test _mtermvectors where MultiTermVectorsRequest constructed with adding each separate request
MultiTermVectorsRequest mtvRequest = new MultiTermVectorsRequest();
TermVectorsRequest tvRequest1 = new TermVectorsRequest(sourceIndex, "_doc", "1");
tvRequest1.setFields("field");
mtvRequest.add(tvRequest1);
TermVectorsRequest tvRequest2 = new TermVectorsRequest(sourceIndex, "_doc");
XContentBuilder docBuilder = XContentFactory.jsonBuilder();
docBuilder.startObject().field("field", "valuex").endObject();
tvRequest2.setDoc(docBuilder);
mtvRequest.add(tvRequest2);

MultiTermVectorsResponse mtvResponse =
execute(mtvRequest, highLevelClient()::mtermvectors, highLevelClient()::mtermvectorsAsync);
for (TermVectorsResponse tvResponse: mtvResponse.getTermVectorsResponses()) {
assertThat(tvResponse.getIndex(), equalTo(sourceIndex));
assertTrue(tvResponse.getFound());
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
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;
Expand Down Expand Up @@ -1301,6 +1302,26 @@ public void testTermVectors() throws IOException {
assertToXContentBody(tvRequest, request.getEntity());
}

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 id = randomAlphaOfLengthBetween(3, 10);
TermVectorsRequest tvRequest = new TermVectorsRequest(index, type, id);
String[] fields = generateRandomStringArray(10, 5, false, false);
tvRequest.setFields(fields);
mtvRequest.add(tvRequest);
}

Request request = RequestConverters.mtermVectors(mtvRequest);
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals("_mtermvectors", request.getEndpoint());
assertToXContentBody(mtvRequest, request.getEntity());
}

public void testFieldCaps() {
// Create a random request.
String[] indices = randomIndicesNames(0, 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,6 @@ public void testApiNamingConventions() throws Exception {
"indices.exists_type",
"indices.get_upgrade",
"indices.put_alias",
"mtermvectors",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay!

"render_search_template",
"scripts_painless_execute",
"tasks.get"
Expand Down
Loading