Search in sources :

Example 1 with TermVectorsResponse

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));
}
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)

Example 2 with TermVectorsResponse

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));
}
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) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) 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) CountDownLatch(java.util.concurrent.CountDownLatch) 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) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

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