Search in sources :

Example 6 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse in project elasticsearch by elastic.

the class DocumentActionsIT method testIndexActions.

public void testIndexActions() throws Exception {
    createIndex();
    NumShards numShards = getNumShards(getConcreteIndexName());
    logger.info("Running Cluster Health");
    ensureGreen();
    logger.info("Indexing [type1/1]");
    IndexResponse indexResponse = client().prepareIndex().setIndex("test").setType("type1").setId("1").setSource(source("1", "test")).setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    assertThat(indexResponse.getIndex(), equalTo(getConcreteIndexName()));
    assertThat(indexResponse.getId(), equalTo("1"));
    assertThat(indexResponse.getType(), equalTo("type1"));
    logger.info("Refreshing");
    RefreshResponse refreshResponse = refresh();
    assertThat(refreshResponse.getSuccessfulShards(), equalTo(numShards.totalNumShards));
    logger.info("--> index exists?");
    assertThat(indexExists(getConcreteIndexName()), equalTo(true));
    logger.info("--> index exists?, fake index");
    assertThat(indexExists("test1234565"), equalTo(false));
    logger.info("Clearing cache");
    ClearIndicesCacheResponse clearIndicesCacheResponse = client().admin().indices().clearCache(clearIndicesCacheRequest("test").recycler(true).fieldDataCache(true).queryCache(true)).actionGet();
    assertNoFailures(clearIndicesCacheResponse);
    assertThat(clearIndicesCacheResponse.getSuccessfulShards(), equalTo(numShards.totalNumShards));
    logger.info("Force Merging");
    waitForRelocation(ClusterHealthStatus.GREEN);
    ForceMergeResponse mergeResponse = forceMerge();
    assertThat(mergeResponse.getSuccessfulShards(), equalTo(numShards.totalNumShards));
    GetResponse getResult;
    logger.info("Get [type1/1]");
    for (int i = 0; i < 5; i++) {
        getResult = client().prepareGet("test", "type1", "1").setOperationThreaded(false).execute().actionGet();
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
        assertThat("cycle #" + i, getResult.getSourceAsString(), equalTo(source("1", "test").string()));
        assertThat("cycle(map) #" + i, (String) getResult.getSourceAsMap().get("name"), equalTo("test"));
        getResult = client().get(getRequest("test").type("type1").id("1").operationThreaded(true)).actionGet();
        assertThat("cycle #" + i, getResult.getSourceAsString(), equalTo(source("1", "test").string()));
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
    }
    logger.info("Get [type1/1] with script");
    for (int i = 0; i < 5; i++) {
        getResult = client().prepareGet("test", "type1", "1").setStoredFields("name").execute().actionGet();
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
        assertThat(getResult.isExists(), equalTo(true));
        assertThat(getResult.getSourceAsBytes(), nullValue());
        assertThat(getResult.getField("name").getValues().get(0).toString(), equalTo("test"));
    }
    logger.info("Get [type1/2] (should be empty)");
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("2")).actionGet();
        assertThat(getResult.isExists(), equalTo(false));
    }
    logger.info("Delete [type1/1]");
    DeleteResponse deleteResponse = client().prepareDelete("test", "type1", "1").execute().actionGet();
    assertThat(deleteResponse.getIndex(), equalTo(getConcreteIndexName()));
    assertThat(deleteResponse.getId(), equalTo("1"));
    assertThat(deleteResponse.getType(), equalTo("type1"));
    logger.info("Refreshing");
    client().admin().indices().refresh(refreshRequest("test")).actionGet();
    logger.info("Get [type1/1] (should be empty)");
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.isExists(), equalTo(false));
    }
    logger.info("Index [type1/1]");
    client().index(indexRequest("test").type("type1").id("1").source(source("1", "test"))).actionGet();
    logger.info("Index [type1/2]");
    client().index(indexRequest("test").type("type1").id("2").source(source("2", "test2"))).actionGet();
    logger.info("Flushing");
    FlushResponse flushResult = client().admin().indices().prepareFlush("test").execute().actionGet();
    assertThat(flushResult.getSuccessfulShards(), equalTo(numShards.totalNumShards));
    assertThat(flushResult.getFailedShards(), equalTo(0));
    logger.info("Refreshing");
    client().admin().indices().refresh(refreshRequest("test")).actionGet();
    logger.info("Get [type1/1] and [type1/2]");
    for (int i = 0; i < 5; i++) {
        getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
        assertThat("cycle #" + i, getResult.getSourceAsString(), equalTo(source("1", "test").string()));
        getResult = client().get(getRequest("test").type("type1").id("2")).actionGet();
        String ste1 = getResult.getSourceAsString();
        String ste2 = source("2", "test2").string();
        assertThat("cycle #" + i, ste1, equalTo(ste2));
        assertThat(getResult.getIndex(), equalTo(getConcreteIndexName()));
    }
    logger.info("Count");
    // check count
    for (int i = 0; i < 5; i++) {
        // test successful
        SearchResponse countResponse = client().prepareSearch("test").setSize(0).setQuery(termQuery("_type", "type1")).execute().actionGet();
        assertNoFailures(countResponse);
        assertThat(countResponse.getHits().getTotalHits(), equalTo(2L));
        assertThat(countResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
        assertThat(countResponse.getFailedShards(), equalTo(0));
        // count with no query is a match all one
        countResponse = client().prepareSearch("test").setSize(0).execute().actionGet();
        assertThat("Failures " + countResponse.getShardFailures(), countResponse.getShardFailures() == null ? 0 : countResponse.getShardFailures().length, equalTo(0));
        assertThat(countResponse.getHits().getTotalHits(), equalTo(2L));
        assertThat(countResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
        assertThat(countResponse.getFailedShards(), equalTo(0));
    }
}
Also used : ClearIndicesCacheResponse(org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse) ForceMergeResponse(org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse) FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) GetResponse(org.elasticsearch.action.get.GetResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 7 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse in project elasticsearch by elastic.

the class ShardInfoIT method testIndexAndDelete.

public void testIndexAndDelete() throws Exception {
    prepareIndex(1);
    IndexResponse indexResponse = client().prepareIndex("idx", "type").setSource("{}", XContentType.JSON).get();
    assertShardInfo(indexResponse);
    DeleteResponse deleteResponse = client().prepareDelete("idx", "type", indexResponse.getId()).get();
    assertShardInfo(deleteResponse);
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 8 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse in project elasticsearch by elastic.

the class GetActionIT method testSimpleGet.

public void testSimpleGet() {
    assertAcked(prepareCreate("test").addMapping("type1", "field1", "type=keyword,store=true", "field2", "type=keyword,store=true").setSettings(Settings.builder().put("index.refresh_interval", -1)).addAlias(new Alias("alias")));
    ensureGreen();
    GetResponse response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(false));
    logger.info("--> index doc 1");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get();
    logger.info("--> non realtime get 1");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setRealtime(false).get();
    assertThat(response.isExists(), equalTo(false));
    logger.info("--> realtime get 1");
    response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));
    logger.info("--> realtime get 1 (no source, implicit)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setStoredFields(Strings.EMPTY_ARRAY).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    Set<String> fields = new HashSet<>(response.getFields().keySet());
    assertThat(fields, equalTo(Collections.<String>emptySet()));
    assertThat(response.getSourceAsBytes(), nullValue());
    logger.info("--> realtime get 1 (no source, explicit)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setFetchSource(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    fields = new HashSet<>(response.getFields().keySet());
    assertThat(fields, equalTo(Collections.<String>emptySet()));
    assertThat(response.getSourceAsBytes(), nullValue());
    logger.info("--> realtime get 1 (no type)");
    response = client().prepareGet(indexOrAlias(), null, "1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));
    logger.info("--> realtime fetch of field");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setStoredFields("field1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsBytes(), nullValue());
    assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1"));
    assertThat(response.getField("field2"), nullValue());
    logger.info("--> realtime fetch of field & source");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setStoredFields("field1").setFetchSource("field1", null).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap(), hasKey("field1"));
    assertThat(response.getSourceAsMap(), not(hasKey("field2")));
    assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1"));
    assertThat(response.getField("field2"), nullValue());
    logger.info("--> flush the index, so we load it from it");
    flush();
    logger.info("--> realtime get 1 (loaded from index)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));
    logger.info("--> non realtime get 1 (loaded from index)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setRealtime(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2"));
    logger.info("--> realtime fetch of field (loaded from index)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setStoredFields("field1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsBytes(), nullValue());
    assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1"));
    assertThat(response.getField("field2"), nullValue());
    logger.info("--> realtime fetch of field & source (loaded from index)");
    response = client().prepareGet(indexOrAlias(), "type1", "1").setStoredFields("field1").setFetchSource(true).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsBytes(), not(nullValue()));
    assertThat(response.getField("field1").getValues().get(0).toString(), equalTo("value1"));
    assertThat(response.getField("field2"), nullValue());
    logger.info("--> update doc 1");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").get();
    logger.info("--> realtime get 1");
    response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1_1"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2_1"));
    logger.info("--> update doc 1 again");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1_2", "field2", "value2_2").get();
    response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getSourceAsMap().get("field1").toString(), equalTo("value1_2"));
    assertThat(response.getSourceAsMap().get("field2").toString(), equalTo("value2_2"));
    DeleteResponse deleteResponse = client().prepareDelete("test", "type1", "1").get();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    response = client().prepareGet(indexOrAlias(), "type1", "1").get();
    assertThat(response.isExists(), equalTo(false));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) HashSet(java.util.HashSet)

Example 9 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse in project elasticsearch by elastic.

the class WaitUntilRefreshIT method testDelete.

public void testDelete() throws InterruptedException, ExecutionException {
    // Index normally
    indexRandom(true, client().prepareIndex("test", "test", "1").setSource("foo", "bar"));
    assertSearchHits(client().prepareSearch("test").setQuery(matchQuery("foo", "bar")).get(), "1");
    // Now delete with blockUntilRefresh
    DeleteResponse delete = client().prepareDelete("test", "test", "1").setRefreshPolicy(RefreshPolicy.WAIT_UNTIL).get();
    assertEquals(DocWriteResponse.Result.DELETED, delete.getResult());
    assertFalse("request shouldn't have forced a refresh", delete.forcedRefresh());
    assertNoSearchHits(client().prepareSearch("test").setQuery(matchQuery("foo", "bar")).get());
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse)

Example 10 with DeleteResponse

use of org.elasticsearch.action.delete.DeleteResponse in project elasticsearch by elastic.

the class SimpleNestedIT method testSimpleNested.

public void testSimpleNested() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", "nested1", "type=nested").addMapping("type2", "nested1", "type=nested"));
    ensureGreen();
    // check on no data, see it works
    SearchResponse searchResponse = client().prepareSearch("test").execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
    searchResponse = client().prepareSearch("test").setQuery(termQuery("n_field1", "n_value1_1")).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("field1", "value1").startArray("nested1").startObject().field("n_field1", "n_value1_1").field("n_field2", "n_value2_1").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()).execute().actionGet();
    waitForRelocation(ClusterHealthStatus.GREEN);
    // flush, so we fetch it from the index (as see that we filter nested docs)
    flush();
    GetResponse getResponse = client().prepareGet("test", "type1", "1").get();
    assertThat(getResponse.isExists(), equalTo(true));
    assertThat(getResponse.getSourceAsBytes(), notNullValue());
    // check the numDocs
    assertDocumentCount("test", 3);
    searchResponse = client().prepareSearch("test").setQuery(termQuery("n_field1", "n_value1_1")).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
    // search for something that matches the nested doc, and see that we don't find the nested doc
    searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).get();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch("test").setQuery(termQuery("n_field1", "n_value1_1")).get();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
    // now, do a nested query
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).get();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    // add another doc, one that would match if it was not nested...
    client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("field1", "value1").startArray("nested1").startObject().field("n_field1", "n_value1_1").field("n_field2", "n_value2_2").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_1").endObject().endArray().endObject()).execute().actionGet();
    waitForRelocation(ClusterHealthStatus.GREEN);
    // flush, so we fetch it from the index (as see that we filter nested docs)
    flush();
    assertDocumentCount("test", 6);
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    // filter
    searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchAllQuery()).mustNot(nestedQuery("nested1", boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")), ScoreMode.Avg))).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    // check with type prefix
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    // check delete, so all is gone...
    DeleteResponse deleteResponse = client().prepareDelete("test", "type1", "2").execute().actionGet();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    // flush, so we fetch it from the index (as see that we filter nested docs)
    flush();
    assertDocumentCount("test", 3);
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch("test").setTypes("type1", "type2").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) GetResponse(org.elasticsearch.action.get.GetResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)42 Test (org.junit.Test)14 IndexResponse (org.elasticsearch.action.index.IndexResponse)13 GetResponse (org.elasticsearch.action.get.GetResponse)11 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 HashMap (java.util.HashMap)7 DeleteRequestBuilder (org.elasticsearch.action.delete.DeleteRequestBuilder)7 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 MockFlowFile (org.apache.nifi.util.MockFlowFile)5 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)3 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)3 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CamelContext (org.apache.camel.CamelContext)2 ProducerTemplate (org.apache.camel.ProducerTemplate)2