Search in sources :

Example 1 with SearchExtBuilder

use of org.elasticsearch.search.SearchExtBuilder in project elasticsearch by elastic.

the class SearchSourceBuilder method parseXContent.

/**
     * Parse some xContent into this SearchSourceBuilder, overwriting any values specified in the xContent. Use this if you need to set up
     * different defaults than a regular SearchSourceBuilder would have and use
     * {@link #fromXContent(QueryParseContext)} if you have normal defaults.
     */
public void parseXContent(QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token = parser.currentToken();
    String currentFieldName = null;
    if (token != XContentParser.Token.START_OBJECT && (token = parser.nextToken()) != XContentParser.Token.START_OBJECT) {
        throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] but found [" + token + "]", parser.getTokenLocation());
    }
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (FROM_FIELD.match(currentFieldName)) {
                from = parser.intValue();
            } else if (SIZE_FIELD.match(currentFieldName)) {
                size = parser.intValue();
            } else if (TIMEOUT_FIELD.match(currentFieldName)) {
                timeout = TimeValue.parseTimeValue(parser.text(), null, TIMEOUT_FIELD.getPreferredName());
            } else if (TERMINATE_AFTER_FIELD.match(currentFieldName)) {
                terminateAfter = parser.intValue();
            } else if (MIN_SCORE_FIELD.match(currentFieldName)) {
                minScore = parser.floatValue();
            } else if (VERSION_FIELD.match(currentFieldName)) {
                version = parser.booleanValue();
            } else if (EXPLAIN_FIELD.match(currentFieldName)) {
                explain = parser.booleanValue();
            } else if (TRACK_SCORES_FIELD.match(currentFieldName)) {
                trackScores = parser.booleanValue();
            } else if (_SOURCE_FIELD.match(currentFieldName)) {
                fetchSourceContext = FetchSourceContext.fromXContent(context.parser());
            } else if (STORED_FIELDS_FIELD.match(currentFieldName)) {
                storedFieldsContext = StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), context);
            } else if (SORT_FIELD.match(currentFieldName)) {
                sort(parser.text());
            } else if (PROFILE_FIELD.match(currentFieldName)) {
                profile = parser.booleanValue();
            } else if (FIELDS_FIELD.match(currentFieldName)) {
                throw new ParsingException(parser.getTokenLocation(), "Deprecated field [" + SearchSourceBuilder.FIELDS_FIELD + "] used, expected [" + SearchSourceBuilder.STORED_FIELDS_FIELD + "] instead");
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (QUERY_FIELD.match(currentFieldName)) {
                queryBuilder = context.parseInnerQueryBuilder();
            } else if (POST_FILTER_FIELD.match(currentFieldName)) {
                postQueryBuilder = context.parseInnerQueryBuilder();
            } else if (_SOURCE_FIELD.match(currentFieldName)) {
                fetchSourceContext = FetchSourceContext.fromXContent(context.parser());
            } else if (SCRIPT_FIELDS_FIELD.match(currentFieldName)) {
                scriptFields = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    scriptFields.add(new ScriptField(context));
                }
            } else if (INDICES_BOOST_FIELD.match(currentFieldName)) {
                DEPRECATION_LOGGER.deprecated("Object format in indices_boost is deprecated, please use array format instead");
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token.isValue()) {
                        indexBoosts.add(new IndexBoost(currentFieldName, parser.floatValue()));
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
                    }
                }
            } else if (AGGREGATIONS_FIELD.match(currentFieldName) || AGGS_FIELD.match(currentFieldName)) {
                aggregations = AggregatorFactories.parseAggregators(context);
            } else if (HIGHLIGHT_FIELD.match(currentFieldName)) {
                highlightBuilder = HighlightBuilder.fromXContent(context);
            } else if (SUGGEST_FIELD.match(currentFieldName)) {
                suggestBuilder = SuggestBuilder.fromXContent(context.parser());
            } else if (SORT_FIELD.match(currentFieldName)) {
                sorts = new ArrayList<>(SortBuilder.fromXContent(context));
            } else if (RESCORE_FIELD.match(currentFieldName)) {
                rescoreBuilders = new ArrayList<>();
                rescoreBuilders.add(RescoreBuilder.parseFromXContent(context));
            } else if (EXT_FIELD.match(currentFieldName)) {
                extBuilders = new ArrayList<>();
                String extSectionName = null;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        extSectionName = parser.currentName();
                    } else {
                        SearchExtBuilder searchExtBuilder = parser.namedObject(SearchExtBuilder.class, extSectionName, null);
                        if (searchExtBuilder.getWriteableName().equals(extSectionName) == false) {
                            throw new IllegalStateException("The parsed [" + searchExtBuilder.getClass().getName() + "] object has a " + "different writeable name compared to the name of the section that it was parsed from: found [" + searchExtBuilder.getWriteableName() + "] expected [" + extSectionName + "]");
                        }
                        extBuilders.add(searchExtBuilder);
                    }
                }
            } else if (SLICE.match(currentFieldName)) {
                sliceBuilder = SliceBuilder.fromXContent(context);
            } else if (COLLAPSE.match(currentFieldName)) {
                collapse = CollapseBuilder.fromXContent(context);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (STORED_FIELDS_FIELD.match(currentFieldName)) {
                storedFieldsContext = StoredFieldsContext.fromXContent(STORED_FIELDS_FIELD.getPreferredName(), context);
            } else if (DOCVALUE_FIELDS_FIELD.match(currentFieldName)) {
                docValueFields = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    if (token == XContentParser.Token.VALUE_STRING) {
                        docValueFields.add(parser.text());
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
                    }
                }
            } else if (INDICES_BOOST_FIELD.match(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    indexBoosts.add(new IndexBoost(context));
                }
            } else if (SORT_FIELD.match(currentFieldName)) {
                sorts = new ArrayList<>(SortBuilder.fromXContent(context));
            } else if (RESCORE_FIELD.match(currentFieldName)) {
                rescoreBuilders = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    rescoreBuilders.add(RescoreBuilder.parseFromXContent(context));
                }
            } else if (STATS_FIELD.match(currentFieldName)) {
                stats = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    if (token == XContentParser.Token.VALUE_STRING) {
                        stats.add(parser.text());
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
                    }
                }
            } else if (_SOURCE_FIELD.match(currentFieldName)) {
                fetchSourceContext = FetchSourceContext.fromXContent(context.parser());
            } else if (SEARCH_AFTER.match(currentFieldName)) {
                searchAfterBuilder = SearchAfterBuilder.fromXContent(parser);
            } else if (FIELDS_FIELD.match(currentFieldName)) {
                throw new ParsingException(parser.getTokenLocation(), "The field [" + SearchSourceBuilder.FIELDS_FIELD + "] is no longer supported, please use [" + SearchSourceBuilder.STORED_FIELDS_FIELD + "] to retrieve stored fields or _source filtering " + "if the field is not stored");
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
        }
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) SearchExtBuilder(org.elasticsearch.search.SearchExtBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 2 with SearchExtBuilder

use of org.elasticsearch.search.SearchExtBuilder in project elasticsearch by elastic.

the class SearchSourceBuilder method innerToXContent.

public void innerToXContent(XContentBuilder builder, Params params) throws IOException {
    if (from != -1) {
        builder.field(FROM_FIELD.getPreferredName(), from);
    }
    if (size != -1) {
        builder.field(SIZE_FIELD.getPreferredName(), size);
    }
    if (timeout != null && !timeout.equals(TimeValue.MINUS_ONE)) {
        builder.field(TIMEOUT_FIELD.getPreferredName(), timeout.getStringRep());
    }
    if (terminateAfter != SearchContext.DEFAULT_TERMINATE_AFTER) {
        builder.field(TERMINATE_AFTER_FIELD.getPreferredName(), terminateAfter);
    }
    if (queryBuilder != null) {
        builder.field(QUERY_FIELD.getPreferredName(), queryBuilder);
    }
    if (postQueryBuilder != null) {
        builder.field(POST_FILTER_FIELD.getPreferredName(), postQueryBuilder);
    }
    if (minScore != null) {
        builder.field(MIN_SCORE_FIELD.getPreferredName(), minScore);
    }
    if (version != null) {
        builder.field(VERSION_FIELD.getPreferredName(), version);
    }
    if (explain != null) {
        builder.field(EXPLAIN_FIELD.getPreferredName(), explain);
    }
    if (profile) {
        builder.field("profile", true);
    }
    if (fetchSourceContext != null) {
        builder.field(_SOURCE_FIELD.getPreferredName(), fetchSourceContext);
    }
    if (storedFieldsContext != null) {
        storedFieldsContext.toXContent(STORED_FIELDS_FIELD.getPreferredName(), builder);
    }
    if (docValueFields != null) {
        builder.startArray(DOCVALUE_FIELDS_FIELD.getPreferredName());
        for (String fieldDataField : docValueFields) {
            builder.value(fieldDataField);
        }
        builder.endArray();
    }
    if (scriptFields != null) {
        builder.startObject(SCRIPT_FIELDS_FIELD.getPreferredName());
        for (ScriptField scriptField : scriptFields) {
            scriptField.toXContent(builder, params);
        }
        builder.endObject();
    }
    if (sorts != null) {
        builder.startArray(SORT_FIELD.getPreferredName());
        for (SortBuilder<?> sort : sorts) {
            sort.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (trackScores) {
        builder.field(TRACK_SCORES_FIELD.getPreferredName(), true);
    }
    if (searchAfterBuilder != null) {
        builder.array(SEARCH_AFTER.getPreferredName(), searchAfterBuilder.getSortValues());
    }
    if (sliceBuilder != null) {
        builder.field(SLICE.getPreferredName(), sliceBuilder);
    }
    if (!indexBoosts.isEmpty()) {
        builder.startArray(INDICES_BOOST_FIELD.getPreferredName());
        for (IndexBoost ib : indexBoosts) {
            builder.startObject();
            builder.field(ib.index, ib.boost);
            builder.endObject();
        }
        builder.endArray();
    }
    if (aggregations != null) {
        builder.field(AGGREGATIONS_FIELD.getPreferredName(), aggregations);
    }
    if (highlightBuilder != null) {
        builder.field(HIGHLIGHT_FIELD.getPreferredName(), highlightBuilder);
    }
    if (suggestBuilder != null) {
        builder.field(SUGGEST_FIELD.getPreferredName(), suggestBuilder);
    }
    if (rescoreBuilders != null) {
        builder.startArray(RESCORE_FIELD.getPreferredName());
        for (RescoreBuilder<?> rescoreBuilder : rescoreBuilders) {
            rescoreBuilder.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (stats != null) {
        builder.field(STATS_FIELD.getPreferredName(), stats);
    }
    if (extBuilders != null && extBuilders.isEmpty() == false) {
        builder.startObject(EXT_FIELD.getPreferredName());
        for (SearchExtBuilder extBuilder : extBuilders) {
            extBuilder.toXContent(builder, params);
        }
        builder.endObject();
    }
    if (collapse != null) {
        builder.field(COLLAPSE.getPreferredName(), collapse);
    }
}
Also used : SearchExtBuilder(org.elasticsearch.search.SearchExtBuilder)

Aggregations

SearchExtBuilder (org.elasticsearch.search.SearchExtBuilder)2 ArrayList (java.util.ArrayList)1 ParsingException (org.elasticsearch.common.ParsingException)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1