Search in sources :

Example 11 with IndicesOptions

use of org.elasticsearch.action.support.IndicesOptions in project elasticsearch by elastic.

the class RestGetSettingsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] names = request.paramAsStringArrayOrEmptyIfAll("name");
    final boolean renderDefaults = request.paramAsBoolean("include_defaults", false);
    GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(Strings.splitStringByCommaToArray(request.param("index"))).indicesOptions(IndicesOptions.fromRequest(request, IndicesOptions.strictExpandOpen())).humanReadable(request.hasParam("human")).names(names);
    getSettingsRequest.local(request.paramAsBoolean("local", getSettingsRequest.local()));
    return channel -> client.admin().indices().getSettings(getSettingsRequest, new RestBuilderListener<GetSettingsResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetSettingsResponse getSettingsResponse, XContentBuilder builder) throws Exception {
            builder.startObject();
            for (ObjectObjectCursor<String, Settings> cursor : getSettingsResponse.getIndexToSettings()) {
                if (cursor.value.isEmpty()) {
                    continue;
                }
                builder.startObject(cursor.key);
                builder.startObject("settings");
                cursor.value.toXContent(builder, request);
                builder.endObject();
                if (renderDefaults) {
                    builder.startObject("defaults");
                    settingsFilter.filter(indexScopedSettings.diff(cursor.value, settings)).toXContent(builder, request);
                    builder.endObject();
                }
                builder.endObject();
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) GET(org.elasticsearch.rest.RestRequest.Method.GET) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) Settings(org.elasticsearch.common.settings.Settings) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) IOException(java.io.IOException) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 12 with IndicesOptions

use of org.elasticsearch.action.support.IndicesOptions in project elasticsearch by elastic.

the class SimpleClusterStateIT method testIndicesOptions.

public void testIndicesOptions() throws Exception {
    ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().clear().setMetaData(true).setIndices("f*").get();
    assertThat(clusterStateResponse.getState().metaData().indices().size(), is(2));
    // close one index
    client().admin().indices().close(Requests.closeIndexRequest("fuu")).get();
    clusterStateResponse = client().admin().cluster().prepareState().clear().setMetaData(true).setIndices("f*").get();
    assertThat(clusterStateResponse.getState().metaData().indices().size(), is(1));
    assertThat(clusterStateResponse.getState().metaData().index("foo").getState(), equalTo(IndexMetaData.State.OPEN));
    // expand_wildcards_closed should toggle return only closed index fuu
    IndicesOptions expandCloseOptions = IndicesOptions.fromOptions(false, true, false, true);
    clusterStateResponse = client().admin().cluster().prepareState().clear().setMetaData(true).setIndices("f*").setIndicesOptions(expandCloseOptions).get();
    assertThat(clusterStateResponse.getState().metaData().indices().size(), is(1));
    assertThat(clusterStateResponse.getState().metaData().index("fuu").getState(), equalTo(IndexMetaData.State.CLOSE));
    // ignore_unavailable set to true should not raise exception on fzzbzz
    IndicesOptions ignoreUnavailabe = IndicesOptions.fromOptions(true, true, true, false);
    clusterStateResponse = client().admin().cluster().prepareState().clear().setMetaData(true).setIndices("fzzbzz").setIndicesOptions(ignoreUnavailabe).get();
    assertThat(clusterStateResponse.getState().metaData().indices().isEmpty(), is(true));
    // empty wildcard expansion result should work when allowNoIndices is
    // turned on
    IndicesOptions allowNoIndices = IndicesOptions.fromOptions(false, true, true, false);
    clusterStateResponse = client().admin().cluster().prepareState().clear().setMetaData(true).setIndices("a*").setIndicesOptions(allowNoIndices).get();
    assertThat(clusterStateResponse.getState().metaData().indices().isEmpty(), is(true));
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Example 13 with IndicesOptions

use of org.elasticsearch.action.support.IndicesOptions in project elasticsearch by elastic.

the class SimpleClusterStateIT method testIndicesOptionsOnAllowNoIndicesFalse.

public void testIndicesOptionsOnAllowNoIndicesFalse() throws Exception {
    // empty wildcard expansion throws exception when allowNoIndices is turned off
    IndicesOptions allowNoIndices = IndicesOptions.fromOptions(false, false, true, false);
    try {
        client().admin().cluster().prepareState().clear().setMetaData(true).setIndices("a*").setIndicesOptions(allowNoIndices).get();
        fail("Expected IndexNotFoundException");
    } catch (IndexNotFoundException e) {
        assertThat(e.getMessage(), is("no such index"));
    }
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Example 14 with IndicesOptions

use of org.elasticsearch.action.support.IndicesOptions in project elasticsearch by elastic.

the class SimpleClusterStateIT method testIndicesIgnoreUnavailableFalse.

public void testIndicesIgnoreUnavailableFalse() throws Exception {
    // ignore_unavailable set to false throws exception when allowNoIndices is turned off
    IndicesOptions allowNoIndices = IndicesOptions.fromOptions(false, true, true, false);
    try {
        client().admin().cluster().prepareState().clear().setMetaData(true).setIndices("fzzbzz").setIndicesOptions(allowNoIndices).get();
        fail("Expected IndexNotFoundException");
    } catch (IndexNotFoundException e) {
        assertThat(e.getMessage(), is("no such index"));
    }
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Example 15 with IndicesOptions

use of org.elasticsearch.action.support.IndicesOptions in project elasticsearch by elastic.

the class IndexNameExpressionResolverTests method testIndexOptionsWildcardExpansion.

public void testIndexOptionsWildcardExpansion() {
    MetaData.Builder mdBuilder = MetaData.builder().put(indexBuilder("foo").state(IndexMetaData.State.CLOSE)).put(indexBuilder("bar")).put(indexBuilder("foobar").putAlias(AliasMetaData.builder("barbaz")));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
    // Only closed
    IndicesOptions options = IndicesOptions.fromOptions(false, true, false, true);
    IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, options);
    String[] results = indexNameExpressionResolver.concreteIndexNames(context, Strings.EMPTY_ARRAY);
    assertEquals(1, results.length);
    assertEquals("foo", results[0]);
    results = indexNameExpressionResolver.concreteIndexNames(context, "foo*");
    assertEquals(1, results.length);
    assertEquals("foo", results[0]);
    // no wildcards, so wildcard expansion don't apply
    results = indexNameExpressionResolver.concreteIndexNames(context, "bar");
    assertEquals(1, results.length);
    assertEquals("bar", results[0]);
    // Only open
    options = IndicesOptions.fromOptions(false, true, true, false);
    context = new IndexNameExpressionResolver.Context(state, options);
    results = indexNameExpressionResolver.concreteIndexNames(context, Strings.EMPTY_ARRAY);
    assertEquals(2, results.length);
    assertThat(results, arrayContainingInAnyOrder("bar", "foobar"));
    results = indexNameExpressionResolver.concreteIndexNames(context, "foo*");
    assertEquals(1, results.length);
    assertEquals("foobar", results[0]);
    results = indexNameExpressionResolver.concreteIndexNames(context, "bar");
    assertEquals(1, results.length);
    assertEquals("bar", results[0]);
    // Open and closed
    options = IndicesOptions.fromOptions(false, true, true, true);
    context = new IndexNameExpressionResolver.Context(state, options);
    results = indexNameExpressionResolver.concreteIndexNames(context, Strings.EMPTY_ARRAY);
    assertEquals(3, results.length);
    assertThat(results, arrayContainingInAnyOrder("bar", "foobar", "foo"));
    results = indexNameExpressionResolver.concreteIndexNames(context, "foo*");
    assertEquals(2, results.length);
    assertThat(results, arrayContainingInAnyOrder("foobar", "foo"));
    results = indexNameExpressionResolver.concreteIndexNames(context, "bar");
    assertEquals(1, results.length);
    assertEquals("bar", results[0]);
    results = indexNameExpressionResolver.concreteIndexNames(context, "*", "-foo*");
    assertEquals(1, results.length);
    assertEquals("bar", results[0]);
    results = indexNameExpressionResolver.concreteIndexNames(context, "-*");
    assertEquals(0, results.length);
    options = IndicesOptions.fromOptions(false, false, true, true);
    context = new IndexNameExpressionResolver.Context(state, options);
    try {
        indexNameExpressionResolver.concreteIndexNames(context, "-*");
        fail();
    } catch (IndexNotFoundException e) {
        assertThat(e.getResourceId().toString(), equalTo("[-*]"));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterName(org.elasticsearch.cluster.ClusterName) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Matchers.containsString(org.hamcrest.Matchers.containsString) IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Aggregations

IndicesOptions (org.elasticsearch.action.support.IndicesOptions)33 ClusterState (org.elasticsearch.cluster.ClusterState)10 ClusterName (org.elasticsearch.cluster.ClusterName)9 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 NodeClient (org.elasticsearch.client.node.NodeClient)3 Strings (org.elasticsearch.common.Strings)3 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 Settings (org.elasticsearch.common.settings.Settings)3 RestController (org.elasticsearch.rest.RestController)3 RestRequest (org.elasticsearch.rest.RestRequest)3 GET (org.elasticsearch.rest.RestRequest.Method.GET)3 RestResponse (org.elasticsearch.rest.RestResponse)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 TestUtil.randomSimpleString (org.apache.lucene.util.TestUtil.randomSimpleString)2 PutRepositoryResponse (org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse)2 CreateSnapshotRequest (org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest)2