Search in sources :

Example 21 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class HighlightField method fromXContent.

public static HighlightField fromXContent(XContentParser parser) throws IOException {
    ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation);
    String fieldName = parser.currentName();
    Text[] fragments = null;
    XContentParser.Token token = parser.nextToken();
    if (token == XContentParser.Token.START_ARRAY) {
        List<Text> values = new ArrayList<>();
        while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
            values.add(new Text(parser.text()));
        }
        fragments = values.toArray(new Text[values.size()]);
    } else if (token == XContentParser.Token.VALUE_NULL) {
        fragments = null;
    } else {
        throw new ParsingException(parser.getTokenLocation(), "unexpected token type [" + token + "]");
    }
    return new HighlightField(fieldName, fragments);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) Text(org.elasticsearch.common.text.Text) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 22 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class StoredFieldsContext method fromXContent.

public static StoredFieldsContext fromXContent(String fieldName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token = parser.currentToken();
    if (token == XContentParser.Token.VALUE_STRING) {
        return fromList(Collections.singletonList(parser.text()));
    } else if (token == XContentParser.Token.START_ARRAY) {
        ArrayList<String> list = new ArrayList<>();
        while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            list.add(parser.text());
        }
        return fromList(list);
    } else {
        throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] or [" + XContentParser.Token.START_ARRAY + "] in [" + fieldName + "] but found [" + token + "]", parser.getTokenLocation());
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 23 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class SearchProfileShardResults method parseSearchProfileResultsEntry.

private static void parseSearchProfileResultsEntry(XContentParser parser, Map<String, ProfileShardResult> searchProfileResults) throws IOException {
    XContentParser.Token token = parser.currentToken();
    ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser::getTokenLocation);
    List<QueryProfileShardResult> queryProfileResults = new ArrayList<>();
    AggregationProfileShardResult aggProfileShardResult = null;
    String id = null;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (ID_FIELD.equals(currentFieldName)) {
                id = parser.text();
            } else {
                throwUnknownField(currentFieldName, parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (SEARCHES_FIELD.equals(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    queryProfileResults.add(QueryProfileShardResult.fromXContent(parser));
                }
            } else if (AggregationProfileShardResult.AGGREGATIONS.equals(currentFieldName)) {
                aggProfileShardResult = AggregationProfileShardResult.fromXContent(parser);
            } else {
                throwUnknownField(currentFieldName, parser.getTokenLocation());
            }
        } else {
            throwUnknownToken(token, parser.getTokenLocation());
        }
    }
    searchProfileResults.put(id, new ProfileShardResult(queryProfileResults, aggProfileShardResult));
}
Also used : AggregationProfileShardResult(org.elasticsearch.search.profile.aggregation.AggregationProfileShardResult) QueryProfileShardResult(org.elasticsearch.search.profile.query.QueryProfileShardResult) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser) QueryProfileShardResult(org.elasticsearch.search.profile.query.QueryProfileShardResult) AggregationProfileShardResult(org.elasticsearch.search.profile.aggregation.AggregationProfileShardResult)

Example 24 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class SearchSourceBuilder method parseXContent.

/**
     * Parse some xContent into this SearchSourceBuilder, overwriting any values specified in the xContent. Use this if you need to set up
     * different defaults than a regular SearchSourceBuilder would have and use
     * {@link #fromXContent(QueryParseContext)} if you have normal defaults.
     */
public void parseXContent(QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token = parser.currentToken();
    String currentFieldName = null;
    if (token != XContentParser.Token.START_OBJECT && (token = parser.nextToken()) != XContentParser.Token.START_OBJECT) {
        throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] but found [" + token + "]", parser.getTokenLocation());
    }
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (FROM_FIELD.match(currentFieldName)) {
                from = parser.intValue();
            } else if (SIZE_FIELD.match(currentFieldName)) {
                size = parser.intValue();
            } else if (TIMEOUT_FIELD.match(currentFieldName)) {
                timeout = TimeValue.parseTimeValue(parser.text(), null, TIMEOUT_FIELD.getPreferredName());
            } else if (TERMINATE_AFTER_FIELD.match(currentFieldName)) {
                terminateAfter = parser.intValue();
            } else if (MIN_SCORE_FIELD.match(currentFieldName)) {
                minScore = parser.floatValue();
            } else if (VERSION_FIELD.match(currentFieldName)) {
                version = parser.booleanValue();
            } else if (EXPLAIN_FIELD.match(currentFieldName)) {
                explain = parser.booleanValue();
            } else if (TRACK_SCORES_FIELD.match(currentFieldName)) {
                trackScores = parser.booleanValue();
            } else if (_SOURCE_FIELD.match(currentFieldName)) {
                fetchSourceContext = FetchSourceContext.fromXContent(context.parser());
            } else if (STORED_FIELDS_FIELD.match(currentFieldName)) {
                storedFieldsContext = StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), context);
            } else if (SORT_FIELD.match(currentFieldName)) {
                sort(parser.text());
            } else if (PROFILE_FIELD.match(currentFieldName)) {
                profile = parser.booleanValue();
            } else if (FIELDS_FIELD.match(currentFieldName)) {
                throw new ParsingException(parser.getTokenLocation(), "Deprecated field [" + SearchSourceBuilder.FIELDS_FIELD + "] used, expected [" + SearchSourceBuilder.STORED_FIELDS_FIELD + "] instead");
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (QUERY_FIELD.match(currentFieldName)) {
                queryBuilder = context.parseInnerQueryBuilder();
            } else if (POST_FILTER_FIELD.match(currentFieldName)) {
                postQueryBuilder = context.parseInnerQueryBuilder();
            } else if (_SOURCE_FIELD.match(currentFieldName)) {
                fetchSourceContext = FetchSourceContext.fromXContent(context.parser());
            } else if (SCRIPT_FIELDS_FIELD.match(currentFieldName)) {
                scriptFields = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    scriptFields.add(new ScriptField(context));
                }
            } else if (INDICES_BOOST_FIELD.match(currentFieldName)) {
                DEPRECATION_LOGGER.deprecated("Object format in indices_boost is deprecated, please use array format instead");
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token.isValue()) {
                        indexBoosts.add(new IndexBoost(currentFieldName, parser.floatValue()));
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
                    }
                }
            } else if (AGGREGATIONS_FIELD.match(currentFieldName) || AGGS_FIELD.match(currentFieldName)) {
                aggregations = AggregatorFactories.parseAggregators(context);
            } else if (HIGHLIGHT_FIELD.match(currentFieldName)) {
                highlightBuilder = HighlightBuilder.fromXContent(context);
            } else if (SUGGEST_FIELD.match(currentFieldName)) {
                suggestBuilder = SuggestBuilder.fromXContent(context.parser());
            } else if (SORT_FIELD.match(currentFieldName)) {
                sorts = new ArrayList<>(SortBuilder.fromXContent(context));
            } else if (RESCORE_FIELD.match(currentFieldName)) {
                rescoreBuilders = new ArrayList<>();
                rescoreBuilders.add(RescoreBuilder.parseFromXContent(context));
            } else if (EXT_FIELD.match(currentFieldName)) {
                extBuilders = new ArrayList<>();
                String extSectionName = null;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        extSectionName = parser.currentName();
                    } else {
                        SearchExtBuilder searchExtBuilder = parser.namedObject(SearchExtBuilder.class, extSectionName, null);
                        if (searchExtBuilder.getWriteableName().equals(extSectionName) == false) {
                            throw new IllegalStateException("The parsed [" + searchExtBuilder.getClass().getName() + "] object has a " + "different writeable name compared to the name of the section that it was parsed from: found [" + searchExtBuilder.getWriteableName() + "] expected [" + extSectionName + "]");
                        }
                        extBuilders.add(searchExtBuilder);
                    }
                }
            } else if (SLICE.match(currentFieldName)) {
                sliceBuilder = SliceBuilder.fromXContent(context);
            } else if (COLLAPSE.match(currentFieldName)) {
                collapse = CollapseBuilder.fromXContent(context);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (STORED_FIELDS_FIELD.match(currentFieldName)) {
                storedFieldsContext = StoredFieldsContext.fromXContent(STORED_FIELDS_FIELD.getPreferredName(), context);
            } else if (DOCVALUE_FIELDS_FIELD.match(currentFieldName)) {
                docValueFields = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    if (token == XContentParser.Token.VALUE_STRING) {
                        docValueFields.add(parser.text());
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
                    }
                }
            } else if (INDICES_BOOST_FIELD.match(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    indexBoosts.add(new IndexBoost(context));
                }
            } else if (SORT_FIELD.match(currentFieldName)) {
                sorts = new ArrayList<>(SortBuilder.fromXContent(context));
            } else if (RESCORE_FIELD.match(currentFieldName)) {
                rescoreBuilders = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    rescoreBuilders.add(RescoreBuilder.parseFromXContent(context));
                }
            } else if (STATS_FIELD.match(currentFieldName)) {
                stats = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    if (token == XContentParser.Token.VALUE_STRING) {
                        stats.add(parser.text());
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
                    }
                }
            } else if (_SOURCE_FIELD.match(currentFieldName)) {
                fetchSourceContext = FetchSourceContext.fromXContent(context.parser());
            } else if (SEARCH_AFTER.match(currentFieldName)) {
                searchAfterBuilder = SearchAfterBuilder.fromXContent(parser);
            } else if (FIELDS_FIELD.match(currentFieldName)) {
                throw new ParsingException(parser.getTokenLocation(), "The field [" + SearchSourceBuilder.FIELDS_FIELD + "] is no longer supported, please use [" + SearchSourceBuilder.STORED_FIELDS_FIELD + "] to retrieve stored fields or _source filtering " + "if the field is not stored");
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
        }
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) SearchExtBuilder(org.elasticsearch.search.SearchExtBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 25 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class RescoreBuilder method parseFromXContent.

public static RescoreBuilder<?> parseFromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    RescoreBuilder<?> rescorer = null;
    Integer windowSize = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            fieldName = parser.currentName();
        } else if (token.isValue()) {
            if (WINDOW_SIZE_FIELD.match(fieldName)) {
                windowSize = parser.intValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "rescore doesn't support [" + fieldName + "]");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            // we only have QueryRescorer at this point
            if (QueryRescorerBuilder.NAME.equals(fieldName)) {
                rescorer = QueryRescorerBuilder.fromXContent(parseContext);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "rescore doesn't support rescorer with name [" + fieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "] after [" + fieldName + "]");
        }
    }
    if (rescorer == null) {
        throw new ParsingException(parser.getTokenLocation(), "missing rescore type");
    }
    if (windowSize != null) {
        rescorer.windowSize(windowSize.intValue());
    }
    return rescorer;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentParser (org.elasticsearch.common.xcontent.XContentParser)463 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)147 ParsingException (org.elasticsearch.common.ParsingException)110 IOException (java.io.IOException)77 BytesReference (org.elasticsearch.common.bytes.BytesReference)63 ArrayList (java.util.ArrayList)59 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)56 XContentType (org.elasticsearch.common.xcontent.XContentType)47 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)44 HashMap (java.util.HashMap)33 Map (java.util.Map)27 Matchers.containsString (org.hamcrest.Matchers.containsString)23 List (java.util.List)22 Settings (org.elasticsearch.common.settings.Settings)18 ToXContent (org.elasticsearch.common.xcontent.ToXContent)16 ShardId (org.elasticsearch.index.shard.ShardId)16 Script (org.elasticsearch.script.Script)16 XContent (org.elasticsearch.common.xcontent.XContent)15 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 NodeClient (org.elasticsearch.client.node.NodeClient)14