Search in sources :

Example 66 with XContentParser

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

the class SpanFirstQueryBuilder method fromXContent.

public static SpanFirstQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    SpanQueryBuilder match = null;
    Integer end = 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 (MATCH_FIELD.match(currentFieldName)) {
                QueryBuilder query = parseContext.parseInnerQueryBuilder();
                if (query instanceof SpanQueryBuilder == false) {
                    throw new ParsingException(parser.getTokenLocation(), "spanFirst [match] must be of type span query");
                }
                match = (SpanQueryBuilder) query;
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_first] query does not support [" + currentFieldName + "]");
            }
        } else {
            if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (END_FIELD.match(currentFieldName)) {
                end = parser.intValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_first] query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (match == null) {
        throw new ParsingException(parser.getTokenLocation(), "spanFirst must have [match] span query clause");
    }
    if (end == null) {
        throw new ParsingException(parser.getTokenLocation(), "spanFirst must have [end] set for it");
    }
    SpanFirstQueryBuilder queryBuilder = new SpanFirstQueryBuilder(match, end);
    queryBuilder.boost(boost).queryName(queryName);
    return queryBuilder;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 67 with XContentParser

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

the class SpanNearQueryBuilder method fromXContent.

public static SpanNearQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    Integer slop = null;
    boolean inOrder = SpanNearQueryBuilder.DEFAULT_IN_ORDER;
    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(), "spanNear [clauses] must be of type span query");
                    }
                    clauses.add((SpanQueryBuilder) query);
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_near] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (IN_ORDER_FIELD.match(currentFieldName)) {
                inOrder = parser.booleanValue();
            } else if (SLOP_FIELD.match(currentFieldName)) {
                slop = parser.intValue();
            } 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_near] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[span_near] query does not support [" + currentFieldName + "]");
        }
    }
    if (clauses.isEmpty()) {
        throw new ParsingException(parser.getTokenLocation(), "span_near must include [clauses]");
    }
    if (slop == null) {
        throw new ParsingException(parser.getTokenLocation(), "span_near must include [slop]");
    }
    SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder(clauses.get(0), slop);
    for (int i = 1; i < clauses.size(); i++) {
        queryBuilder.addClause(clauses.get(i));
    }
    queryBuilder.inOrder(inOrder);
    queryBuilder.boost(boost);
    queryBuilder.queryName(queryName);
    return queryBuilder;
}
Also used : ArrayList(java.util.ArrayList) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 68 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser 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 69 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser 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 70 with XContentParser

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

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