Search in sources :

Example 11 with BulkRequest

use of org.opensearch.action.bulk.BulkRequest in project OpenSearch by opensearch-project.

the class CrudIT method testMultiGetWithIds.

public void testMultiGetWithIds() throws IOException {
    BulkRequest bulk = new BulkRequest();
    bulk.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    bulk.add(new IndexRequest("index").id("id1").source("{\"field\":\"value1\"}", XContentType.JSON));
    bulk.add(new IndexRequest("index").id("id2").source("{\"field\":\"value2\"}", XContentType.JSON));
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    multiGetRequest.add("index", "id1");
    multiGetRequest.add("index", "id2");
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Example 12 with BulkRequest

use of org.opensearch.action.bulk.BulkRequest in project OpenSearch by opensearch-project.

the class CrudIT method testMultiGet.

public void testMultiGet() throws IOException {
    {
        MultiGetRequest multiGetRequest = new MultiGetRequest();
        multiGetRequest.add("index", "id1");
        multiGetRequest.add("index", "id2");
        MultiGetResponse response = execute(multiGetRequest, highLevelClient()::mget, highLevelClient()::mgetAsync);
        assertEquals(2, response.getResponses().length);
        assertTrue(response.getResponses()[0].isFailed());
        assertNull(response.getResponses()[0].getResponse());
        assertEquals("id1", response.getResponses()[0].getFailure().getId());
        assertEquals("index", response.getResponses()[0].getFailure().getIndex());
        assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]", response.getResponses()[0].getFailure().getFailure().getMessage());
        assertTrue(response.getResponses()[1].isFailed());
        assertNull(response.getResponses()[1].getResponse());
        assertEquals("id2", response.getResponses()[1].getId());
        assertEquals("index", response.getResponses()[1].getIndex());
        assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]", response.getResponses()[1].getFailure().getFailure().getMessage());
    }
    BulkRequest bulk = new BulkRequest();
    bulk.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    IndexRequest index = new IndexRequest("index").id("id1");
    index.source("{\"field\":\"value1\"}", XContentType.JSON);
    bulk.add(index);
    index = new IndexRequest("index").id("id2");
    index.source("{\"field\":\"value2\"}", XContentType.JSON);
    bulk.add(index);
    highLevelClient().bulk(bulk, RequestOptions.DEFAULT);
    {
        MultiGetRequest multiGetRequest = new MultiGetRequest();
        multiGetRequest.add("index", "id1");
        multiGetRequest.add("index", "id2");
        MultiGetResponse response = execute(multiGetRequest, highLevelClient()::mget, highLevelClient()::mgetAsync);
        assertEquals(2, response.getResponses().length);
        assertFalse(response.getResponses()[0].isFailed());
        assertNull(response.getResponses()[0].getFailure());
        assertEquals("id1", response.getResponses()[0].getId());
        assertEquals("index", response.getResponses()[0].getIndex());
        assertEquals(Collections.singletonMap("field", "value1"), response.getResponses()[0].getResponse().getSource());
        assertFalse(response.getResponses()[1].isFailed());
        assertNull(response.getResponses()[1].getFailure());
        assertEquals("id2", response.getResponses()[1].getId());
        assertEquals("index", response.getResponses()[1].getIndex());
        assertEquals(Collections.singletonMap("field", "value2"), response.getResponses()[1].getResponse().getSource());
    }
}
Also used : MultiGetResponse(org.opensearch.action.get.MultiGetResponse) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Example 13 with BulkRequest

use of org.opensearch.action.bulk.BulkRequest in project OpenSearch by opensearch-project.

the class UpdateByQueryIT method testUpdateByQueryConflict.

public void testUpdateByQueryConflict() throws IOException {
    final String index = "testupdatebyqueryconflict";
    final Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
    createIndex(index, settings);
    final BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest(index).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).add(new IndexRequest(index).id("2").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    assertThat(highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status(), equalTo(RestStatus.OK));
    putConflictPipeline();
    final UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest();
    updateByQueryRequest.indices(index);
    updateByQueryRequest.setRefresh(true);
    updateByQueryRequest.setPipeline(CONFLICT_PIPELINE_ID);
    final BulkByScrollResponse response = highLevelClient().updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT);
    assertThat(response.getVersionConflicts(), equalTo(1L));
    assertThat(response.getSearchFailures(), empty());
    assertThat(response.getBulkFailures(), hasSize(1));
    assertThat(response.getBulkFailures().stream().map(BulkItemResponse.Failure::getMessage).collect(Collectors.toSet()), everyItem(containsString("version conflict")));
    assertThat(response.getTotal(), equalTo(2L));
    assertThat(response.getCreated(), equalTo(0L));
    assertThat(response.getUpdated(), equalTo(1L));
    assertThat(response.getDeleted(), equalTo(0L));
    assertThat(response.getNoops(), equalTo(0L));
    assertThat(response.getBatches(), equalTo(1));
    assertTrue(response.getTook().getMillis() > 0);
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexRequest(org.opensearch.action.index.IndexRequest) Settings(org.opensearch.common.settings.Settings) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse)

Example 14 with BulkRequest

use of org.opensearch.action.bulk.BulkRequest in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method indexSearchTestData.

private void indexSearchTestData() throws IOException {
    CreateIndexRequest authorsRequest = new CreateIndexRequest("authors").mapping(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("id").field("type", "keyword").endObject().startObject("user").field("type", "keyword").field("doc_values", "false").endObject().endObject().endObject());
    CreateIndexResponse authorsResponse = highLevelClient().indices().create(authorsRequest, RequestOptions.DEFAULT);
    assertTrue(authorsResponse.isAcknowledged());
    CreateIndexRequest reviewersRequest = new CreateIndexRequest("contributors").mapping(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("id").field("type", "keyword").endObject().startObject("user").field("type", "keyword").field("store", "true").endObject().endObject().endObject());
    CreateIndexResponse reviewersResponse = highLevelClient().indices().create(reviewersRequest, RequestOptions.DEFAULT);
    assertTrue(reviewersResponse.isAcknowledged());
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(new IndexRequest("posts").id("1").source(XContentType.JSON, "id", 1, "title", "In which order are my OpenSearch queries executed?", "user", Arrays.asList("foobar", "quxx"), "innerObject", Collections.singletonMap("key", "value")));
    bulkRequest.add(new IndexRequest("posts").id("2").source(XContentType.JSON, "id", 2, "title", "Current status and upcoming changes in OpenSearch", "user", Arrays.asList("foobar", "grault"), "innerObject", Collections.singletonMap("key", "value")));
    bulkRequest.add(new IndexRequest("posts").id("3").source(XContentType.JSON, "id", 3, "title", "The Future of Federated Search in OpenSearch", "user", Arrays.asList("foobar", "quuz"), "innerObject", Collections.singletonMap("key", "value")));
    bulkRequest.add(new IndexRequest("authors").id("1").source(XContentType.JSON, "id", 1, "user", "foobar"));
    bulkRequest.add(new IndexRequest("contributors").id("1").source(XContentType.JSON, "id", 1, "user", "quuz"));
    bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    BulkResponse bulkResponse = highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT);
    assertSame(RestStatus.OK, bulkResponse.status());
    assertFalse(bulkResponse.hasFailures());
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) BulkResponse(org.opensearch.action.bulk.BulkResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest)

Example 15 with BulkRequest

use of org.opensearch.action.bulk.BulkRequest in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testSearchRequestSuggestions.

@SuppressWarnings({ "unused", "rawtypes" })
public void testSearchRequestSuggestions() throws IOException {
    RestHighLevelClient client = highLevelClient();
    {
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest("posts").id("1").source(XContentType.JSON, "user", "foobar"));
        request.add(new IndexRequest("posts").id("2").source(XContentType.JSON, "user", "quxx"));
        request.add(new IndexRequest("posts").id("3").source(XContentType.JSON, "user", "quzz"));
        request.add(new IndexRequest("posts").id("4").source(XContentType.JSON, "user", "corge"));
        request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
        assertSame(RestStatus.OK, bulkResponse.status());
        assertFalse(bulkResponse.hasFailures());
    }
    {
        SearchRequest searchRequest = new SearchRequest();
        // tag::search-request-suggestion
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SuggestionBuilder termSuggestionBuilder = // <1>
        SuggestBuilders.termSuggestion("user").text("fooarb");
        SuggestBuilder suggestBuilder = new SuggestBuilder();
        // <2>
        suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
        searchSourceBuilder.suggest(suggestBuilder);
        // end::search-request-suggestion
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        {
            // tag::search-request-suggestion-get
            // <1>
            Suggest suggest = searchResponse.getSuggest();
            // <2>
            TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
            for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
                // <3>
                for (TermSuggestion.Entry.Option option : entry) {
                    // <4>
                    String suggestText = option.getText().string();
                }
            }
            // end::search-request-suggestion-get
            assertEquals(1, termSuggestion.getEntries().size());
            assertEquals(1, termSuggestion.getEntries().get(0).getOptions().size());
            assertEquals("foobar", termSuggestion.getEntries().get(0).getOptions().get(0).getText().string());
        }
    }
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) BulkResponse(org.opensearch.action.bulk.BulkResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) Suggest(org.opensearch.search.suggest.Suggest) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) SuggestBuilder(org.opensearch.search.suggest.SuggestBuilder) BulkRequest(org.opensearch.action.bulk.BulkRequest) TermSuggestion(org.opensearch.search.suggest.term.TermSuggestion) SuggestionBuilder(org.opensearch.search.suggest.SuggestionBuilder)

Aggregations

BulkRequest (org.opensearch.action.bulk.BulkRequest)68 IndexRequest (org.opensearch.action.index.IndexRequest)59 BulkResponse (org.opensearch.action.bulk.BulkResponse)35 Settings (org.opensearch.common.settings.Settings)20 ThreadPool (org.opensearch.threadpool.ThreadPool)16 SearchRequest (org.opensearch.action.search.SearchRequest)15 BulkShardRequest (org.opensearch.action.bulk.BulkShardRequest)14 ShardId (org.opensearch.index.shard.ShardId)14 HashMap (java.util.HashMap)12 UpdateRequest (org.opensearch.action.update.UpdateRequest)12 Releasable (org.opensearch.common.lease.Releasable)12 List (java.util.List)11 DocWriteRequest (org.opensearch.action.DocWriteRequest)10 DeleteRequest (org.opensearch.action.delete.DeleteRequest)10 XContentType (org.opensearch.common.xcontent.XContentType)10 Map (java.util.Map)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 SearchHit (org.opensearch.search.SearchHit)9 Arrays (java.util.Arrays)8 Collections (java.util.Collections)8