Search in sources :

Example 16 with FetchSourceContext

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

the class TopHitsTests method createTestAggregatorBuilder.

@Override
protected final TopHitsAggregationBuilder createTestAggregatorBuilder() {
    TopHitsAggregationBuilder factory = new TopHitsAggregationBuilder("foo");
    if (randomBoolean()) {
        factory.from(randomIntBetween(0, 10000));
    }
    if (randomBoolean()) {
        factory.size(randomIntBetween(0, 10000));
    }
    if (randomBoolean()) {
        factory.explain(randomBoolean());
    }
    if (randomBoolean()) {
        factory.version(randomBoolean());
    }
    if (randomBoolean()) {
        factory.trackScores(randomBoolean());
    }
    switch(randomInt(3)) {
        case 0:
            break;
        case 1:
            factory.storedField("_none_");
            break;
        case 2:
            factory.storedFields(Collections.emptyList());
            break;
        case 3:
            int fieldsSize = randomInt(25);
            List<String> fields = new ArrayList<>(fieldsSize);
            for (int i = 0; i < fieldsSize; i++) {
                fields.add(randomAsciiOfLengthBetween(5, 50));
            }
            factory.storedFields(fields);
            break;
        default:
            throw new IllegalStateException();
    }
    if (randomBoolean()) {
        int fieldDataFieldsSize = randomInt(25);
        for (int i = 0; i < fieldDataFieldsSize; i++) {
            factory.fieldDataField(randomAsciiOfLengthBetween(5, 50));
        }
    }
    if (randomBoolean()) {
        int scriptFieldsSize = randomInt(25);
        for (int i = 0; i < scriptFieldsSize; i++) {
            if (randomBoolean()) {
                factory.scriptField(randomAsciiOfLengthBetween(5, 50), new Script("foo"), randomBoolean());
            } else {
                factory.scriptField(randomAsciiOfLengthBetween(5, 50), new Script("foo"));
            }
        }
    }
    if (randomBoolean()) {
        FetchSourceContext fetchSourceContext;
        int branch = randomInt(5);
        String[] includes = new String[randomIntBetween(0, 20)];
        for (int i = 0; i < includes.length; i++) {
            includes[i] = randomAsciiOfLengthBetween(5, 20);
        }
        String[] excludes = new String[randomIntBetween(0, 20)];
        for (int i = 0; i < excludes.length; i++) {
            excludes[i] = randomAsciiOfLengthBetween(5, 20);
        }
        switch(branch) {
            case 0:
                fetchSourceContext = new FetchSourceContext(randomBoolean());
                break;
            case 1:
                fetchSourceContext = new FetchSourceContext(true, includes, excludes);
                break;
            case 2:
                fetchSourceContext = new FetchSourceContext(true, new String[] { randomAsciiOfLengthBetween(5, 20) }, new String[] { randomAsciiOfLengthBetween(5, 20) });
                break;
            case 3:
                fetchSourceContext = new FetchSourceContext(true, includes, excludes);
                break;
            case 4:
                fetchSourceContext = new FetchSourceContext(true, includes, null);
                break;
            case 5:
                fetchSourceContext = new FetchSourceContext(true, new String[] { randomAsciiOfLengthBetween(5, 20) }, null);
                break;
            default:
                throw new IllegalStateException();
        }
        factory.fetchSource(fetchSourceContext);
    }
    if (randomBoolean()) {
        int numSorts = randomIntBetween(1, 5);
        for (int i = 0; i < numSorts; i++) {
            int branch = randomInt(5);
            switch(branch) {
                case 0:
                    factory.sort(SortBuilders.fieldSort(randomAsciiOfLengthBetween(5, 20)).order(randomFrom(SortOrder.values())));
                    break;
                case 1:
                    factory.sort(SortBuilders.geoDistanceSort(randomAsciiOfLengthBetween(5, 20), AbstractQueryTestCase.randomGeohash(1, 12)).order(randomFrom(SortOrder.values())));
                    break;
                case 2:
                    factory.sort(SortBuilders.scoreSort().order(randomFrom(SortOrder.values())));
                    break;
                case 3:
                    factory.sort(SortBuilders.scriptSort(new Script("foo"), ScriptSortType.NUMBER).order(randomFrom(SortOrder.values())));
                    break;
                case 4:
                    factory.sort(randomAsciiOfLengthBetween(5, 20));
                    break;
                case 5:
                    factory.sort(randomAsciiOfLengthBetween(5, 20), randomFrom(SortOrder.values()));
                    break;
            }
        }
    }
    if (randomBoolean()) {
        factory.highlighter(HighlightBuilderTests.randomHighlighterBuilder());
    }
    return factory;
}
Also used : Script(org.elasticsearch.script.Script) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) TopHitsAggregationBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsAggregationBuilder) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 17 with FetchSourceContext

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

the class GetRequestBuilder method setFetchSource.

/**
     * Indicates whether the response should contain the stored _source.
     *
     * @return this for chaining
     */
public GetRequestBuilder setFetchSource(boolean fetch) {
    FetchSourceContext context = request.fetchSourceContext() == null ? FetchSourceContext.FETCH_SOURCE : request.fetchSourceContext();
    request.fetchSourceContext(new FetchSourceContext(fetch, context.includes(), context.excludes()));
    return this;
}
Also used : FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext)

Example 18 with FetchSourceContext

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

the class GetRequestBuilder method setFetchSource.

/**
     * Indicate that _source should be returned, with an "include" and/or "exclude" set which can include simple wildcard
     * elements.
     *
     * @param includes An optional list of include (optionally wildcarded) pattern to filter the returned _source
     * @param excludes An optional list of exclude (optionally wildcarded) pattern to filter the returned _source
     */
public GetRequestBuilder setFetchSource(@Nullable String[] includes, @Nullable String[] excludes) {
    FetchSourceContext context = request.fetchSourceContext() == null ? FetchSourceContext.FETCH_SOURCE : request.fetchSourceContext();
    request.fetchSourceContext(new FetchSourceContext(context.fetchSource(), includes, excludes));
    return this;
}
Also used : FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext)

Example 19 with FetchSourceContext

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

the class MultiGetRequest method parseDocuments.

public static void parseDocuments(XContentParser parser, List<Item> items, @Nullable String defaultIndex, @Nullable String defaultType, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, @Nullable String defaultRouting, boolean allowExplicitIndex) throws IOException {
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
        if (token != XContentParser.Token.START_OBJECT) {
            throw new IllegalArgumentException("docs array element should include an object");
        }
        String index = defaultIndex;
        String type = defaultType;
        String id = null;
        String routing = defaultRouting;
        String parent = null;
        List<String> storedFields = null;
        long version = Versions.MATCH_ANY;
        VersionType versionType = VersionType.INTERNAL;
        FetchSourceContext fetchSourceContext = FetchSourceContext.FETCH_SOURCE;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token.isValue()) {
                if ("_index".equals(currentFieldName)) {
                    if (!allowExplicitIndex) {
                        throw new IllegalArgumentException("explicit index in multi get is not allowed");
                    }
                    index = parser.text();
                } else if ("_type".equals(currentFieldName)) {
                    type = parser.text();
                } else if ("_id".equals(currentFieldName)) {
                    id = parser.text();
                } else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
                    routing = parser.text();
                } else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) {
                    parent = parser.text();
                } else if ("fields".equals(currentFieldName)) {
                    throw new ParsingException(parser.getTokenLocation(), "Unsupported field [fields] used, expected [stored_fields] instead");
                } else if ("stored_fields".equals(currentFieldName)) {
                    storedFields = new ArrayList<>();
                    storedFields.add(parser.text());
                } else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
                    version = parser.longValue();
                } else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
                    versionType = VersionType.fromString(parser.text());
                } else if ("_source".equals(currentFieldName)) {
                    // check lenient to avoid interpreting the value as string but parse strict in order to provoke an error early on.
                    if (parser.isBooleanValueLenient()) {
                        fetchSourceContext = new FetchSourceContext(parser.booleanValue(), fetchSourceContext.includes(), fetchSourceContext.excludes());
                    } else if (token == XContentParser.Token.VALUE_STRING) {
                        fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), new String[] { parser.text() }, fetchSourceContext.excludes());
                    } else {
                        throw new ElasticsearchParseException("illegal type for _source: [{}]", token);
                    }
                }
            } else if (token == XContentParser.Token.START_ARRAY) {
                if ("fields".equals(currentFieldName)) {
                    throw new ParsingException(parser.getTokenLocation(), "Unsupported field [fields] used, expected [stored_fields] instead");
                } else if ("stored_fields".equals(currentFieldName)) {
                    storedFields = new ArrayList<>();
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        storedFields.add(parser.text());
                    }
                } else if ("_source".equals(currentFieldName)) {
                    ArrayList<String> includes = new ArrayList<>();
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        includes.add(parser.text());
                    }
                    fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes.toArray(Strings.EMPTY_ARRAY), fetchSourceContext.excludes());
                }
            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("_source".equals(currentFieldName)) {
                    List<String> currentList = null, includes = null, excludes = null;
                    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                        if (token == XContentParser.Token.FIELD_NAME) {
                            currentFieldName = parser.currentName();
                            if ("includes".equals(currentFieldName) || "include".equals(currentFieldName)) {
                                currentList = includes != null ? includes : (includes = new ArrayList<>(2));
                            } else if ("excludes".equals(currentFieldName) || "exclude".equals(currentFieldName)) {
                                currentList = excludes != null ? excludes : (excludes = new ArrayList<>(2));
                            } else {
                                throw new ElasticsearchParseException("source definition may not contain [{}]", parser.text());
                            }
                        } else if (token == XContentParser.Token.START_ARRAY) {
                            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                                currentList.add(parser.text());
                            }
                        } else if (token.isValue()) {
                            currentList.add(parser.text());
                        } else {
                            throw new ElasticsearchParseException("unexpected token while parsing source settings");
                        }
                    }
                    fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes == null ? Strings.EMPTY_ARRAY : includes.toArray(new String[includes.size()]), excludes == null ? Strings.EMPTY_ARRAY : excludes.toArray(new String[excludes.size()]));
                }
            }
        }
        String[] aFields;
        if (storedFields != null) {
            aFields = storedFields.toArray(new String[storedFields.size()]);
        } else {
            aFields = defaultFields;
        }
        items.add(new Item(index, type, id).routing(routing).storedFields(aFields).parent(parent).version(version).versionType(versionType).fetchSourceContext(fetchSourceContext == FetchSourceContext.FETCH_SOURCE ? defaultFetchSource : fetchSourceContext));
    }
}
Also used : ArrayList(java.util.ArrayList) VersionType(org.elasticsearch.index.VersionType) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ArrayList(java.util.ArrayList) List(java.util.List) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 20 with FetchSourceContext

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

the class ExplainRequestBuilder method setFetchSource.

/**
     * Indicate that _source should be returned, with an "include" and/or "exclude" set which can include simple wildcard
     * elements.
     *
     * @param includes An optional list of include (optionally wildcarded) pattern to filter the returned _source
     * @param excludes An optional list of exclude (optionally wildcarded) pattern to filter the returned _source
     */
public ExplainRequestBuilder setFetchSource(@Nullable String[] includes, @Nullable String[] excludes) {
    FetchSourceContext fetchSourceContext = request.fetchSourceContext() != null ? request.fetchSourceContext() : FetchSourceContext.FETCH_SOURCE;
    request.fetchSourceContext(new FetchSourceContext(fetchSourceContext.fetchSource(), includes, excludes));
    return this;
}
Also used : FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext)

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