Search in sources :

Example 1 with MultiTermVectorsResponse

use of org.opensearch.client.core.MultiTermVectorsResponse 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));
}
Also used : TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) BulkRequest(org.opensearch.action.bulk.BulkRequest) Request(org.opensearch.client.Request) WriteRequest(org.opensearch.action.support.WriteRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) RethrottleRequest(org.opensearch.client.RethrottleRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) GetRequest(org.opensearch.action.get.GetRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CountDownLatch(java.util.concurrent.CountDownLatch) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) OpenSearchException(org.opensearch.OpenSearchException) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) IndexResponse(org.opensearch.action.index.IndexResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) GetResponse(org.opensearch.action.get.GetResponse) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) Response(org.opensearch.client.Response) GetSourceResponse(org.opensearch.client.core.GetSourceResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 OpenSearchException (org.opensearch.OpenSearchException)1 DocWriteRequest (org.opensearch.action.DocWriteRequest)1 DocWriteResponse (org.opensearch.action.DocWriteResponse)1 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)1 BulkItemResponse (org.opensearch.action.bulk.BulkItemResponse)1 BulkRequest (org.opensearch.action.bulk.BulkRequest)1 BulkResponse (org.opensearch.action.bulk.BulkResponse)1 DeleteRequest (org.opensearch.action.delete.DeleteRequest)1 DeleteResponse (org.opensearch.action.delete.DeleteResponse)1 GetRequest (org.opensearch.action.get.GetRequest)1 GetResponse (org.opensearch.action.get.GetResponse)1 MultiGetItemResponse (org.opensearch.action.get.MultiGetItemResponse)1 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)1 MultiGetResponse (org.opensearch.action.get.MultiGetResponse)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 IndexResponse (org.opensearch.action.index.IndexResponse)1 WriteRequest (org.opensearch.action.support.WriteRequest)1 ReplicationResponse (org.opensearch.action.support.replication.ReplicationResponse)1