Search in sources :

Example 26 with FetchSourceContext

use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.

the class RestMultiGetAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    multiGetRequest.refresh(request.paramAsBoolean("refresh", multiGetRequest.refresh()));
    multiGetRequest.preference(request.param("preference"));
    multiGetRequest.realtime(request.paramAsBoolean("realtime", multiGetRequest.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 filtering if the field is not stored");
    }
    String[] sFields = null;
    String sField = request.param("stored_fields");
    if (sField != null) {
        sFields = Strings.splitStringByCommaToArray(sField);
    }
    FetchSourceContext defaultFetchSource = FetchSourceContext.parseFromRestRequest(request);
    try (XContentParser parser = request.contentOrSourceParamParser()) {
        multiGetRequest.add(request.param("index"), request.param("type"), sFields, defaultFetchSource, request.param("routing"), parser, allowExplicitIndex);
    }
    return channel -> client.multiGet(multiGetRequest, new RestToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestToXContentListener(org.elasticsearch.rest.action.RestToXContentListener) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) XContentParser(org.elasticsearch.common.xcontent.XContentParser) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Example 27 with FetchSourceContext

use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.

the class RequestTests method randomizeFetchSourceContextParams.

/**
     * Randomize the {@link FetchSourceContext} request parameters.
     */
private static void randomizeFetchSourceContextParams(Consumer<FetchSourceContext> consumer, Map<String, String> expectedParams) {
    if (randomBoolean()) {
        if (randomBoolean()) {
            boolean fetchSource = randomBoolean();
            consumer.accept(new FetchSourceContext(fetchSource));
            if (fetchSource == false) {
                expectedParams.put("_source", "false");
            }
        } else {
            int numIncludes = randomIntBetween(0, 5);
            String[] includes = new String[numIncludes];
            StringBuilder includesParam = new StringBuilder();
            for (int i = 0; i < numIncludes; i++) {
                String include = randomAsciiOfLengthBetween(3, 10);
                includes[i] = include;
                includesParam.append(include);
                if (i < numIncludes - 1) {
                    includesParam.append(",");
                }
            }
            if (numIncludes > 0) {
                expectedParams.put("_source_include", includesParam.toString());
            }
            int numExcludes = randomIntBetween(0, 5);
            String[] excludes = new String[numExcludes];
            StringBuilder excludesParam = new StringBuilder();
            for (int i = 0; i < numExcludes; i++) {
                String exclude = randomAsciiOfLengthBetween(3, 10);
                excludes[i] = exclude;
                excludesParam.append(exclude);
                if (i < numExcludes - 1) {
                    excludesParam.append(",");
                }
            }
            if (numExcludes > 0) {
                expectedParams.put("_source_exclude", excludesParam.toString());
            }
            consumer.accept(new FetchSourceContext(true, includes, excludes));
        }
    }
}
Also used : FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext)

Example 28 with FetchSourceContext

use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.

the class ExplainRequestTests method testSerialize.

public void testSerialize() throws IOException {
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        ExplainRequest request = new ExplainRequest("index", "type", "id");
        request.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1.*" }, new String[] { "field2.*" }));
        request.filteringAlias(new AliasFilter(QueryBuilders.termQuery("filter_field", "value"), new String[] { "alias0", "alias1" }));
        request.preference("the_preference");
        request.query(QueryBuilders.termQuery("field", "value"));
        request.storedFields(new String[] { "field1", "field2" });
        request.routing("some_routing");
        request.writeTo(output);
        try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
            ExplainRequest readRequest = new ExplainRequest();
            readRequest.readFrom(in);
            assertEquals(request.filteringAlias(), readRequest.filteringAlias());
            assertArrayEquals(request.storedFields(), readRequest.storedFields());
            assertEquals(request.preference(), readRequest.preference());
            assertEquals(request.query(), readRequest.query());
            assertEquals(request.routing(), readRequest.routing());
            assertEquals(request.fetchSourceContext(), readRequest.fetchSourceContext());
        }
    }
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 29 with FetchSourceContext

use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project gora by apache.

the class ElasticsearchStore method exists.

@Override
public boolean exists(K key) throws GoraException {
    GetRequest getRequest = new GetRequest(elasticsearchMapping.getIndexName(), (String) key);
    getRequest.fetchSourceContext(new FetchSourceContext(false)).storedFields("_none_");
    try {
        return client.exists(getRequest, RequestOptions.DEFAULT);
    } catch (IOException ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) GetRequest(org.elasticsearch.action.get.GetRequest) IOException(java.io.IOException)

Aggregations

FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)29 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 XContentParser (org.elasticsearch.common.xcontent.XContentParser)4 NodeClient (org.elasticsearch.client.node.NodeClient)3 Strings (org.elasticsearch.common.Strings)3 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)3 StreamInput (org.elasticsearch.common.io.stream.StreamInput)3 Settings (org.elasticsearch.common.settings.Settings)3 VersionType (org.elasticsearch.index.VersionType)3 Map (java.util.Map)2 ExplainRequest (org.elasticsearch.action.explain.ExplainRequest)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 ActiveShardCount (org.elasticsearch.action.support.ActiveShardCount)2 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)2 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)2 DeprecationLogger (org.elasticsearch.common.logging.DeprecationLogger)2 Loggers (org.elasticsearch.common.logging.Loggers)2 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)2