Search in sources :

Example 6 with XContentParser

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

the class BoolQueryBuilder method fromXContent.

public static BoolQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, ParsingException {
    XContentParser parser = parseContext.parser();
    boolean disableCoord = BoolQueryBuilder.DISABLE_COORD_DEFAULT;
    boolean adjustPureNegative = BoolQueryBuilder.ADJUST_PURE_NEGATIVE_DEFAULT;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String minimumShouldMatch = null;
    final List<QueryBuilder> mustClauses = new ArrayList<>();
    final List<QueryBuilder> mustNotClauses = new ArrayList<>();
    final List<QueryBuilder> shouldClauses = new ArrayList<>();
    final List<QueryBuilder> filterClauses = new ArrayList<>();
    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) {
            switch(currentFieldName) {
                case MUST:
                    mustClauses.add(parseContext.parseInnerQueryBuilder());
                    break;
                case SHOULD:
                    shouldClauses.add(parseContext.parseInnerQueryBuilder());
                    break;
                case FILTER:
                    filterClauses.add(parseContext.parseInnerQueryBuilder());
                    break;
                case MUST_NOT:
                case MUSTNOT:
                    mustNotClauses.add(parseContext.parseInnerQueryBuilder());
                    break;
                default:
                    throw new ParsingException(parser.getTokenLocation(), "[bool] query does not support [" + currentFieldName + "]");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                switch(currentFieldName) {
                    case MUST:
                        mustClauses.add(parseContext.parseInnerQueryBuilder());
                        break;
                    case SHOULD:
                        shouldClauses.add(parseContext.parseInnerQueryBuilder());
                        break;
                    case FILTER:
                        filterClauses.add(parseContext.parseInnerQueryBuilder());
                        break;
                    case MUST_NOT:
                    case MUSTNOT:
                        mustNotClauses.add(parseContext.parseInnerQueryBuilder());
                        break;
                    default:
                        throw new ParsingException(parser.getTokenLocation(), "bool query does not support [" + currentFieldName + "]");
                }
            }
        } else if (token.isValue()) {
            if (DISABLE_COORD_FIELD.match(currentFieldName)) {
                disableCoord = parser.booleanValue();
            } else if (MINIMUM_SHOULD_MATCH.match(currentFieldName)) {
                minimumShouldMatch = parser.textOrNull();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (ADJUST_PURE_NEGATIVE.match(currentFieldName)) {
                adjustPureNegative = parser.booleanValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[bool] query does not support [" + currentFieldName + "]");
            }
        }
    }
    BoolQueryBuilder boolQuery = new BoolQueryBuilder();
    for (QueryBuilder queryBuilder : mustClauses) {
        boolQuery.must(queryBuilder);
    }
    for (QueryBuilder queryBuilder : mustNotClauses) {
        boolQuery.mustNot(queryBuilder);
    }
    for (QueryBuilder queryBuilder : shouldClauses) {
        boolQuery.should(queryBuilder);
    }
    for (QueryBuilder queryBuilder : filterClauses) {
        boolQuery.filter(queryBuilder);
    }
    boolQuery.boost(boost);
    boolQuery.disableCoord(disableCoord);
    boolQuery.adjustPureNegative(adjustPureNegative);
    boolQuery.minimumShouldMatch(minimumShouldMatch);
    boolQuery.queryName(queryName);
    return boolQuery;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 7 with XContentParser

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

the class BoostingQueryBuilder method fromXContent.

public static BoostingQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    QueryBuilder positiveQuery = null;
    boolean positiveQueryFound = false;
    QueryBuilder negativeQuery = null;
    boolean negativeQueryFound = false;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    float negativeBoost = -1;
    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 (token == XContentParser.Token.START_OBJECT) {
            if (POSITIVE_FIELD.match(currentFieldName)) {
                positiveQuery = parseContext.parseInnerQueryBuilder();
                positiveQueryFound = true;
            } else if (NEGATIVE_FIELD.match(currentFieldName)) {
                negativeQuery = parseContext.parseInnerQueryBuilder();
                negativeQueryFound = true;
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[boosting] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (NEGATIVE_BOOST_FIELD.match(currentFieldName)) {
                negativeBoost = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[boosting] query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (!positiveQueryFound) {
        throw new ParsingException(parser.getTokenLocation(), "[boosting] query requires 'positive' query to be set'");
    }
    if (!negativeQueryFound) {
        throw new ParsingException(parser.getTokenLocation(), "[boosting] query requires 'negative' query to be set'");
    }
    if (negativeBoost < 0) {
        throw new ParsingException(parser.getTokenLocation(), "[boosting] query requires 'negative_boost' to be set to be a positive value'");
    }
    BoostingQueryBuilder boostingQuery = new BoostingQueryBuilder(positiveQuery, negativeQuery);
    boostingQuery.negativeBoost(negativeBoost);
    boostingQuery.boost(boost);
    boostingQuery.queryName(queryName);
    return boostingQuery;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 8 with XContentParser

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

the class ConstantScoreQueryBuilder method fromXContent.

public static ConstantScoreQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    QueryBuilder query = null;
    boolean queryFound = false;
    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 (parseContext.isDeprecatedSetting(currentFieldName)) {
        // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (INNER_QUERY_FIELD.match(currentFieldName)) {
                if (queryFound) {
                    throw new ParsingException(parser.getTokenLocation(), "[" + ConstantScoreQueryBuilder.NAME + "]" + " accepts only one 'filter' element.");
                }
                query = parseContext.parseInnerQueryBuilder();
                queryFound = true;
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[constant_score] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[constant_score] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "]");
        }
    }
    if (!queryFound) {
        throw new ParsingException(parser.getTokenLocation(), "[constant_score] requires a 'filter' element");
    }
    ConstantScoreQueryBuilder constantScoreBuilder = new ConstantScoreQueryBuilder(query);
    constantScoreBuilder.boost(boost);
    constantScoreBuilder.queryName(queryName);
    return constantScoreBuilder;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 9 with XContentParser

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

the class ExistsQueryBuilder method fromXContent.

public static ExistsQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldPattern = 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 (token.isValue()) {
            if (FIELD_FIELD.match(currentFieldName)) {
                fieldPattern = parser.text();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + ExistsQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + ExistsQueryBuilder.NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
        }
    }
    if (fieldPattern == null) {
        throw new ParsingException(parser.getTokenLocation(), "[" + ExistsQueryBuilder.NAME + "] must be provided with a [field]");
    }
    ExistsQueryBuilder builder = new ExistsQueryBuilder(fieldPattern);
    builder.queryName(queryName);
    builder.boost(boost);
    return builder;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 10 with XContentParser

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

the class FieldMaskingSpanQueryBuilder method fromXContent.

public static FieldMaskingSpanQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    SpanQueryBuilder inner = null;
    String field = null;
    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 (token == XContentParser.Token.START_OBJECT) {
            if (QUERY_FIELD.match(currentFieldName)) {
                QueryBuilder query = parseContext.parseInnerQueryBuilder();
                if (query instanceof SpanQueryBuilder == false) {
                    throw new ParsingException(parser.getTokenLocation(), "[field_masking_span] query must be of type span query");
                }
                inner = (SpanQueryBuilder) query;
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[field_masking_span] query does not support [" + currentFieldName + "]");
            }
        } else {
            if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (FIELD_FIELD.match(currentFieldName)) {
                field = parser.text();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[field_masking_span] query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (inner == null) {
        throw new ParsingException(parser.getTokenLocation(), "field_masking_span must have [query] span query clause");
    }
    if (field == null) {
        throw new ParsingException(parser.getTokenLocation(), "field_masking_span must have [field] set for it");
    }
    FieldMaskingSpanQueryBuilder queryBuilder = new FieldMaskingSpanQueryBuilder(inner, field);
    queryBuilder.boost(boost);
    queryBuilder.queryName(queryName);
    return queryBuilder;
}
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