Search in sources :

Example 76 with XContentParser

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

the class RestIndicesAliasesAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
    indicesAliasesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", indicesAliasesRequest.masterNodeTimeout()));
    indicesAliasesRequest.timeout(request.paramAsTime("timeout", indicesAliasesRequest.timeout()));
    try (XContentParser parser = request.contentParser()) {
        PARSER.parse(parser, indicesAliasesRequest, null);
    }
    if (indicesAliasesRequest.getAliasActions().isEmpty()) {
        throw new IllegalArgumentException("No action specified");
    }
    return channel -> client.admin().indices().aliases(indicesAliasesRequest, new AcknowledgedRestListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) AliasActions(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) XContentParser(org.elasticsearch.common.xcontent.XContentParser) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) ObjectParser(org.elasticsearch.common.xcontent.ObjectParser) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) ParseField(org.elasticsearch.common.ParseField) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 77 with XContentParser

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

the class BucketSelectorPipelineAggregationBuilder method parse.

public static BucketSelectorPipelineAggregationBuilder parse(String reducerName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token;
    Script script = null;
    String currentFieldName = null;
    Map<String, String> bucketsPathsMap = null;
    GapPolicy gapPolicy = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (BUCKETS_PATH.match(currentFieldName)) {
                bucketsPathsMap = new HashMap<>();
                bucketsPathsMap.put("_value", parser.text());
            } else if (GAP_POLICY.match(currentFieldName)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (BUCKETS_PATH.match(currentFieldName)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPathsMap = new HashMap<>();
                for (int i = 0; i < paths.size(); i++) {
                    bucketsPathsMap.put("_value" + i, paths.get(i));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } else if (BUCKETS_PATH.match(currentFieldName)) {
                Map<String, Object> map = parser.map();
                bucketsPathsMap = new HashMap<>();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + reducerName + "].");
        }
    }
    if (bucketsPathsMap == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + BUCKETS_PATH.getPreferredName() + "] for bucket_selector aggregation [" + reducerName + "]");
    }
    if (script == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + Script.SCRIPT_PARSE_FIELD.getPreferredName() + "] for bucket_selector aggregation [" + reducerName + "]");
    }
    BucketSelectorPipelineAggregationBuilder factory = new BucketSelectorPipelineAggregationBuilder(reducerName, bucketsPathsMap, script);
    if (gapPolicy != null) {
        factory.gapPolicy(gapPolicy);
    }
    return factory;
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) ParsingException(org.elasticsearch.common.ParsingException) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 78 with XContentParser

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

the class SmoothingModel method fromXContent.

public static SmoothingModel fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    String fieldName = null;
    SmoothingModel model = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            fieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (LinearInterpolation.PARSE_FIELD.match(fieldName)) {
                model = LinearInterpolation.fromXContent(parser);
            } else if (Laplace.PARSE_FIELD.match(fieldName)) {
                model = Laplace.fromXContent(parser);
            } else if (StupidBackoff.PARSE_FIELD.match(fieldName)) {
                model = StupidBackoff.fromXContent(parser);
            } else {
                throw new IllegalArgumentException("suggester[phrase] doesn't support object field [" + fieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[smoothing] unknown token [" + token + "] after [" + fieldName + "]");
        }
    }
    return model;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 79 with XContentParser

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

the class TermSuggestionBuilder method fromXContent.

public static TermSuggestionBuilder fromXContent(XContentParser parser) throws IOException {
    TermSuggestionBuilder tmpSuggestion = new TermSuggestionBuilder("_na_");
    XContentParser.Token token;
    String currentFieldName = null;
    String fieldname = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (SuggestionBuilder.ANALYZER_FIELD.match(currentFieldName)) {
                tmpSuggestion.analyzer(parser.text());
            } else if (SuggestionBuilder.FIELDNAME_FIELD.match(currentFieldName)) {
                fieldname = parser.text();
            } else if (SuggestionBuilder.SIZE_FIELD.match(currentFieldName)) {
                tmpSuggestion.size(parser.intValue());
            } else if (SuggestionBuilder.SHARDSIZE_FIELD.match(currentFieldName)) {
                tmpSuggestion.shardSize(parser.intValue());
            } else if (SUGGESTMODE_FIELD.match(currentFieldName)) {
                tmpSuggestion.suggestMode(SuggestMode.resolve(parser.text()));
            } else if (ACCURACY_FIELD.match(currentFieldName)) {
                tmpSuggestion.accuracy(parser.floatValue());
            } else if (SORT_FIELD.match(currentFieldName)) {
                tmpSuggestion.sort(SortBy.resolve(parser.text()));
            } else if (STRING_DISTANCE_FIELD.match(currentFieldName)) {
                tmpSuggestion.stringDistance(StringDistanceImpl.resolve(parser.text()));
            } else if (MAX_EDITS_FIELD.match(currentFieldName)) {
                tmpSuggestion.maxEdits(parser.intValue());
            } else if (MAX_INSPECTIONS_FIELD.match(currentFieldName)) {
                tmpSuggestion.maxInspections(parser.intValue());
            } else if (MAX_TERM_FREQ_FIELD.match(currentFieldName)) {
                tmpSuggestion.maxTermFreq(parser.floatValue());
            } else if (PREFIX_LENGTH_FIELD.match(currentFieldName)) {
                tmpSuggestion.prefixLength(parser.intValue());
            } else if (MIN_WORD_LENGTH_FIELD.match(currentFieldName)) {
                tmpSuggestion.minWordLength(parser.intValue());
            } else if (MIN_DOC_FREQ_FIELD.match(currentFieldName)) {
                tmpSuggestion.minDocFreq(parser.floatValue());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggester[term] doesn't support field [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "suggester[term] parsing failed on [" + currentFieldName + "]");
        }
    }
    // now we should have field name, check and copy fields over to the suggestion builder we return
    if (fieldname == null) {
        throw new ElasticsearchParseException("the required field option [" + FIELDNAME_FIELD.getPreferredName() + "] is missing");
    }
    return new TermSuggestionBuilder(fieldname, tmpSuggestion);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 80 with XContentParser

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

the class SnapshotShardFailure method fromXContent.

/**
     * Deserializes snapshot failure information from JSON
     *
     * @param parser JSON parser
     * @return snapshot failure information
     */
public static SnapshotShardFailure fromXContent(XContentParser parser) throws IOException {
    SnapshotShardFailure snapshotShardFailure = new SnapshotShardFailure();
    XContentParser.Token token = parser.currentToken();
    String index = null;
    String index_uuid = IndexMetaData.INDEX_UUID_NA_VALUE;
    int shardId = -1;
    if (token == XContentParser.Token.START_OBJECT) {
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                String currentFieldName = parser.currentName();
                token = parser.nextToken();
                if (token.isValue()) {
                    if ("index".equals(currentFieldName)) {
                        index = parser.text();
                    } else if ("index_uuid".equals(currentFieldName)) {
                        index_uuid = parser.text();
                    } else if ("node_id".equals(currentFieldName)) {
                        snapshotShardFailure.nodeId = parser.text();
                    } else if ("reason".equals(currentFieldName)) {
                        snapshotShardFailure.reason = parser.text();
                    } else if ("shard_id".equals(currentFieldName)) {
                        shardId = parser.intValue();
                    } else if ("status".equals(currentFieldName)) {
                        snapshotShardFailure.status = RestStatus.valueOf(parser.text());
                    } else {
                        throw new ElasticsearchParseException("unknown parameter [{}]", currentFieldName);
                    }
                }
            } else {
                throw new ElasticsearchParseException("unexpected token [{}]", token);
            }
        }
    } else {
        throw new ElasticsearchParseException("unexpected token [{}]", token);
    }
    if (index == null) {
        throw new ElasticsearchParseException("index name was not set");
    }
    if (shardId == -1) {
        throw new ElasticsearchParseException("index shard was not set");
    }
    snapshotShardFailure.shardId = new ShardId(index, index_uuid, shardId);
    return snapshotShardFailure;
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentParser (org.elasticsearch.common.xcontent.XContentParser)411 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)125 ParsingException (org.elasticsearch.common.ParsingException)109 IOException (java.io.IOException)69 BytesReference (org.elasticsearch.common.bytes.BytesReference)57 ArrayList (java.util.ArrayList)50 XContentType (org.elasticsearch.common.xcontent.XContentType)43 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)43 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)42 HashMap (java.util.HashMap)28 Matchers.containsString (org.hamcrest.Matchers.containsString)23 Map (java.util.Map)22 List (java.util.List)17 Settings (org.elasticsearch.common.settings.Settings)17 ShardId (org.elasticsearch.index.shard.ShardId)16 Script (org.elasticsearch.script.Script)16 ToXContent (org.elasticsearch.common.xcontent.ToXContent)15 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 NodeClient (org.elasticsearch.client.node.NodeClient)14 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)14