private static void parseDocuments()

in server/src/main/java/org/elasticsearch/action/get/MultiGetRequest.java [418:558]


    private static void parseDocuments(
        XContentParser parser,
        List<Item> items,
        @Nullable String defaultIndex,
        @Nullable String[] defaultFields,
        @Nullable FetchSourceContext defaultFetchSource,
        @Nullable String defaultRouting,
        boolean allowExplicitIndex
    ) throws IOException {
        String currentFieldName = null;
        Token token;
        while ((token = parser.nextToken()) != Token.END_ARRAY) {
            if (token != Token.START_OBJECT) {
                throw new IllegalArgumentException("docs array element should include an object");
            }
            String index = defaultIndex;
            String id = null;
            String routing = defaultRouting;
            List<String> storedFields = null;
            long version = Versions.MATCH_ANY;
            VersionType versionType = VersionType.INTERNAL;

            FetchSourceContext fetchSourceContext = null;

            while ((token = parser.nextToken()) != Token.END_OBJECT) {
                if (token == Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                    if (INDEX.match(currentFieldName, parser.getDeprecationHandler())) {
                        if (allowExplicitIndex == false) {
                            throw new IllegalArgumentException("explicit index in multi get is not allowed");
                        }
                        index = parser.text();
                    } else if (ID.match(currentFieldName, parser.getDeprecationHandler())) {
                        id = parser.text();
                    } else if (ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
                        routing = parser.text();
                    } else if (FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
                        throw new ParsingException(
                            parser.getTokenLocation(),
                            "Unsupported field [fields] used, expected [stored_fields] instead"
                        );
                    } else if (STORED_FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
                        storedFields = new ArrayList<>();
                        storedFields.add(parser.text());
                    } else if (VERSION.match(currentFieldName, parser.getDeprecationHandler())) {
                        version = parser.longValue();
                    } else if (VERSION_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
                        versionType = VersionType.fromString(parser.text());
                    } else if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
                        if (parser.isBooleanValue()) {
                            fetchSourceContext = fetchSourceContext == null
                                ? FetchSourceContext.of(parser.booleanValue())
                                : FetchSourceContext.of(
                                    parser.booleanValue(),
                                    fetchSourceContext.includes(),
                                    fetchSourceContext.excludes()
                                );
                        } else if (token == Token.VALUE_STRING) {
                            fetchSourceContext = FetchSourceContext.of(
                                fetchSourceContext == null || fetchSourceContext.fetchSource(),
                                new String[] { parser.text() },
                                fetchSourceContext == null ? Strings.EMPTY_ARRAY : fetchSourceContext.excludes()
                            );
                        } else {
                            throw new ElasticsearchParseException("illegal type for _source: [{}]", token);
                        }
                    } else {
                        throw new ElasticsearchParseException("failed to parse multi get request. unknown field [{}]", currentFieldName);
                    }
                } else if (token == Token.START_ARRAY) {
                    if (FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
                        throw new ParsingException(
                            parser.getTokenLocation(),
                            "Unsupported field [fields] used, expected [stored_fields] instead"
                        );
                    } else if (STORED_FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
                        storedFields = new ArrayList<>();
                        while ((token = parser.nextToken()) != Token.END_ARRAY) {
                            storedFields.add(parser.text());
                        }
                    } else if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
                        ArrayList<String> includes = new ArrayList<>();
                        while ((token = parser.nextToken()) != Token.END_ARRAY) {
                            includes.add(parser.text());
                        }
                        fetchSourceContext = FetchSourceContext.of(
                            fetchSourceContext == null || fetchSourceContext.fetchSource(),
                            includes.toArray(Strings.EMPTY_ARRAY),
                            fetchSourceContext == null ? Strings.EMPTY_ARRAY : fetchSourceContext.excludes()
                        );
                    }

                } else if (token == Token.START_OBJECT) {
                    if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
                        List<String> currentList = null, includes = null, excludes = null;

                        while ((token = parser.nextToken()) != Token.END_OBJECT) {
                            if (token == 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 == Token.START_ARRAY) {
                                while ((token = parser.nextToken()) != 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 = FetchSourceContext.of(
                            fetchSourceContext == null || fetchSourceContext.fetchSource(),
                            includes == null ? Strings.EMPTY_ARRAY : includes.toArray(Strings.EMPTY_ARRAY),
                            excludes == null ? Strings.EMPTY_ARRAY : excludes.toArray(Strings.EMPTY_ARRAY)
                        );
                    }
                }
            }
            String[] aFields;
            if (storedFields != null) {
                aFields = storedFields.toArray(Strings.EMPTY_ARRAY);
            } else {
                aFields = defaultFields;
            }
            items.add(
                new Item(index, id).routing(routing)
                    .storedFields(aFields)
                    .version(version)
                    .versionType(versionType)
                    .fetchSourceContext(fetchSourceContext == null ? defaultFetchSource : fetchSourceContext)
            );
        }
    }