Search in sources :

Example 26 with IndicesStatsResponse

use of org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse in project elasticsearch by elastic.

the class CompletionSuggestSearchIT method testThatStatsAreWorking.

public void testThatStatsAreWorking() throws Exception {
    String otherField = "testOtherField";
    client().admin().indices().prepareCreate(INDEX).setSettings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", 2)).execute().actionGet();
    ensureGreen();
    PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping(INDEX).setType(TYPE).setSource(jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject(FIELD).field("type", "completion").field("analyzer", "simple").endObject().startObject(otherField).field("type", "completion").field("analyzer", "simple").endObject().endObject().endObject().endObject()).get();
    assertThat(putMappingResponse.isAcknowledged(), is(true));
    // Index two entities
    client().prepareIndex(INDEX, TYPE, "1").setSource(jsonBuilder().startObject().field(FIELD, "Foo Fighters").field(otherField, "WHATEVER").endObject()).get();
    client().prepareIndex(INDEX, TYPE, "2").setSource(jsonBuilder().startObject().field(FIELD, "Bar Fighters").field(otherField, "WHATEVER2").endObject()).get();
    refresh();
    ensureGreen();
    // load the fst index into ram
    client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("f"))).get();
    client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(otherField).prefix("f"))).get();
    // Get all stats
    IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats(INDEX).setIndices(INDEX).setCompletion(true).get();
    CompletionStats completionStats = indicesStatsResponse.getIndex(INDEX).getPrimaries().completion;
    assertThat(completionStats, notNullValue());
    long totalSizeInBytes = completionStats.getSizeInBytes();
    assertThat(totalSizeInBytes, is(greaterThan(0L)));
    IndicesStatsResponse singleFieldStats = client().admin().indices().prepareStats(INDEX).setIndices(INDEX).setCompletion(true).setCompletionFields(FIELD).get();
    long singleFieldSizeInBytes = singleFieldStats.getIndex(INDEX).getPrimaries().completion.getFields().get(FIELD);
    IndicesStatsResponse otherFieldStats = client().admin().indices().prepareStats(INDEX).setIndices(INDEX).setCompletion(true).setCompletionFields(otherField).get();
    long otherFieldSizeInBytes = otherFieldStats.getIndex(INDEX).getPrimaries().completion.getFields().get(otherField);
    assertThat(singleFieldSizeInBytes + otherFieldSizeInBytes, is(totalSizeInBytes));
    // regexes
    IndicesStatsResponse regexFieldStats = client().admin().indices().prepareStats(INDEX).setIndices(INDEX).setCompletion(true).setCompletionFields("*").get();
    FieldMemoryStats fields = regexFieldStats.getIndex(INDEX).getPrimaries().completion.getFields();
    long regexSizeInBytes = fields.get(FIELD) + fields.get(otherField);
    assertThat(regexSizeInBytes, is(totalSizeInBytes));
}
Also used : IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) FieldMemoryStats(org.elasticsearch.common.FieldMemoryStats) Matchers.containsString(org.hamcrest.Matchers.containsString) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) CompletionStats(org.elasticsearch.search.suggest.completion.CompletionStats)

Example 27 with IndicesStatsResponse

use of org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse in project elasticsearch by elastic.

the class RestIndicesStatsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
    indicesStatsRequest.indicesOptions(IndicesOptions.fromRequest(request, indicesStatsRequest.indicesOptions()));
    indicesStatsRequest.indices(Strings.splitStringByCommaToArray(request.param("index")));
    indicesStatsRequest.types(Strings.splitStringByCommaToArray(request.param("types")));
    Set<String> metrics = Strings.splitStringByCommaToSet(request.param("metric", "_all"));
    // short cut, if no metrics have been specified in URI
    if (metrics.size() == 1 && metrics.contains("_all")) {
        indicesStatsRequest.all();
    } else if (metrics.contains("_all")) {
        throw new IllegalArgumentException(String.format(Locale.ROOT, "request [%s] contains _all and individual metrics [%s]", request.path(), request.param("metric")));
    } else {
        indicesStatsRequest.clear();
        // use a sorted set so the unrecognized parameters appear in a reliable sorted order
        final Set<String> invalidMetrics = new TreeSet<>();
        for (final String metric : metrics) {
            final Consumer<IndicesStatsRequest> consumer = METRICS.get(metric);
            if (consumer != null) {
                consumer.accept(indicesStatsRequest);
            } else {
                invalidMetrics.add(metric);
            }
        }
        if (!invalidMetrics.isEmpty()) {
            throw new IllegalArgumentException(unrecognized(request, invalidMetrics, METRICS.keySet(), "metric"));
        }
    }
    if (request.hasParam("groups")) {
        indicesStatsRequest.groups(Strings.splitStringByCommaToArray(request.param("groups")));
    }
    if (request.hasParam("types")) {
        indicesStatsRequest.types(Strings.splitStringByCommaToArray(request.param("types")));
    }
    if (indicesStatsRequest.completion() && (request.hasParam("fields") || request.hasParam("completion_fields"))) {
        indicesStatsRequest.completionFields(request.paramAsStringArray("completion_fields", request.paramAsStringArray("fields", Strings.EMPTY_ARRAY)));
    }
    if (indicesStatsRequest.fieldData() && (request.hasParam("fields") || request.hasParam("fielddata_fields"))) {
        indicesStatsRequest.fieldDataFields(request.paramAsStringArray("fielddata_fields", request.paramAsStringArray("fields", Strings.EMPTY_ARRAY)));
    }
    if (indicesStatsRequest.segments()) {
        indicesStatsRequest.includeSegmentFileSizes(request.paramAsBoolean("include_segment_file_sizes", false));
    }
    return channel -> client.admin().indices().stats(indicesStatsRequest, new RestBuilderListener<IndicesStatsResponse>(channel) {

        @Override
        public RestResponse buildResponse(IndicesStatsResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            buildBroadcastShardsHeader(builder, request, response);
            response.toXContent(builder, request);
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) TreeSet(java.util.TreeSet) Strings(org.elasticsearch.common.Strings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Consumer(java.util.function.Consumer) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) IndicesStatsRequest(org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest) Settings(org.elasticsearch.common.settings.Settings) RestActions.buildBroadcastShardsHeader(org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader) Locale(java.util.Locale) Map(java.util.Map) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) Collections(java.util.Collections) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) Set(java.util.Set) TreeSet(java.util.TreeSet) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) IOException(java.io.IOException) Consumer(java.util.function.Consumer) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IndicesStatsRequest(org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest)

Example 28 with IndicesStatsResponse

use of org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse in project elasticsearch by elastic.

the class DateMathIndexExpressionsIntegrationIT method testAutoCreateIndexWithDateMathExpression.

public void testAutoCreateIndexWithDateMathExpression() throws Exception {
    DateTime now = new DateTime(DateTimeZone.UTC);
    String index1 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now);
    String index2 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(1));
    String index3 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(2));
    String dateMathExp1 = "<.marvel-{now/d}>";
    String dateMathExp2 = "<.marvel-{now/d-1d}>";
    String dateMathExp3 = "<.marvel-{now/d-2d}>";
    client().prepareIndex(dateMathExp1, "type", "1").setSource("{}", XContentType.JSON).get();
    client().prepareIndex(dateMathExp2, "type", "2").setSource("{}", XContentType.JSON).get();
    client().prepareIndex(dateMathExp3, "type", "3").setSource("{}", XContentType.JSON).get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch(dateMathExp1, dateMathExp2, dateMathExp3).get();
    assertHitCount(searchResponse, 3);
    assertSearchHits(searchResponse, "1", "2", "3");
    IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats(dateMathExp1, dateMathExp2, dateMathExp3).get();
    assertThat(indicesStatsResponse.getIndex(index1), notNullValue());
    assertThat(indicesStatsResponse.getIndex(index2), notNullValue());
    assertThat(indicesStatsResponse.getIndex(index3), notNullValue());
}
Also used : IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 29 with IndicesStatsResponse

use of org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse in project elasticsearch by elastic.

the class DateMathIndexExpressionsIntegrationIT method testIndexNameDateMathExpressions.

public void testIndexNameDateMathExpressions() {
    DateTime now = new DateTime(DateTimeZone.UTC);
    String index1 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now);
    String index2 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(1));
    String index3 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(2));
    createIndex(index1, index2, index3);
    GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings(index1, index2, index3).get();
    assertEquals(index1, getSettingsResponse.getSetting(index1, IndexMetaData.SETTING_INDEX_PROVIDED_NAME));
    assertEquals(index2, getSettingsResponse.getSetting(index2, IndexMetaData.SETTING_INDEX_PROVIDED_NAME));
    assertEquals(index3, getSettingsResponse.getSetting(index3, IndexMetaData.SETTING_INDEX_PROVIDED_NAME));
    String dateMathExp1 = "<.marvel-{now/d}>";
    String dateMathExp2 = "<.marvel-{now/d-1d}>";
    String dateMathExp3 = "<.marvel-{now/d-2d}>";
    client().prepareIndex(dateMathExp1, "type", "1").setSource("{}", XContentType.JSON).get();
    client().prepareIndex(dateMathExp2, "type", "2").setSource("{}", XContentType.JSON).get();
    client().prepareIndex(dateMathExp3, "type", "3").setSource("{}", XContentType.JSON).get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch(dateMathExp1, dateMathExp2, dateMathExp3).get();
    assertHitCount(searchResponse, 3);
    assertSearchHits(searchResponse, "1", "2", "3");
    GetResponse getResponse = client().prepareGet(dateMathExp1, "type", "1").get();
    assertThat(getResponse.isExists(), is(true));
    assertThat(getResponse.getId(), equalTo("1"));
    getResponse = client().prepareGet(dateMathExp2, "type", "2").get();
    assertThat(getResponse.isExists(), is(true));
    assertThat(getResponse.getId(), equalTo("2"));
    getResponse = client().prepareGet(dateMathExp3, "type", "3").get();
    assertThat(getResponse.isExists(), is(true));
    assertThat(getResponse.getId(), equalTo("3"));
    MultiGetResponse mgetResponse = client().prepareMultiGet().add(dateMathExp1, "type", "1").add(dateMathExp2, "type", "2").add(dateMathExp3, "type", "3").get();
    assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true));
    assertThat(mgetResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
    assertThat(mgetResponse.getResponses()[1].getResponse().isExists(), is(true));
    assertThat(mgetResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
    assertThat(mgetResponse.getResponses()[2].getResponse().isExists(), is(true));
    assertThat(mgetResponse.getResponses()[2].getResponse().getId(), equalTo("3"));
    IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats(dateMathExp1, dateMathExp2, dateMathExp3).get();
    assertThat(indicesStatsResponse.getIndex(index1), notNullValue());
    assertThat(indicesStatsResponse.getIndex(index2), notNullValue());
    assertThat(indicesStatsResponse.getIndex(index3), notNullValue());
    DeleteResponse deleteResponse = client().prepareDelete(dateMathExp1, "type", "1").get();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    assertThat(deleteResponse.getId(), equalTo("1"));
    deleteResponse = client().prepareDelete(dateMathExp2, "type", "2").get();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    assertThat(deleteResponse.getId(), equalTo("2"));
    deleteResponse = client().prepareDelete(dateMathExp3, "type", "3").get();
    assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
    assertThat(deleteResponse.getId(), equalTo("3"));
}
Also used : MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 30 with IndicesStatsResponse

use of org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse in project elasticsearch by elastic.

the class IndexStatsIT method testThrottleStats.

public void testThrottleStats() throws Exception {
    assertAcked(prepareCreate("test").setSettings(settingsBuilder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, "1").put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, "0").put(MergePolicyConfig.INDEX_MERGE_POLICY_MAX_MERGE_AT_ONCE_SETTING.getKey(), "2").put(MergePolicyConfig.INDEX_MERGE_POLICY_SEGMENTS_PER_TIER_SETTING.getKey(), "2").put(MergeSchedulerConfig.MAX_THREAD_COUNT_SETTING.getKey(), "1").put(MergeSchedulerConfig.MAX_MERGE_COUNT_SETTING.getKey(), "1").put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC.name())));
    ensureGreen();
    long termUpto = 0;
    IndicesStatsResponse stats;
    // make sure we see throttling kicking in:
    boolean done = false;
    long start = System.currentTimeMillis();
    while (!done) {
        for (int i = 0; i < 100; i++) {
            // Provoke slowish merging by making many unique terms:
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < 100; j++) {
                sb.append(' ');
                sb.append(termUpto++);
            }
            client().prepareIndex("test", "type", "" + termUpto).setSource("field" + (i % 10), sb.toString()).get();
            if (i % 2 == 0) {
                refresh();
            }
        }
        refresh();
        stats = client().admin().indices().prepareStats().execute().actionGet();
        //nodesStats = client().admin().cluster().prepareNodesStats().setIndices(true).get();
        done = stats.getPrimaries().getIndexing().getTotal().getThrottleTime().millis() > 0;
        if (System.currentTimeMillis() - start > 300 * 1000) {
            //Wait 5 minutes for throttling to kick in
            fail("index throttling didn't kick in after 5 minutes of intense merging");
        }
    }
    // Optimize & flush and wait; else we sometimes get a "Delete Index failed - not acked"
    // when ESIntegTestCase.after tries to remove indices created by the test:
    logger.info("test: now optimize");
    client().admin().indices().prepareForceMerge("test").get();
    flush();
    logger.info("test: test done");
}
Also used : IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse)

Aggregations

IndicesStatsResponse (org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse)39 SearchResponse (org.elasticsearch.action.search.SearchResponse)9 NodesStatsResponse (org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse)7 IndicesStatsRequest (org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest)7 Settings (org.elasticsearch.common.settings.Settings)6 IndicesStatsRequestBuilder (org.elasticsearch.action.admin.indices.stats.IndicesStatsRequestBuilder)5 ShardStats (org.elasticsearch.action.admin.indices.stats.ShardStats)5 IOException (java.io.IOException)4 Locale (java.util.Locale)4 IndexStats (org.elasticsearch.action.admin.indices.stats.IndexStats)4 GetResponse (org.elasticsearch.action.get.GetResponse)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 Arrays (java.util.Arrays)3 List (java.util.List)3 Set (java.util.Set)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 NodeStats (org.elasticsearch.action.admin.cluster.node.stats.NodeStats)3 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2