Search in sources :

Example 31 with IndicesOptions

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

the class IndicesOptionsIntegrationIT method testWildcardBehaviour.

public void testWildcardBehaviour() throws Exception {
    // Verify defaults for wildcards, when specifying no indices (*, _all, /)
    String[] indices = Strings.EMPTY_ARRAY;
    verify(search(indices), false);
    verify(msearch(null, indices), false);
    verify(clearCache(indices), false);
    verify(_flush(indices), false);
    verify(segments(indices), false);
    verify(stats(indices), false);
    verify(forceMerge(indices), false);
    verify(refreshBuilder(indices), false);
    verify(validateQuery(indices), true);
    verify(aliasExists(indices), false);
    verify(typesExists(indices), false);
    verify(getAliases(indices), false);
    verify(getFieldMapping(indices), false);
    verify(getMapping(indices), false);
    verify(getSettings(indices), false);
    // Now force allow_no_indices=true
    IndicesOptions options = IndicesOptions.fromOptions(false, true, true, false);
    verify(search(indices).setIndicesOptions(options), false);
    verify(msearch(options, indices).setIndicesOptions(options), false);
    verify(clearCache(indices).setIndicesOptions(options), false);
    verify(_flush(indices).setIndicesOptions(options), false);
    verify(segments(indices).setIndicesOptions(options), false);
    verify(stats(indices).setIndicesOptions(options), false);
    verify(forceMerge(indices).setIndicesOptions(options), false);
    verify(refreshBuilder(indices).setIndicesOptions(options), false);
    verify(validateQuery(indices).setIndicesOptions(options), false);
    verify(aliasExists(indices).setIndicesOptions(options), false);
    verify(typesExists(indices).setIndicesOptions(options), false);
    verify(getAliases(indices).setIndicesOptions(options), false);
    verify(getFieldMapping(indices).setIndicesOptions(options), false);
    verify(getMapping(indices).setIndicesOptions(options), false);
    verify(getSettings(indices).setIndicesOptions(options), false);
    assertAcked(prepareCreate("foobar"));
    client().prepareIndex("foobar", "type", "1").setSource("k", "v").setRefreshPolicy(IMMEDIATE).get();
    // Verify defaults for wildcards, with one wildcard expression and one existing index
    indices = new String[] { "foo*" };
    verify(search(indices), false, 1);
    verify(msearch(null, indices), false, 1);
    verify(clearCache(indices), false);
    verify(_flush(indices), false);
    verify(segments(indices), false);
    verify(stats(indices), false);
    verify(forceMerge(indices), false);
    verify(refreshBuilder(indices), false);
    verify(validateQuery(indices), false);
    verify(aliasExists(indices), false);
    verify(typesExists(indices), false);
    verify(getAliases(indices), false);
    verify(getFieldMapping(indices), false);
    verify(getMapping(indices), false);
    verify(getSettings(indices).setIndicesOptions(options), false);
    // Verify defaults for wildcards, with two wildcard expression and one existing index
    indices = new String[] { "foo*", "bar*" };
    verify(search(indices), false, 1);
    verify(msearch(null, indices), false, 1);
    verify(clearCache(indices), false);
    verify(_flush(indices), false);
    verify(segments(indices), false);
    verify(stats(indices), false);
    verify(forceMerge(indices), false);
    verify(refreshBuilder(indices), false);
    verify(validateQuery(indices), true);
    verify(aliasExists(indices), false);
    verify(typesExists(indices), false);
    verify(getAliases(indices), false);
    verify(getFieldMapping(indices), false);
    verify(getMapping(indices), false);
    verify(getSettings(indices).setIndicesOptions(options), false);
    // Now force allow_no_indices=true
    options = IndicesOptions.fromOptions(false, true, true, false);
    verify(search(indices).setIndicesOptions(options), false, 1);
    verify(msearch(options, indices).setIndicesOptions(options), false, 1);
    verify(clearCache(indices).setIndicesOptions(options), false);
    verify(_flush(indices).setIndicesOptions(options), false);
    verify(segments(indices).setIndicesOptions(options), false);
    verify(stats(indices).setIndicesOptions(options), false);
    verify(forceMerge(indices).setIndicesOptions(options), false);
    verify(refreshBuilder(indices).setIndicesOptions(options), false);
    verify(validateQuery(indices).setIndicesOptions(options), false);
    verify(aliasExists(indices).setIndicesOptions(options), false);
    verify(typesExists(indices).setIndicesOptions(options), false);
    verify(getAliases(indices).setIndicesOptions(options), false);
    verify(getFieldMapping(indices).setIndicesOptions(options), false);
    verify(getMapping(indices).setIndicesOptions(options), false);
    verify(getSettings(indices).setIndicesOptions(options), false);
}
Also used : IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Example 32 with IndicesOptions

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

the class IndicesOptionsIntegrationIT method testSpecifiedIndexUnavailableSnapshotRestore.

public void testSpecifiedIndexUnavailableSnapshotRestore() throws Exception {
    createIndex("test1");
    ensureGreen("test1");
    waitForRelocation();
    PutRepositoryResponse putRepositoryResponse = client().admin().cluster().preparePutRepository("dummy-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath())).get();
    assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
    client().admin().cluster().prepareCreateSnapshot("dummy-repo", "snap1").setWaitForCompletion(true).get();
    verify(snapshot("snap2", "test1", "test2"), true);
    verify(restore("snap1", "test1", "test2"), true);
    IndicesOptions options = IndicesOptions.strictExpandOpen();
    verify(snapshot("snap2", "test1", "test2").setIndicesOptions(options), true);
    verify(restore("snap1", "test1", "test2").setIndicesOptions(options), true);
    options = IndicesOptions.lenientExpandOpen();
    verify(snapshot("snap2", "test1", "test2").setIndicesOptions(options), false);
    verify(restore("snap2", "test1", "test2").setIndicesOptions(options), false);
    options = IndicesOptions.strictExpandOpen();
    createIndex("test2");
    //TODO: temporary work-around for #5531
    ensureGreen("test2");
    waitForRelocation();
    verify(snapshot("snap3", "test1", "test2").setIndicesOptions(options), false);
    verify(restore("snap3", "test1", "test2").setIndicesOptions(options), false);
}
Also used : PutRepositoryResponse(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse) IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Example 33 with IndicesOptions

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

the class IndexNameExpressionResolver method concreteIndices.

Index[] concreteIndices(Context context, String... indexExpressions) {
    if (indexExpressions == null || indexExpressions.length == 0) {
        indexExpressions = new String[] { MetaData.ALL };
    }
    MetaData metaData = context.getState().metaData();
    IndicesOptions options = context.getOptions();
    boolean failClosed = options.forbidClosedIndices() && options.ignoreUnavailable() == false;
    boolean failNoIndices = options.ignoreUnavailable() == false;
    // or multiple indices are specified yield different behaviour.
    if (indexExpressions.length == 1) {
        failNoIndices = options.allowNoIndices() == false;
    }
    List<String> expressions = Arrays.asList(indexExpressions);
    for (ExpressionResolver expressionResolver : expressionResolvers) {
        expressions = expressionResolver.resolve(context, expressions);
    }
    if (expressions.isEmpty()) {
        if (!options.allowNoIndices()) {
            IndexNotFoundException infe = new IndexNotFoundException((String) null);
            infe.setResources("index_expression", indexExpressions);
            throw infe;
        } else {
            return Index.EMPTY_ARRAY;
        }
    }
    final Set<Index> concreteIndices = new HashSet<>(expressions.size());
    for (String expression : expressions) {
        AliasOrIndex aliasOrIndex = metaData.getAliasAndIndexLookup().get(expression);
        if (aliasOrIndex == null) {
            if (failNoIndices) {
                IndexNotFoundException infe = new IndexNotFoundException(expression);
                infe.setResources("index_expression", expression);
                throw infe;
            } else {
                continue;
            }
        }
        Collection<IndexMetaData> resolvedIndices = aliasOrIndex.getIndices();
        if (resolvedIndices.size() > 1 && !options.allowAliasesToMultipleIndices()) {
            String[] indexNames = new String[resolvedIndices.size()];
            int i = 0;
            for (IndexMetaData indexMetaData : resolvedIndices) {
                indexNames[i++] = indexMetaData.getIndex().getName();
            }
            throw new IllegalArgumentException("Alias [" + expression + "] has more than one indices associated with it [" + Arrays.toString(indexNames) + "], can't execute a single index op");
        }
        for (IndexMetaData index : resolvedIndices) {
            if (index.getState() == IndexMetaData.State.CLOSE) {
                if (failClosed) {
                    throw new IndexClosedException(index.getIndex());
                } else {
                    if (options.forbidClosedIndices() == false) {
                        concreteIndices.add(index.getIndex());
                    }
                }
            } else if (index.getState() == IndexMetaData.State.OPEN) {
                concreteIndices.add(index.getIndex());
            } else {
                throw new IllegalStateException("index state [" + index.getState() + "] not supported");
            }
        }
    }
    if (options.allowNoIndices() == false && concreteIndices.isEmpty()) {
        IndexNotFoundException infe = new IndexNotFoundException((String) null);
        infe.setResources("index_expression", indexExpressions);
        throw infe;
    }
    return concreteIndices.toArray(new Index[concreteIndices.size()]);
}
Also used : Index(org.elasticsearch.index.Index) IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) HashSet(java.util.HashSet)

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