Search in sources :

Example 6 with GetResponse

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

the class IndexStatsIT method testSimpleStats.

public void testSimpleStats() throws Exception {
    createIndex("test1", "test2");
    ensureGreen();
    client().prepareIndex("test1", "type1", Integer.toString(1)).setSource("field", "value").execute().actionGet();
    client().prepareIndex("test1", "type2", Integer.toString(1)).setSource("field", "value").execute().actionGet();
    client().prepareIndex("test2", "type", Integer.toString(1)).setSource("field", "value").execute().actionGet();
    refresh();
    NumShards test1 = getNumShards("test1");
    long test1ExpectedWrites = 2 * test1.dataCopies;
    NumShards test2 = getNumShards("test2");
    long test2ExpectedWrites = test2.dataCopies;
    long totalExpectedWrites = test1ExpectedWrites + test2ExpectedWrites;
    IndicesStatsResponse stats = client().admin().indices().prepareStats().execute().actionGet();
    assertThat(stats.getPrimaries().getDocs().getCount(), equalTo(3L));
    assertThat(stats.getTotal().getDocs().getCount(), equalTo(totalExpectedWrites));
    assertThat(stats.getPrimaries().getIndexing().getTotal().getIndexCount(), equalTo(3L));
    assertThat(stats.getPrimaries().getIndexing().getTotal().getIndexFailedCount(), equalTo(0L));
    assertThat(stats.getPrimaries().getIndexing().getTotal().isThrottled(), equalTo(false));
    assertThat(stats.getPrimaries().getIndexing().getTotal().getThrottleTime().millis(), equalTo(0L));
    assertThat(stats.getTotal().getIndexing().getTotal().getIndexCount(), equalTo(totalExpectedWrites));
    assertThat(stats.getTotal().getStore(), notNullValue());
    assertThat(stats.getTotal().getMerge(), notNullValue());
    assertThat(stats.getTotal().getFlush(), notNullValue());
    assertThat(stats.getTotal().getRefresh(), notNullValue());
    assertThat(stats.getIndex("test1").getPrimaries().getDocs().getCount(), equalTo(2L));
    assertThat(stats.getIndex("test1").getTotal().getDocs().getCount(), equalTo(test1ExpectedWrites));
    assertThat(stats.getIndex("test1").getPrimaries().getStore(), notNullValue());
    assertThat(stats.getIndex("test1").getPrimaries().getMerge(), notNullValue());
    assertThat(stats.getIndex("test1").getPrimaries().getFlush(), notNullValue());
    assertThat(stats.getIndex("test1").getPrimaries().getRefresh(), notNullValue());
    assertThat(stats.getIndex("test2").getPrimaries().getDocs().getCount(), equalTo(1L));
    assertThat(stats.getIndex("test2").getTotal().getDocs().getCount(), equalTo(test2ExpectedWrites));
    // make sure that number of requests in progress is 0
    assertThat(stats.getIndex("test1").getTotal().getIndexing().getTotal().getIndexCurrent(), equalTo(0L));
    assertThat(stats.getIndex("test1").getTotal().getIndexing().getTotal().getDeleteCurrent(), equalTo(0L));
    assertThat(stats.getIndex("test1").getTotal().getSearch().getTotal().getFetchCurrent(), equalTo(0L));
    assertThat(stats.getIndex("test1").getTotal().getSearch().getTotal().getQueryCurrent(), equalTo(0L));
    // check flags
    stats = client().admin().indices().prepareStats().clear().setFlush(true).setRefresh(true).setMerge(true).execute().actionGet();
    assertThat(stats.getTotal().getDocs(), nullValue());
    assertThat(stats.getTotal().getStore(), nullValue());
    assertThat(stats.getTotal().getIndexing(), nullValue());
    assertThat(stats.getTotal().getMerge(), notNullValue());
    assertThat(stats.getTotal().getFlush(), notNullValue());
    assertThat(stats.getTotal().getRefresh(), notNullValue());
    // check types
    stats = client().admin().indices().prepareStats().setTypes("type1", "type").execute().actionGet();
    assertThat(stats.getPrimaries().getIndexing().getTypeStats().get("type1").getIndexCount(), equalTo(1L));
    assertThat(stats.getPrimaries().getIndexing().getTypeStats().get("type").getIndexCount(), equalTo(1L));
    assertThat(stats.getPrimaries().getIndexing().getTypeStats().get("type1").getIndexFailedCount(), equalTo(0L));
    assertThat(stats.getPrimaries().getIndexing().getTypeStats().get("type2"), nullValue());
    assertThat(stats.getPrimaries().getIndexing().getTypeStats().get("type1").getIndexCurrent(), equalTo(0L));
    assertThat(stats.getPrimaries().getIndexing().getTypeStats().get("type1").getDeleteCurrent(), equalTo(0L));
    assertThat(stats.getTotal().getGet().getCount(), equalTo(0L));
    // check get
    GetResponse getResponse = client().prepareGet("test1", "type1", "1").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    stats = client().admin().indices().prepareStats().execute().actionGet();
    assertThat(stats.getTotal().getGet().getCount(), equalTo(1L));
    assertThat(stats.getTotal().getGet().getExistsCount(), equalTo(1L));
    assertThat(stats.getTotal().getGet().getMissingCount(), equalTo(0L));
    // missing get
    getResponse = client().prepareGet("test1", "type1", "2").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(false));
    stats = client().admin().indices().prepareStats().execute().actionGet();
    assertThat(stats.getTotal().getGet().getCount(), equalTo(2L));
    assertThat(stats.getTotal().getGet().getExistsCount(), equalTo(1L));
    assertThat(stats.getTotal().getGet().getMissingCount(), equalTo(1L));
    // clear all
    stats = client().admin().indices().prepareStats().setDocs(false).setStore(false).setIndexing(false).setFlush(true).setRefresh(true).setMerge(true).clear().execute().actionGet();
    assertThat(stats.getTotal().getDocs(), nullValue());
    assertThat(stats.getTotal().getStore(), nullValue());
    assertThat(stats.getTotal().getIndexing(), nullValue());
    assertThat(stats.getTotal().getGet(), nullValue());
    assertThat(stats.getTotal().getSearch(), nullValue());
    // index failed
    try {
        client().prepareIndex("test1", "type1", Integer.toString(1)).setSource("field", "value").setVersion(1).setVersionType(VersionType.EXTERNAL).execute().actionGet();
        fail("Expected a version conflict");
    } catch (VersionConflictEngineException e) {
    }
    try {
        client().prepareIndex("test1", "type2", Integer.toString(1)).setSource("field", "value").setVersion(1).setVersionType(VersionType.EXTERNAL).execute().actionGet();
        fail("Expected a version conflict");
    } catch (VersionConflictEngineException e) {
    }
    try {
        client().prepareIndex("test2", "type", Integer.toString(1)).setSource("field", "value").setVersion(1).setVersionType(VersionType.EXTERNAL).execute().actionGet();
        fail("Expected a version conflict");
    } catch (VersionConflictEngineException e) {
    }
    stats = client().admin().indices().prepareStats().setTypes("type1", "type2").execute().actionGet();
    assertThat(stats.getIndex("test1").getPrimaries().getIndexing().getTotal().getIndexFailedCount(), equalTo(2L));
    assertThat(stats.getIndex("test2").getPrimaries().getIndexing().getTotal().getIndexFailedCount(), equalTo(1L));
    assertThat(stats.getPrimaries().getIndexing().getTypeStats().get("type1").getIndexFailedCount(), equalTo(1L));
    assertThat(stats.getPrimaries().getIndexing().getTypeStats().get("type2").getIndexFailedCount(), equalTo(1L));
    assertThat(stats.getPrimaries().getIndexing().getTotal().getIndexFailedCount(), equalTo(3L));
}
Also used : IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 7 with GetResponse

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

the class RestGetAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final GetRequest getRequest = new GetRequest(request.param("index"), request.param("type"), request.param("id"));
    getRequest.operationThreaded(true);
    getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
    getRequest.routing(request.param("routing"));
    getRequest.parent(request.param("parent"));
    getRequest.preference(request.param("preference"));
    getRequest.realtime(request.paramAsBoolean("realtime", getRequest.realtime()));
    if (request.param("fields") != null) {
        throw new IllegalArgumentException("the parameter [fields] is no longer supported, " + "please use [stored_fields] to retrieve stored fields or [_source] to load the field from _source");
    }
    final String fieldsParam = request.param("stored_fields");
    if (fieldsParam != null) {
        final String[] fields = Strings.splitStringByCommaToArray(fieldsParam);
        if (fields != null) {
            getRequest.storedFields(fields);
        }
    }
    getRequest.version(RestActions.parseVersion(request));
    getRequest.versionType(VersionType.fromString(request.param("version_type"), getRequest.versionType()));
    getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
    return channel -> client.get(getRequest, new RestToXContentListener<GetResponse>(channel) {

        @Override
        protected RestStatus getStatus(final GetResponse response) {
            return response.isExists() ? OK : NOT_FOUND;
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GetResponse(org.elasticsearch.action.get.GetResponse) GetRequest(org.elasticsearch.action.get.GetRequest) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestToXContentListener(org.elasticsearch.rest.action.RestToXContentListener) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) RestController(org.elasticsearch.rest.RestController) VersionType(org.elasticsearch.index.VersionType) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) HEAD(org.elasticsearch.rest.RestRequest.Method.HEAD) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) RestStatus(org.elasticsearch.rest.RestStatus) GetRequest(org.elasticsearch.action.get.GetRequest) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 8 with GetResponse

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

the class NoMasterNodeIT method testNoMasterActionsWriteMasterBlock.

public void testNoMasterActionsWriteMasterBlock() throws Exception {
    Settings settings = Settings.builder().put("discovery.type", "zen").put("action.auto_create_index", false).put("discovery.zen.minimum_master_nodes", 2).put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "200ms").put("discovery.initial_state_timeout", "500ms").put(DiscoverySettings.NO_MASTER_BLOCK_SETTING.getKey(), "write").build();
    internalCluster().startNode(settings);
    // start a second node, create an index, and then shut it down so we have no master block
    internalCluster().startNode(settings);
    prepareCreate("test1").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).get();
    prepareCreate("test2").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 2, IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).get();
    client().admin().cluster().prepareHealth("_all").setWaitForGreenStatus().get();
    client().prepareIndex("test1", "type1", "1").setSource("field", "value1").get();
    client().prepareIndex("test2", "type1", "1").setSource("field", "value1").get();
    refresh();
    ensureSearchable("test1", "test2");
    ClusterStateResponse clusterState = client().admin().cluster().prepareState().get();
    logger.info("Cluster state:\n{}", clusterState.getState());
    internalCluster().stopRandomDataNode();
    assertTrue(awaitBusy(() -> {
        ClusterState state = client().admin().cluster().prepareState().setLocal(true).get().getState();
        return state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID);
    }));
    GetResponse getResponse = client().prepareGet("test1", "type1", "1").get();
    assertExists(getResponse);
    SearchResponse countResponse = client().prepareSearch("test1").setSize(0).get();
    assertHitCount(countResponse, 1L);
    SearchResponse searchResponse = client().prepareSearch("test1").get();
    assertHitCount(searchResponse, 1L);
    countResponse = client().prepareSearch("test2").setSize(0).get();
    assertThat(countResponse.getTotalShards(), equalTo(2));
    assertThat(countResponse.getSuccessfulShards(), equalTo(1));
    TimeValue timeout = TimeValue.timeValueMillis(200);
    long now = System.currentTimeMillis();
    try {
        client().prepareUpdate("test1", "type1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").setTimeout(timeout).get();
        fail("Expected ClusterBlockException");
    } catch (ClusterBlockException e) {
        assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
        assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
    }
    now = System.currentTimeMillis();
    try {
        client().prepareIndex("test1", "type1", "1").setSource(XContentFactory.jsonBuilder().startObject().endObject()).setTimeout(timeout).get();
        fail("Expected ClusterBlockException");
    } catch (ClusterBlockException e) {
        assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
        assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
    }
    internalCluster().startNode(settings);
    client().admin().cluster().prepareHealth().setWaitForGreenStatus().setWaitForNodes("2").get();
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) GetResponse(org.elasticsearch.action.get.GetResponse) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) Settings(org.elasticsearch.common.settings.Settings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 9 with GetResponse

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

the class OldIndexBackwardsCompatibilityIT method assertRealtimeGetWorks.

void assertRealtimeGetWorks(String indexName) {
    assertAcked(client().admin().indices().prepareUpdateSettings(indexName).setSettings(Settings.builder().put("refresh_interval", -1).build()));
    SearchRequestBuilder searchReq = client().prepareSearch(indexName).setQuery(QueryBuilders.matchAllQuery());
    SearchHit hit = searchReq.get().getHits().getAt(0);
    String docId = hit.getId();
    // foo is new, it is not a field in the generated index
    client().prepareUpdate(indexName, "doc", docId).setDoc(Requests.INDEX_CONTENT_TYPE, "foo", "bar").get();
    GetResponse getRsp = client().prepareGet(indexName, "doc", docId).get();
    Map<String, Object> source = getRsp.getSourceAsMap();
    assertThat(source, Matchers.hasKey("foo"));
    assertAcked(client().admin().indices().prepareUpdateSettings(indexName).setSettings(Settings.builder().put("refresh_interval", IndexSettings.DEFAULT_REFRESH_INTERVAL).build()));
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 10 with GetResponse

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

the class UpdateIT method testContextVariables.

public void testContextVariables() throws Exception {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").endObject().endObject()).addMapping("subtype1", XContentFactory.jsonBuilder().startObject().startObject("subtype1").startObject("_parent").field("type", "type1").endObject().endObject().endObject()));
    ensureGreen();
    // Index some documents
    client().prepareIndex().setIndex("test").setType("type1").setId("parentId1").setSource("field1", 0, "content", "bar").execute().actionGet();
    client().prepareIndex().setIndex("test").setType("subtype1").setId("id1").setParent("parentId1").setRouting("routing1").setSource("field1", 1, "content", "foo").execute().actionGet();
    // Update the first object and note context variables values
    UpdateResponse updateResponse = client().prepareUpdate("test", "subtype1", "id1").setRouting("routing1").setScript(new Script(ScriptType.INLINE, "extract_ctx", "", Collections.emptyMap())).execute().actionGet();
    assertEquals(2, updateResponse.getVersion());
    GetResponse getResponse = client().prepareGet("test", "subtype1", "id1").setRouting("routing1").execute().actionGet();
    Map<String, Object> updateContext = (Map<String, Object>) getResponse.getSourceAsMap().get("update_context");
    assertEquals("test", updateContext.get("_index"));
    assertEquals("subtype1", updateContext.get("_type"));
    assertEquals("id1", updateContext.get("_id"));
    assertEquals(1, updateContext.get("_version"));
    assertEquals("parentId1", updateContext.get("_parent"));
    assertEquals("routing1", updateContext.get("_routing"));
    // Idem with the second object
    updateResponse = client().prepareUpdate("test", "type1", "parentId1").setScript(new Script(ScriptType.INLINE, "extract_ctx", "", Collections.emptyMap())).execute().actionGet();
    assertEquals(2, updateResponse.getVersion());
    getResponse = client().prepareGet("test", "type1", "parentId1").execute().actionGet();
    updateContext = (Map<String, Object>) getResponse.getSourceAsMap().get("update_context");
    assertEquals("test", updateContext.get("_index"));
    assertEquals("type1", updateContext.get("_type"));
    assertEquals("parentId1", updateContext.get("_id"));
    assertEquals(1, updateContext.get("_version"));
    assertNull(updateContext.get("_parent"));
    assertNull(updateContext.get("_routing"));
    assertNull(updateContext.get("_ttl"));
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Matchers.containsString(org.hamcrest.Matchers.containsString) GetResponse(org.elasticsearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap)

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