use of org.opensearch.client.core.TermVectorsResponse 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));
}
use of org.opensearch.client.core.TermVectorsResponse in project OpenSearch by opensearch-project.
the class CRUDDocumentationIT method testTermVectors.
// Not entirely sure if _termvectors belongs to CRUD, and in the absence of a better place, will have it here
public void testTermVectors() 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);
Response refreshResponse = client().performRequest(new Request("POST", "/authors/_refresh"));
assertEquals(200, refreshResponse.getStatusLine().getStatusCode());
{
// tag::term-vectors-request
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", // <1>
docBuilder);
// end::term-vectors-request-artificial
// tag::term-vectors-request-optional-arguments
// <1>
request.setFieldStatistics(false);
// <2>
request.setTermStatistics(true);
// <3>
request.setPositions(false);
// <4>
request.setOffsets(false);
// <5>
request.setPayloads(false);
Map<String, Integer> filterSettings = new HashMap<>();
filterSettings.put("max_num_terms", 3);
filterSettings.put("min_term_freq", 1);
filterSettings.put("max_term_freq", 10);
filterSettings.put("min_doc_freq", 1);
filterSettings.put("max_doc_freq", 100);
filterSettings.put("min_word_length", 1);
filterSettings.put("max_word_length", 10);
// <6>
request.setFilterSettings(filterSettings);
Map<String, String> perFieldAnalyzer = new HashMap<>();
perFieldAnalyzer.put("user", "keyword");
// <7>
request.setPerFieldAnalyzer(perFieldAnalyzer);
// <8>
request.setRealtime(false);
// <9>
request.setRouting("routing");
// end::term-vectors-request-optional-arguments
}
TermVectorsRequest request = new TermVectorsRequest("authors", "1");
request.setFields("user");
// tag::term-vectors-execute
TermVectorsResponse response = client.termvectors(request, RequestOptions.DEFAULT);
// end::term-vectors-execute
// tag::term-vectors-response
// <1>
String index = response.getIndex();
// <2>
String id = response.getId();
// <3>
boolean found = response.getFound();
if (response.getTermVectorsList() != null) {
// tag::term-vectors-term-vectors
for (TermVectorsResponse.TermVector tv : response.getTermVectorsList()) {
// <1>
String fieldname = tv.getFieldName();
// <2>
int docCount = tv.getFieldStatistics().getDocCount();
long sumTotalTermFreq = // <3>
tv.getFieldStatistics().getSumTotalTermFreq();
// <4>
long sumDocFreq = tv.getFieldStatistics().getSumDocFreq();
if (tv.getTerms() != null) {
List<TermVectorsResponse.TermVector.Term> terms = // <5>
tv.getTerms();
for (TermVectorsResponse.TermVector.Term term : terms) {
// <6>
String termStr = term.getTerm();
// <7>
int termFreq = term.getTermFreq();
// <8>
int docFreq = term.getDocFreq();
// <9>
long totalTermFreq = term.getTotalTermFreq();
// <10>
float score = term.getScore();
if (term.getTokens() != null) {
List<TermVectorsResponse.TermVector.Token> tokens = // <11>
term.getTokens();
for (TermVectorsResponse.TermVector.Token token : tokens) {
// <12>
int position = token.getPosition();
// <13>
int startOffset = token.getStartOffset();
// <14>
int endOffset = token.getEndOffset();
// <15>
String payload = token.getPayload();
}
}
}
}
}
// end::term-vectors-term-vectors
}
ActionListener<TermVectorsResponse> listener;
// tag::term-vectors-execute-listener
listener = new ActionListener<TermVectorsResponse>() {
@Override
public void onResponse(TermVectorsResponse termVectorsResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::term-vectors-execute-listener
CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::term-vectors-execute-async
// <1>
client.termvectorsAsync(request, RequestOptions.DEFAULT, listener);
// end::term-vectors-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Aggregations