Search in sources :

Example 36 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class SpanOrQueryBuilder method fromXContent.

public static SpanOrQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    List<SpanQueryBuilder> clauses = new ArrayList<>();
    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_ARRAY) {
            if (CLAUSES_FIELD.match(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    QueryBuilder query = parseContext.parseInnerQueryBuilder();
                    if (query instanceof SpanQueryBuilder == false) {
                        throw new ParsingException(parser.getTokenLocation(), "spanOr [clauses] must be of type span query");
                    }
                    clauses.add((SpanQueryBuilder) query);
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_or] query does not support [" + currentFieldName + "]");
            }
        } else {
            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(), "[span_or] query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (clauses.isEmpty()) {
        throw new ParsingException(parser.getTokenLocation(), "spanOr must include [clauses]");
    }
    SpanOrQueryBuilder queryBuilder = new SpanOrQueryBuilder(clauses.get(0));
    for (int i = 1; i < clauses.size(); i++) {
        queryBuilder.addClause(clauses.get(i));
    }
    queryBuilder.boost(boost);
    queryBuilder.queryName(queryName);
    return queryBuilder;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 37 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class SpanTermQueryBuilder method fromXContent.

public static SpanTermQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException, ParsingException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    Object 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 (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 (TERM_FIELD.match(currentFieldName)) {
                        value = parser.objectBytes();
                    } else if (BaseTermQueryBuilder.VALUE_FIELD.match(currentFieldName)) {
                        value = parser.objectBytes();
                    } else 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(), "[span_term] query does not support [" + currentFieldName + "]");
                    }
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectBytes();
        }
    }
    SpanTermQueryBuilder result = new SpanTermQueryBuilder(fieldName, value);
    result.boost(boost).queryName(queryName);
    return result;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 38 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class SpanWithinQueryBuilder method fromXContent.

public static SpanWithinQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    SpanQueryBuilder big = null;
    SpanQueryBuilder little = 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 (BIG_FIELD.match(currentFieldName)) {
                QueryBuilder query = parseContext.parseInnerQueryBuilder();
                if (query instanceof SpanQueryBuilder == false) {
                    throw new ParsingException(parser.getTokenLocation(), "span_within [big] must be of type span query");
                }
                big = (SpanQueryBuilder) query;
            } else if (LITTLE_FIELD.match(currentFieldName)) {
                QueryBuilder query = parseContext.parseInnerQueryBuilder();
                if (query instanceof SpanQueryBuilder == false) {
                    throw new ParsingException(parser.getTokenLocation(), "span_within [little] must be of type span query");
                }
                little = (SpanQueryBuilder) query;
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_within] query does not support [" + currentFieldName + "]");
            }
        } else 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(), "[span_within] query does not support [" + currentFieldName + "]");
        }
    }
    if (big == null) {
        throw new ParsingException(parser.getTokenLocation(), "span_within must include [big]");
    }
    if (little == null) {
        throw new ParsingException(parser.getTokenLocation(), "span_within must include [little]");
    }
    SpanWithinQueryBuilder query = new SpanWithinQueryBuilder(big, little);
    query.boost(boost).queryName(queryName);
    return query;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 39 with ParsingException

use of org.elasticsearch.common.ParsingException 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 40 with ParsingException

use of org.elasticsearch.common.ParsingException 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)

Aggregations

ParsingException (org.elasticsearch.common.ParsingException)165 XContentParser (org.elasticsearch.common.xcontent.XContentParser)96 ArrayList (java.util.ArrayList)26 Matchers.containsString (org.hamcrest.Matchers.containsString)22 IOException (java.io.IOException)12 HashMap (java.util.HashMap)10 Token (org.elasticsearch.common.xcontent.XContentParser.Token)9 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)7 Index (org.elasticsearch.index.Index)7 Script (org.elasticsearch.script.Script)7 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)7 List (java.util.List)6 BytesReference (org.elasticsearch.common.bytes.BytesReference)6 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)6 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)5 GeoPoint (org.elasticsearch.common.geo.GeoPoint)5 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)5 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)4 ToXContent (org.elasticsearch.common.xcontent.ToXContent)4 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)4