Search in sources :

Example 16 with IndicesOptions

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

the class IndexNameExpressionResolverTests method testConcreteIndicesWildcardNoMatch.

/**
     * test resolving wildcard pattern that matches no index of alias for random IndicesOptions
     */
public void testConcreteIndicesWildcardNoMatch() {
    for (int i = 0; i < 10; i++) {
        IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
        MetaData.Builder mdBuilder = MetaData.builder().put(indexBuilder("aaa").state(State.OPEN).putAlias(AliasMetaData.builder("aaa_alias1"))).put(indexBuilder("bbb").state(State.OPEN).putAlias(AliasMetaData.builder("bbb_alias1"))).put(indexBuilder("ccc").state(State.CLOSE).putAlias(AliasMetaData.builder("ccc_alias1")));
        ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
        IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, indicesOptions);
        // asking for non existing wildcard pattern should return empty list or exception
        if (indicesOptions.allowNoIndices()) {
            String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(context, "Foo*");
            assertThat(concreteIndices, notNullValue());
            assertThat(concreteIndices.length, equalTo(0));
        } else {
            try {
                indexNameExpressionResolver.concreteIndexNames(context, "Foo*");
                fail("expecting exception when result empty and allowNoIndicec=false");
            } catch (IndexNotFoundException e) {
            // expected exception
            }
        }
    }
}
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)

Example 17 with IndicesOptions

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

the class IndexNameExpressionResolverTests method testConcreteIndicesAllPatternRandom.

/**
     * test resolving _all pattern (null, empty array or "_all") for random IndicesOptions
     */
public void testConcreteIndicesAllPatternRandom() {
    for (int i = 0; i < 10; i++) {
        String[] allIndices = null;
        switch(randomIntBetween(0, 2)) {
            case 0:
                break;
            case 1:
                allIndices = new String[0];
                break;
            case 2:
                allIndices = new String[] { MetaData.ALL };
                break;
        }
        IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
        ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(MetaData.builder().build()).build();
        IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, indicesOptions);
        // with no indices, asking for all indices should return empty list or exception, depending on indices options
        if (indicesOptions.allowNoIndices()) {
            String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(context, allIndices);
            assertThat(concreteIndices, notNullValue());
            assertThat(concreteIndices.length, equalTo(0));
        } else {
            checkCorrectException(indexNameExpressionResolver, context, allIndices);
        }
        // with existing indices, asking for all indices should return all open/closed indices depending on options
        MetaData.Builder mdBuilder = MetaData.builder().put(indexBuilder("aaa").state(State.OPEN).putAlias(AliasMetaData.builder("aaa_alias1"))).put(indexBuilder("bbb").state(State.OPEN).putAlias(AliasMetaData.builder("bbb_alias1"))).put(indexBuilder("ccc").state(State.CLOSE).putAlias(AliasMetaData.builder("ccc_alias1")));
        state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
        context = new IndexNameExpressionResolver.Context(state, indicesOptions);
        if (indicesOptions.expandWildcardsOpen() || indicesOptions.expandWildcardsClosed() || indicesOptions.allowNoIndices()) {
            String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(context, allIndices);
            assertThat(concreteIndices, notNullValue());
            int expectedNumberOfIndices = 0;
            if (indicesOptions.expandWildcardsOpen()) {
                expectedNumberOfIndices += 2;
            }
            if (indicesOptions.expandWildcardsClosed()) {
                expectedNumberOfIndices += 1;
            }
            assertThat(concreteIndices.length, equalTo(expectedNumberOfIndices));
        } else {
            checkCorrectException(indexNameExpressionResolver, context, allIndices);
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterName(org.elasticsearch.cluster.ClusterName) Matchers.containsString(org.hamcrest.Matchers.containsString) IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Example 18 with IndicesOptions

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

the class RestMultiSearchAction method parseMultiLineRequest.

/**
     * Parses a multi-line {@link RestRequest} body, instantiating a {@link SearchRequest} for each line and applying the given consumer.
     */
public static void parseMultiLineRequest(RestRequest request, IndicesOptions indicesOptions, boolean allowExplicitIndex, BiConsumer<SearchRequest, XContentParser> consumer) throws IOException {
    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    String[] types = Strings.splitStringByCommaToArray(request.param("type"));
    String searchType = request.param("search_type");
    String routing = request.param("routing");
    final Tuple<XContentType, BytesReference> sourceTuple = request.contentOrSourceParam();
    final XContent xContent = sourceTuple.v1().xContent();
    final BytesReference data = sourceTuple.v2();
    int from = 0;
    int length = data.length();
    byte marker = xContent.streamSeparator();
    while (true) {
        int nextMarker = findNextMarker(marker, from, data, length);
        if (nextMarker == -1) {
            break;
        }
        // support first line with \n
        if (nextMarker == 0) {
            from = nextMarker + 1;
            continue;
        }
        SearchRequest searchRequest = new SearchRequest();
        if (indices != null) {
            searchRequest.indices(indices);
        }
        if (indicesOptions != null) {
            searchRequest.indicesOptions(indicesOptions);
        }
        if (types != null && types.length > 0) {
            searchRequest.types(types);
        }
        if (routing != null) {
            searchRequest.routing(routing);
        }
        if (searchType != null) {
            searchRequest.searchType(searchType);
        }
        IndicesOptions defaultOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
        // now parse the action
        if (nextMarker - from > 0) {
            try (XContentParser parser = xContent.createParser(request.getXContentRegistry(), data.slice(from, nextMarker - from))) {
                Map<String, Object> source = parser.map();
                for (Map.Entry<String, Object> entry : source.entrySet()) {
                    Object value = entry.getValue();
                    if ("index".equals(entry.getKey()) || "indices".equals(entry.getKey())) {
                        if (!allowExplicitIndex) {
                            throw new IllegalArgumentException("explicit index in multi search is not allowed");
                        }
                        searchRequest.indices(nodeStringArrayValue(value));
                    } else if ("type".equals(entry.getKey()) || "types".equals(entry.getKey())) {
                        searchRequest.types(nodeStringArrayValue(value));
                    } else if ("search_type".equals(entry.getKey()) || "searchType".equals(entry.getKey())) {
                        searchRequest.searchType(nodeStringValue(value, null));
                    } else if ("request_cache".equals(entry.getKey()) || "requestCache".equals(entry.getKey())) {
                        searchRequest.requestCache(nodeBooleanValue(value, entry.getKey()));
                    } else if ("preference".equals(entry.getKey())) {
                        searchRequest.preference(nodeStringValue(value, null));
                    } else if ("routing".equals(entry.getKey())) {
                        searchRequest.routing(nodeStringValue(value, null));
                    }
                }
                defaultOptions = IndicesOptions.fromMap(source, defaultOptions);
            }
        }
        searchRequest.indicesOptions(defaultOptions);
        // move pointers
        from = nextMarker + 1;
        // now for the body
        nextMarker = findNextMarker(marker, from, data, length);
        if (nextMarker == -1) {
            break;
        }
        BytesReference bytes = data.slice(from, nextMarker - from);
        try (XContentParser parser = xContent.createParser(request.getXContentRegistry(), bytes)) {
            consumer.accept(searchRequest, parser);
        }
        // move pointers
        from = nextMarker + 1;
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) XContentType(org.elasticsearch.common.xcontent.XContentType) XContent(org.elasticsearch.common.xcontent.XContent) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) Map(java.util.Map) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 19 with IndicesOptions

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

the class IndicesSegmentsRequestTests method testRequestOnClosedIndexIgnoreUnavailable.

/**
     * setting the "ignoreUnavailable" option prevents IndexClosedException
     */
public void testRequestOnClosedIndexIgnoreUnavailable() {
    client().admin().indices().prepareClose("test").get();
    IndicesOptions defaultOptions = new IndicesSegmentsRequest().indicesOptions();
    IndicesOptions testOptions = IndicesOptions.fromOptions(true, true, true, false, defaultOptions);
    IndicesSegmentResponse rsp = client().admin().indices().prepareSegments("test").setIndicesOptions(testOptions).get();
    assertEquals(0, rsp.getIndices().size());
}
Also used : IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Example 20 with IndicesOptions

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

the class DeleteByQueryRequestTests method testDeleteteByQueryRequestImplementsIndicesRequestReplaceable.

public void testDeleteteByQueryRequestImplementsIndicesRequestReplaceable() {
    int numIndices = between(1, 100);
    String[] indices = new String[numIndices];
    for (int i = 0; i < numIndices; i++) {
        indices[i] = randomSimpleString(random(), 1, 30);
    }
    SearchRequest searchRequest = new SearchRequest(indices);
    IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
    searchRequest.indicesOptions(indicesOptions);
    DeleteByQueryRequest request = new DeleteByQueryRequest(searchRequest);
    for (int i = 0; i < numIndices; i++) {
        assertEquals(indices[i], request.indices()[i]);
    }
    assertSame(indicesOptions, request.indicesOptions());
    assertSame(request.indicesOptions(), request.getSearchRequest().indicesOptions());
    int numNewIndices = between(1, 100);
    String[] newIndices = new String[numNewIndices];
    for (int i = 0; i < numNewIndices; i++) {
        newIndices[i] = randomSimpleString(random(), 1, 30);
    }
    request.indices(newIndices);
    for (int i = 0; i < numNewIndices; i++) {
        ;
        assertEquals(newIndices[i], request.indices()[i]);
    }
    for (int i = 0; i < numNewIndices; i++) {
        ;
        assertEquals(newIndices[i], request.getSearchRequest().indices()[i]);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString) 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