use of org.opensearch.client.core.TermVectorsRequest in project OpenSearch by opensearch-project.
the class RequestConverters method termVectors.
static Request termVectors(TermVectorsRequest tvrequest) throws IOException {
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();
params.withRouting(tvrequest.getRouting());
params.withPreference(tvrequest.getPreference());
params.withRealtime(tvrequest.getRealtime());
request.addParameters(params.asMap());
request.setEntity(createEntity(tvrequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}
use of org.opensearch.client.core.TermVectorsRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testMultiTermVectorsWithType.
public void testMultiTermVectorsWithType() 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());
}
use of org.opensearch.client.core.TermVectorsRequest in project OpenSearch by opensearch-project.
the class CrudIT method testTermvectorsWithNonExistentIndex.
// 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");
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(request, highLevelClient()::termvectors, highLevelClient()::termvectorsAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
}
use of org.opensearch.client.core.TermVectorsRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testTermVectors.
public void testTermVectors() throws IOException {
String index = randomAlphaOfLengthBetween(3, 10);
String id = randomAlphaOfLengthBetween(3, 10);
TermVectorsRequest tvRequest = new TermVectorsRequest(index, id);
Map<String, String> expectedParams = new HashMap<>();
String[] fields;
if (randomBoolean()) {
String routing = randomAlphaOfLengthBetween(3, 10);
tvRequest.setRouting(routing);
expectedParams.put("routing", routing);
}
if (randomBoolean()) {
tvRequest.setRealtime(false);
expectedParams.put("realtime", "false");
}
boolean hasFields = randomBoolean();
if (hasFields) {
fields = generateRandomStringArray(10, 5, false, false);
tvRequest.setFields(fields);
}
Request request = RequestConverters.termVectors(tvRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
endpoint.add(index).add("_termvectors").add(id);
assertEquals(HttpGet.METHOD_NAME, request.getMethod());
assertEquals(endpoint.toString(), request.getEndpoint());
for (Map.Entry<String, String> param : expectedParams.entrySet()) {
assertThat(request.getParameters(), hasEntry(param.getKey(), param.getValue()));
}
assertToXContentBody(tvRequest, request.getEntity());
}
use of org.opensearch.client.core.TermVectorsRequest in project OpenSearch by opensearch-project.
the class CRUDDocumentationIT method testMultiTermVectors.
// Not entirely sure if _mtermvectors belongs to CRUD, and in the absence of a better place, will have it here
public void testMultiTermVectors() throws Exception {
RestHighLevelClient client = highLevelClient();
CreateIndexRequest authorsRequest = new CreateIndexRequest("authors").mapping(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("user").field("type", "keyword").endObject().endObject().endObject());
CreateIndexResponse authorsResponse = client.indices().create(authorsRequest, RequestOptions.DEFAULT);
assertTrue(authorsResponse.isAcknowledged());
client.index(new IndexRequest("index").id("1").source("user", "foobar"), RequestOptions.DEFAULT);
client.index(new IndexRequest("index").id("2").source("user", "baz"), RequestOptions.DEFAULT);
Response refreshResponse = client().performRequest(new Request("POST", "/authors/_refresh"));
assertEquals(200, refreshResponse.getStatusLine().getStatusCode());
{
// tag::multi-term-vectors-request
// <1>
MultiTermVectorsRequest request = new MultiTermVectorsRequest();
TermVectorsRequest tvrequest1 = new TermVectorsRequest("authors", "1");
tvrequest1.setFields("user");
// <2>
request.add(tvrequest1);
XContentBuilder docBuilder = XContentFactory.jsonBuilder();
docBuilder.startObject().field("user", "guest-user").endObject();
TermVectorsRequest tvrequest2 = new TermVectorsRequest("authors", docBuilder);
// <3>
request.add(tvrequest2);
// end::multi-term-vectors-request
}
// tag::multi-term-vectors-request-template
TermVectorsRequest tvrequestTemplate = // <1>
new TermVectorsRequest("authors", "fake_id");
tvrequestTemplate.setFields("user");
String[] ids = { "1", "2" };
MultiTermVectorsRequest request = // <2>
new MultiTermVectorsRequest(ids, tvrequestTemplate);
// end::multi-term-vectors-request-template
// tag::multi-term-vectors-execute
MultiTermVectorsResponse response = client.mtermvectors(request, RequestOptions.DEFAULT);
// end::multi-term-vectors-execute
// tag::multi-term-vectors-response
List<TermVectorsResponse> tvresponseList = // <1>
response.getTermVectorsResponses();
if (tvresponseList != null) {
for (TermVectorsResponse tvresponse : tvresponseList) {
}
}
// end::multi-term-vectors-response
ActionListener<MultiTermVectorsResponse> listener;
// tag::multi-term-vectors-execute-listener
listener = new ActionListener<MultiTermVectorsResponse>() {
@Override
public void onResponse(MultiTermVectorsResponse mtvResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::multi-term-vectors-execute-listener
CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::multi-term-vectors-execute-async
client.mtermvectorsAsync(request, RequestOptions.DEFAULT, // <1>
listener);
// end::multi-term-vectors-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Aggregations