-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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
HLRC API for _termvectors #33447
Changes from 1 commit
2272642
75a0c63
e09d885
7056664
282e04e
227b6bd
e31df93
8a1998c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,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; | ||
|
@@ -1122,6 +1123,26 @@ static Request analyze(AnalyzeRequest request) throws IOException { | |
return req; | ||
} | ||
|
||
static Request termVectors(TermVectorsRequest tvrequest) throws IOException { | ||
String endpoint = "_termvectors"; | ||
if (tvrequest.getId() != null) { | ||
endpoint = new EndpointBuilder().addPathPart( | ||
tvrequest.getIndex(), tvrequest.getType(), tvrequest.getId()).addPathPartAsIs(endpoint).build(); | ||
} else { | ||
endpoint = new EndpointBuilder().addPathPart(tvrequest.getIndex(), tvrequest.getType()).addPathPartAsIs(endpoint).build(); | ||
} | ||
Request request = new Request(HttpGet.METHOD_NAME, endpoint); | ||
Params params = new Params(request); | ||
if (tvrequest.getRouting() != null) params.withRouting(tvrequest.getRouting()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the params methods all call |
||
if (tvrequest.getPreference() != null) params.withPreference(tvrequest.getPreference()); | ||
if (tvrequest.getFields() != null) { | ||
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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
/* | ||
* 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 = index; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.type = type; | ||
this.id = docId; | ||
} | ||
|
||
public String getIndex() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you put these below the other ctor? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
/** | ||
* Sets the fields for which term vectors information should be retrieved | ||
* @param fields | ||
*/ | ||
public void setFields(String... fields) { | ||
this.fields = fields; | ||
} | ||
|
||
public String[] getFields() { | ||
return fields; | ||
} | ||
|
||
/** | ||
* Sets whether to request term positions | ||
* @param requestPositions | ||
*/ | ||
public void setPositions(boolean requestPositions) { | ||
this.requestPositions = requestPositions; | ||
} | ||
|
||
/** | ||
* Sets whether to request term payloads | ||
* @param requestPayloads | ||
*/ | ||
public void setPayloads(boolean requestPayloads) { | ||
this.requestPayloads = requestPayloads; | ||
} | ||
|
||
/** | ||
* Sets whether to request term offsets | ||
* @param requestOffsets | ||
*/ | ||
public void setOffsets(boolean requestOffsets) { | ||
this.requestOffsets = requestOffsets; | ||
} | ||
|
||
/** | ||
* Sets whether to request field statistics | ||
* @param requestFieldStatistics | ||
*/ | ||
public void setFieldStatistics(boolean requestFieldStatistics) { | ||
this.requestFieldStatistics = requestFieldStatistics; | ||
} | ||
|
||
/** | ||
* Sets whether to request term statistics | ||
* @param requestTermStatistics | ||
*/ | ||
public void setTermStatistics(boolean requestTermStatistics) { | ||
this.requestTermStatistics = requestTermStatistics; | ||
} | ||
|
||
/** | ||
* Sets different analyzers than the one at the fields | ||
* @param perFieldAnalyzer | ||
*/ | ||
public void setPerFieldAnalyzer(Map<String, String> perFieldAnalyzer) { | ||
this.perFieldAnalyzer = perFieldAnalyzer; | ||
} | ||
|
||
/** | ||
* Sets an artifical document on what to request _termvectors | ||
* @param docBuilder | ||
*/ | ||
public void setDoc(XContentBuilder docBuilder) { | ||
this.docBuilder = docBuilder; | ||
} | ||
|
||
/** | ||
* Sets conditions for terms filtering | ||
* @param filterSettings | ||
*/ | ||
public void setFilterSettings(Map<String, Integer> filterSettings) { | ||
this.filterSettings = filterSettings; | ||
} | ||
|
||
/** | ||
* Sets a routing to route a request to a particular shard | ||
* @param routing | ||
*/ | ||
public void setRouting(String routing) { | ||
this.routing = routing; | ||
} | ||
|
||
public String getRouting() { | ||
return routing; | ||
} | ||
|
||
/** | ||
* Set a preference of which shard copies to execute the request | ||
* @param preference | ||
*/ | ||
public void setPreference(String preference) { | ||
this.preference = preference; | ||
} | ||
|
||
public String getPreference() { | ||
return preference; | ||
} | ||
|
||
/** | ||
* Sets if the request should be realtime or near-realtime | ||
* @param realtime | ||
*/ | ||
public void setRealtime(boolean realtime) { | ||
this.realtime = realtime; | ||
} | ||
|
||
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
if (filterSettings.containsKey("max_num_terms")) builder.field("max_num_terms", filterSettings.get("max_num_terms")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a helper method here to reduce the copy/paste |
||
if (filterSettings.containsKey("min_term_freq")) builder.field("min_term_freq", filterSettings.get("min_term_freq")); | ||
if (filterSettings.containsKey("max_term_freq")) builder.field("max_term_freq", filterSettings.get("max_term_freq")); | ||
if (filterSettings.containsKey("min_doc_freq")) builder.field("min_doc_freq", filterSettings.get("min_doc_freq")); | ||
if (filterSettings.containsKey("max_doc_freq")) builder.field("max_doc_freq", filterSettings.get("max_doc_freq")); | ||
if (filterSettings.containsKey("min_word_length")) builder.field("min_word_length", filterSettings.get("min_word_length")); | ||
if (filterSettings.containsKey("max_word_length")) builder.field("max_word_length", filterSettings.get("max_word_length")); | ||
builder.endObject(); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the EndpointBuilder() gladly accepts nulls in its
addPathPart(...)
method. You dont need to worry about passing a null value in, as it just wont put it in the path.