Search in sources :

Example 1 with SearchExtBuilder

use of org.opensearch.search.SearchExtBuilder in project OpenSearch by opensearch-project.

the class SearchSourceBuilder method innerToXContent.

public XContentBuilder 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 (seqNoAndPrimaryTerm != null) {
        builder.field(SEQ_NO_PRIMARY_TERM_FIELD.getPreferredName(), seqNoAndPrimaryTerm);
    }
    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 (FieldAndFormat docValueField : docValueFields) {
            docValueField.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (fetchFields != null) {
        builder.startArray(FETCH_FIELDS_FIELD.getPreferredName());
        for (FieldAndFormat docValueField : fetchFields) {
            docValueField.toXContent(builder, params);
        }
        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 (trackTotalHitsUpTo != null) {
        builder.field(TRACK_TOTAL_HITS_FIELD.getPreferredName(), trackTotalHitsUpTo);
    }
    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 (RescorerBuilder<?> 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);
    }
    if (pointInTimeBuilder != null) {
        pointInTimeBuilder.toXContent(builder, params);
    }
    return builder;
}
Also used : SearchExtBuilder(org.opensearch.search.SearchExtBuilder) FieldAndFormat(org.opensearch.search.fetch.subphase.FieldAndFormat)

Example 2 with SearchExtBuilder

use of org.opensearch.search.SearchExtBuilder in project OpenSearch by opensearch-project.

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(XContentParser, boolean)} if you have
 * normal defaults.
 *
 * @param parser The xContent parser.
 * @param checkTrailingTokens If true throws a parsing exception when extra tokens are found after the main object.
 */
public void parseXContent(XContentParser parser, boolean checkTrailingTokens) throws IOException {
    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, parser.getDeprecationHandler())) {
                from = parser.intValue();
            } else if (SIZE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                size = parser.intValue();
            } else if (TIMEOUT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                timeout = TimeValue.parseTimeValue(parser.text(), null, TIMEOUT_FIELD.getPreferredName());
            } else if (TERMINATE_AFTER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                terminateAfter = parser.intValue();
            } else if (MIN_SCORE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                minScore = parser.floatValue();
            } else if (VERSION_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                version = parser.booleanValue();
            } else if (SEQ_NO_PRIMARY_TERM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                seqNoAndPrimaryTerm = parser.booleanValue();
            } else if (EXPLAIN_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                explain = parser.booleanValue();
            } else if (TRACK_SCORES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                trackScores = parser.booleanValue();
            } else if (TRACK_TOTAL_HITS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                if (token == XContentParser.Token.VALUE_BOOLEAN || (token == XContentParser.Token.VALUE_STRING && Booleans.isBoolean(parser.text()))) {
                    trackTotalHitsUpTo = parser.booleanValue() ? TRACK_TOTAL_HITS_ACCURATE : TRACK_TOTAL_HITS_DISABLED;
                } else {
                    trackTotalHitsUpTo = parser.intValue();
                }
            } else if (_SOURCE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                fetchSourceContext = FetchSourceContext.fromXContent(parser);
            } else if (STORED_FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                storedFieldsContext = StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), parser);
            } else if (SORT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                sort(parser.text());
            } else if (PROFILE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                profile = parser.booleanValue();
            } 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, parser.getDeprecationHandler())) {
                queryBuilder = parseInnerQueryBuilder(parser);
            } else if (POST_FILTER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                postQueryBuilder = parseInnerQueryBuilder(parser);
            } else if (_SOURCE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                fetchSourceContext = FetchSourceContext.fromXContent(parser);
            } else if (SCRIPT_FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                scriptFields = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    scriptFields.add(new ScriptField(parser));
                }
            } else if (INDICES_BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                deprecationLogger.deprecate("indices_boost_object_format", "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, parser.getDeprecationHandler()) || AGGS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                aggregations = AggregatorFactories.parseAggregators(parser);
            } else if (HIGHLIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                highlightBuilder = HighlightBuilder.fromXContent(parser);
            } else if (SUGGEST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                suggestBuilder = SuggestBuilder.fromXContent(parser);
            } else if (SORT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                sorts = new ArrayList<>(SortBuilder.fromXContent(parser));
            } else if (RESCORE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                rescoreBuilders = new ArrayList<>();
                rescoreBuilders.add(RescorerBuilder.parseFromXContent(parser));
            } else if (EXT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                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, parser.getDeprecationHandler())) {
                sliceBuilder = SliceBuilder.fromXContent(parser);
            } else if (COLLAPSE.match(currentFieldName, parser.getDeprecationHandler())) {
                collapse = CollapseBuilder.fromXContent(parser);
            } else if (POINT_IN_TIME.match(currentFieldName, parser.getDeprecationHandler())) {
                pointInTimeBuilder = PointInTimeBuilder.fromXContent(parser);
            } 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, parser.getDeprecationHandler())) {
                storedFieldsContext = StoredFieldsContext.fromXContent(STORED_FIELDS_FIELD.getPreferredName(), parser);
            } else if (DOCVALUE_FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                docValueFields = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    docValueFields.add(FieldAndFormat.fromXContent(parser));
                }
            } else if (FETCH_FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                fetchFields = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    fetchFields.add(FieldAndFormat.fromXContent(parser));
                }
            } else if (INDICES_BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    indexBoosts.add(new IndexBoost(parser));
                }
            } else if (SORT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                sorts = new ArrayList<>(SortBuilder.fromXContent(parser));
            } else if (RESCORE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                rescoreBuilders = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    rescoreBuilders.add(RescorerBuilder.parseFromXContent(parser));
                }
            } else if (STATS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                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, parser.getDeprecationHandler())) {
                fetchSourceContext = FetchSourceContext.fromXContent(parser);
            } else if (SEARCH_AFTER.match(currentFieldName, parser.getDeprecationHandler())) {
                searchAfterBuilder = SearchAfterBuilder.fromXContent(parser);
            } 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());
        }
    }
    if (checkTrailingTokens) {
        token = parser.nextToken();
        if (token != null) {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token [" + token + "] found after the main object.");
        }
    }
}
Also used : ParsingException(org.opensearch.common.ParsingException) ArrayList(java.util.ArrayList) SearchExtBuilder(org.opensearch.search.SearchExtBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

SearchExtBuilder (org.opensearch.search.SearchExtBuilder)2 ArrayList (java.util.ArrayList)1 ParsingException (org.opensearch.common.ParsingException)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 FieldAndFormat (org.opensearch.search.fetch.subphase.FieldAndFormat)1