Search in sources :

Example 16 with GetResponse

use of org.elasticsearch.action.get.GetResponse 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 17 with GetResponse

use of org.elasticsearch.action.get.GetResponse 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 18 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GetActionIT method assertGetFieldNull.

protected void assertGetFieldNull(String index, String type, String docId, String field, @Nullable String routing) {
    //for get
    GetResponse response = getDocument(index, type, docId, field, routing);
    assertTrue(response.isExists());
    assertNull(response.getField(field));
    assertThat(response.getId(), equalTo(docId));
    //same for multi get
    response = multiGetDocument(index, type, docId, field, routing);
    assertNull(response.getField(field));
    assertThat(response.getId(), equalTo(docId));
    assertTrue(response.isExists());
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse)

Example 19 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GetActionIT method assertGetFieldWorks.

private void assertGetFieldWorks(String index, String type, String docId, String field, @Nullable String routing) {
    GetResponse response = getDocument(index, type, docId, field, routing);
    assertThat(response.getId(), equalTo(docId));
    assertTrue(response.isExists());
    assertNotNull(response.getField(field));
    response = multiGetDocument(index, type, docId, field, routing);
    assertThat(response.getId(), equalTo(docId));
    assertTrue(response.isExists());
    assertNotNull(response.getField(field));
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse)

Example 20 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class GetActionIT method testGetWithVersion.

public void testGetWithVersion() {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put("index.refresh_interval", -1)));
    ensureGreen();
    GetResponse response = client().prepareGet("test", "type1", "1").get();
    assertThat(response.isExists(), equalTo(false));
    logger.info("--> index doc 1");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get();
    // From translog:
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(Versions.MATCH_ANY).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getVersion(), equalTo(1L));
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getVersion(), equalTo(1L));
    try {
        client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).get();
        fail();
    } catch (VersionConflictEngineException e) {
    //all good
    }
    // From Lucene index:
    refresh();
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(Versions.MATCH_ANY).setRealtime(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(1L));
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).setRealtime(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(1L));
    try {
        client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).setRealtime(false).get();
        fail();
    } catch (VersionConflictEngineException e) {
    //all good
    }
    logger.info("--> index doc 1 again, so increasing the version");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get();
    // From translog:
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(Versions.MATCH_ANY).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(2L));
    try {
        client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).get();
        fail();
    } catch (VersionConflictEngineException e) {
    //all good
    }
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(2L));
    // From Lucene index:
    refresh();
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(Versions.MATCH_ANY).setRealtime(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(2L));
    try {
        client().prepareGet(indexOrAlias(), "type1", "1").setVersion(1).setRealtime(false).get();
        fail();
    } catch (VersionConflictEngineException e) {
    //all good
    }
    response = client().prepareGet(indexOrAlias(), "type1", "1").setVersion(2).setRealtime(false).get();
    assertThat(response.isExists(), equalTo(true));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getVersion(), equalTo(2L));
}
Also used : VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) Alias(org.elasticsearch.action.admin.indices.alias.Alias) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse)

Aggregations

GetResponse (org.elasticsearch.action.get.GetResponse)84 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)22 Test (org.junit.Test)18 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 HashMap (java.util.HashMap)11 GetRequest (org.elasticsearch.action.get.GetRequest)11 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)10 Script (org.elasticsearch.script.Script)9 Settings (org.elasticsearch.common.settings.Settings)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IOException (java.io.IOException)7 Map (java.util.Map)7 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 CompiledScript (org.elasticsearch.script.CompiledScript)7 ExecutableScript (org.elasticsearch.script.ExecutableScript)7 SearchScript (org.elasticsearch.script.SearchScript)7 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)6 IndexResponse (org.elasticsearch.action.index.IndexResponse)5 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)5 Path (java.nio.file.Path)4