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 API for _termvectors #33447

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import org.elasticsearch.script.mustache.SearchTemplateRequest;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.client.core.TermVectorsRequest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -578,6 +579,19 @@ static Request analyze(AnalyzeRequest request) throws IOException {
return req;
}

static Request termVectors(TermVectorsRequest tvrequest) throws IOException {
String endpoint = new EndpointBuilder().addPathPart(
tvrequest.getIndex(), tvrequest.getType(), tvrequest.getId()).addPathPartAsIs("_termvectors").build();
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
Params params = new Params(request);
params.withRouting(tvrequest.getRouting());
params.withPreference(tvrequest.getPreference());
params.withFields(tvrequest.getFields());
params.withRealtime(tvrequest.getRealtime());
request.setEntity(createEntity(tvrequest, 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 @@ -56,6 +56,8 @@
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.core.TermVectorsResponse;
import org.elasticsearch.client.core.TermVectorsRequest;
import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.CheckedFunction;
import org.elasticsearch.common.ParseField;
Expand Down Expand Up @@ -1029,6 +1031,36 @@ public final void explainAsync(ExplainRequest explainRequest, RequestOptions opt
listener, singleton(404));
}


/**
* Calls the Term Vectors API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html">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 TermVectorsResponse termvectors(TermVectorsRequest request, RequestOptions options) throws IOException {
return performRequestAndParseEntity(request, RequestConverters::termVectors, options, TermVectorsResponse::fromXContent,
emptySet());
}

/**
* Asynchronously calls the Term Vectors API
*
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html">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 termvectorsAsync(TermVectorsRequest request, RequestOptions options, ActionListener<TermVectorsResponse> listener) {
performRequestAsyncAndParseEntity(request, RequestConverters::termVectors, options, TermVectorsResponse::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,228 @@
/*
* 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.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class TermVectorsRequest implements ToXContentObject, Validatable {

private final String index;
private final String type;
private String id = null;
private String routing = null;
private String preference = null;
private boolean realtime = true;
private String[] fields = null;
private boolean requestPositions = true;
private boolean requestPayloads = true;
private boolean requestOffsets = true;
private boolean requestFieldStatistics = true;
private boolean requestTermStatistics = false;
private Map<String, String> perFieldAnalyzer = null;
private Map<String, Integer> filterSettings = null;
private XContentBuilder docBuilder = null;


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

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

/**
* Returns the index of the request
*/
public String getIndex() {
Copy link
Member

Choose a reason for hiding this comment

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

Could you put these below the other ctor?

Copy link
Member

Choose a reason for hiding this comment

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

And maybe add javadoc for them like you did with the other methods?

return index;
}

/**
* Returns the type of the request
*/
public String getType() {
return type;
}

/**
* Returns the id of the request
* can be NULL if there is no document ID
*/
public String getId() {
return id;
}

/**
* Sets the fields for which term vectors information should be retrieved
*/
public void setFields(String... fields) {
this.fields = fields;
}

public String[] getFields() {
return fields;
}

/**
* Sets whether to request term positions
*/
public void setPositions(boolean requestPositions) {
this.requestPositions = requestPositions;
}

/**
* Sets whether to request term payloads
*/
public void setPayloads(boolean requestPayloads) {
this.requestPayloads = requestPayloads;
}

/**
* Sets whether to request term offsets
*/
public void setOffsets(boolean requestOffsets) {
this.requestOffsets = requestOffsets;
}

/**
* Sets whether to request field statistics
*/
public void setFieldStatistics(boolean requestFieldStatistics) {
this.requestFieldStatistics = requestFieldStatistics;
}

/**
* Sets whether to request term statistics
*/
public void setTermStatistics(boolean requestTermStatistics) {
this.requestTermStatistics = requestTermStatistics;
}

/**
* Sets different analyzers than the one at the fields
*/
public void setPerFieldAnalyzer(Map<String, String> perFieldAnalyzer) {
this.perFieldAnalyzer = perFieldAnalyzer;
}

/**
* Sets an artifical document on what to request _termvectors
*/
public void setDoc(XContentBuilder docBuilder) {
this.docBuilder = docBuilder;
}

/**
* Sets conditions for terms filtering
*/
public void setFilterSettings(Map<String, Integer> filterSettings) {
this.filterSettings = filterSettings;
}

/**
* Sets a routing to route a request to a particular shard
*/
public void setRouting(String routing) {
this.routing = routing;
}

public String getRouting() {
return routing;
}

/**
* Set a preference of which shard copies to execute the request
*/
public void setPreference(String preference) {
this.preference = preference;
}

public String getPreference() {
return preference;
}

/**
* Sets if the request should be realtime or near-realtime
*/
public void setRealtime(boolean realtime) {
this.realtime = realtime;
}

/**
* Returns if the request is realtime(true) or near-realtime(false)
*/
public boolean getRealtime() {
return realtime;
}


@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
// set values only when different from defaults
if (requestPositions == false) builder.field("positions", false);
if (requestPayloads == false) builder.field("payloads", false);
if (requestOffsets == false) builder.field("offsets", false);
if (requestFieldStatistics == false) builder.field("field_statistics", false);
if (requestTermStatistics) builder.field("term_statistics", true);
if (perFieldAnalyzer != null) builder.field("per_field_analyzer", perFieldAnalyzer);

if (docBuilder != null) {
BytesReference doc = BytesReference.bytes(docBuilder);
try (InputStream stream = doc.streamInput()) {
builder.rawField("doc", stream, docBuilder.contentType());
Copy link
Member

Choose a reason for hiding this comment

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

I really don't like the name of this method! It makes me think we're copying the field raw without checking the content type but we are checking it.

}
}

if (filterSettings != null) {
builder.startObject("filter");
String[] filterSettingNames =
{"max_num_terms", "min_term_freq", "max_term_freq", "min_doc_freq", "max_doc_freq", "min_word_length", "max_word_length"};
for (String settingName : filterSettingNames) {
if (filterSettings.containsKey(settingName)) builder.field(settingName, filterSettings.get(settingName));
Copy link
Member

Choose a reason for hiding this comment

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

Should these be a class rather than a free form map? It looks like this'd silently not serialize some stuff and that seems bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All these parameters are optional, and a request doesn't require any of them, that is why I think it is fine to have it as a map.

}
builder.endObject();
}
builder.endObject();
return builder;
}

}