Search in sources :

Example 71 with XContentParser

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

the class TermsQueryBuilder method fromXContent.

public static TermsQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    List<Object> values = null;
    TermsLookup termsLookup = null;
    String queryName = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
        // skip
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (fieldName != null) {
                throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] query does not support multiple fields");
            }
            fieldName = currentFieldName;
            values = parseValues(parser);
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (fieldName != null) {
                throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] query does not support more than one field. " + "Already got: [" + fieldName + "] but also found [" + currentFieldName + "]");
            }
            fieldName = currentFieldName;
            termsLookup = TermsLookup.parseTermsLookup(parser);
        } else if (token.isValue()) {
            if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
        }
    }
    if (fieldName == null) {
        throw new ParsingException(parser.getTokenLocation(), "[" + TermsQueryBuilder.NAME + "] query requires a field name, " + "followed by array of terms or a document lookup specification");
    }
    return new TermsQueryBuilder(fieldName, values, termsLookup).boost(boost).queryName(queryName);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) TermsLookup(org.elasticsearch.indices.TermsLookup) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 72 with XContentParser

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

the class TypeQueryBuilder method fromXContent.

public static TypeQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    BytesRef type = null;
    String queryName = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (VALUE_FIELD.match(currentFieldName)) {
                type = parser.utf8Bytes();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter doesn't support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter doesn't support [" + currentFieldName + "]");
        }
    }
    if (type == null) {
        throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter needs to be provided with a value for the type");
    }
    return new TypeQueryBuilder(type).boost(boost).queryName(queryName);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) BytesRef(org.apache.lucene.util.BytesRef)

Example 73 with XContentParser

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

the class WildcardQueryBuilder method fromXContent.

public static WildcardQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    String rewrite = null;
    String value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
        // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else {
                    if (WILDCARD_FIELD.match(currentFieldName)) {
                        value = parser.text();
                    } else if (VALUE_FIELD.match(currentFieldName)) {
                        value = parser.text();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                        boost = parser.floatValue();
                    } else if (REWRITE_FIELD.match(currentFieldName)) {
                        rewrite = parser.textOrNull();
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[wildcard] query does not support [" + currentFieldName + "]");
                    }
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
            fieldName = parser.currentName();
            value = parser.text();
        }
    }
    return new WildcardQueryBuilder(fieldName, value).rewrite(rewrite).boost(boost).queryName(queryName);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 74 with XContentParser

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

the class WrapperQueryBuilder method fromXContent.

public static WrapperQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    XContentParser.Token token = parser.nextToken();
    if (token != XContentParser.Token.FIELD_NAME) {
        throw new ParsingException(parser.getTokenLocation(), "[wrapper] query malformed");
    }
    String fieldName = parser.currentName();
    if (!QUERY_FIELD.match(fieldName)) {
        throw new ParsingException(parser.getTokenLocation(), "[wrapper] query malformed, expected `query` but was " + fieldName);
    }
    parser.nextToken();
    byte[] source = parser.binaryValue();
    parser.nextToken();
    if (source == null) {
        throw new ParsingException(parser.getTokenLocation(), "wrapper query has no [query] specified");
    }
    return new WrapperQueryBuilder(source);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 75 with XContentParser

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

the class RestUpdateSettingsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    UpdateSettingsRequest updateSettingsRequest = updateSettingsRequest(Strings.splitStringByCommaToArray(request.param("index")));
    updateSettingsRequest.timeout(request.paramAsTime("timeout", updateSettingsRequest.timeout()));
    updateSettingsRequest.setPreserveExisting(request.paramAsBoolean("preserve_existing", updateSettingsRequest.isPreserveExisting()));
    updateSettingsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", updateSettingsRequest.masterNodeTimeout()));
    updateSettingsRequest.indicesOptions(IndicesOptions.fromRequest(request, updateSettingsRequest.indicesOptions()));
    Map<String, Object> settings = new HashMap<>();
    if (request.hasContent()) {
        try (XContentParser parser = request.contentParser()) {
            Map<String, Object> bodySettings = parser.map();
            Object innerBodySettings = bodySettings.get("settings");
            // clean up in case the body is wrapped with "settings" : { ... }
            if (innerBodySettings instanceof Map) {
                @SuppressWarnings("unchecked") Map<String, Object> innerBodySettingsMap = (Map<String, Object>) innerBodySettings;
                settings.putAll(innerBodySettingsMap);
            } else {
                settings.putAll(bodySettings);
            }
        }
    }
    updateSettingsRequest.settings(settings);
    return channel -> client.admin().indices().updateSettings(updateSettingsRequest, new AcknowledgedRestListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) RestController(org.elasticsearch.rest.RestController) Requests.updateSettingsRequest(org.elasticsearch.client.Requests.updateSettingsRequest) UpdateSettingsRequest(org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest) Strings(org.elasticsearch.common.Strings) XContentParser(org.elasticsearch.common.xcontent.XContentParser) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) UpdateSettingsRequest(org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) 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