Search in sources :

Example 1 with ScriptField

use of org.opensearch.search.builder.SearchSourceBuilder.ScriptField in project OpenSearch by opensearch-project.

the class InnerHitBuilder method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    if (name != null) {
        builder.field(NAME_FIELD.getPreferredName(), name);
    }
    builder.field(IGNORE_UNMAPPED.getPreferredName(), ignoreUnmapped);
    builder.field(SearchSourceBuilder.FROM_FIELD.getPreferredName(), from);
    builder.field(SearchSourceBuilder.SIZE_FIELD.getPreferredName(), size);
    builder.field(SearchSourceBuilder.VERSION_FIELD.getPreferredName(), version);
    builder.field(SearchSourceBuilder.SEQ_NO_PRIMARY_TERM_FIELD.getPreferredName(), seqNoAndPrimaryTerm);
    builder.field(SearchSourceBuilder.EXPLAIN_FIELD.getPreferredName(), explain);
    builder.field(SearchSourceBuilder.TRACK_SCORES_FIELD.getPreferredName(), trackScores);
    if (fetchSourceContext != null) {
        builder.field(SearchSourceBuilder._SOURCE_FIELD.getPreferredName(), fetchSourceContext, params);
    }
    if (storedFieldsContext != null) {
        storedFieldsContext.toXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), builder);
    }
    if (docValueFields != null) {
        builder.startArray(SearchSourceBuilder.DOCVALUE_FIELDS_FIELD.getPreferredName());
        for (FieldAndFormat docValueField : docValueFields) {
            docValueField.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (fetchFields != null) {
        builder.startArray(SearchSourceBuilder.FETCH_FIELDS_FIELD.getPreferredName());
        for (FieldAndFormat docValueField : fetchFields) {
            docValueField.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (scriptFields != null) {
        builder.startObject(SearchSourceBuilder.SCRIPT_FIELDS_FIELD.getPreferredName());
        for (ScriptField scriptField : scriptFields) {
            scriptField.toXContent(builder, params);
        }
        builder.endObject();
    }
    if (sorts != null) {
        builder.startArray(SearchSourceBuilder.SORT_FIELD.getPreferredName());
        for (SortBuilder<?> sort : sorts) {
            sort.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (highlightBuilder != null) {
        builder.field(SearchSourceBuilder.HIGHLIGHT_FIELD.getPreferredName(), highlightBuilder, params);
    }
    if (innerCollapseBuilder != null) {
        builder.field(COLLAPSE_FIELD.getPreferredName(), innerCollapseBuilder);
    }
    builder.endObject();
    return builder;
}
Also used : ScriptField(org.opensearch.search.builder.SearchSourceBuilder.ScriptField) FieldAndFormat(org.opensearch.search.fetch.subphase.FieldAndFormat)

Example 2 with ScriptField

use of org.opensearch.search.builder.SearchSourceBuilder.ScriptField in project OpenSearch by opensearch-project.

the class TopHitsAggregationBuilder method doBuild.

@Override
protected TopHitsAggregatorFactory doBuild(QueryShardContext queryShardContext, AggregatorFactory parent, Builder subfactoriesBuilder) throws IOException {
    long innerResultWindow = from() + size();
    int maxInnerResultWindow = queryShardContext.getMapperService().getIndexSettings().getMaxInnerResultWindow();
    if (innerResultWindow > maxInnerResultWindow) {
        throw new IllegalArgumentException("Top hits result window is too large, the top hits aggregator [" + name + "]'s from + size must be less " + "than or equal to: [" + maxInnerResultWindow + "] but was [" + innerResultWindow + "]. This limit can be set by changing the [" + IndexSettings.MAX_INNER_RESULT_WINDOW_SETTING.getKey() + "] index level setting.");
    }
    List<ScriptFieldsContext.ScriptField> scriptFields = new ArrayList<>();
    if (this.scriptFields != null) {
        for (ScriptField field : this.scriptFields) {
            FieldScript.Factory factory = queryShardContext.compile(field.script(), FieldScript.CONTEXT);
            FieldScript.LeafFactory searchScript = factory.newFactory(field.script().getParams(), queryShardContext.lookup());
            scriptFields.add(new org.opensearch.search.fetch.subphase.ScriptFieldsContext.ScriptField(field.fieldName(), searchScript, field.ignoreFailure()));
        }
    }
    final Optional<SortAndFormats> optionalSort;
    if (sorts == null) {
        optionalSort = Optional.empty();
    } else {
        optionalSort = SortBuilder.buildSort(sorts, queryShardContext);
    }
    return new TopHitsAggregatorFactory(name, from, size, explain, version, seqNoAndPrimaryTerm, trackScores, optionalSort, highlightBuilder, storedFieldsContext, docValueFields, fetchFields, scriptFields, fetchSourceContext, queryShardContext, parent, subfactoriesBuilder, metadata);
}
Also used : FieldScript(org.opensearch.script.FieldScript) ArrayList(java.util.ArrayList) SortAndFormats(org.opensearch.search.sort.SortAndFormats) ScriptFieldsContext(org.opensearch.search.fetch.subphase.ScriptFieldsContext) ScriptField(org.opensearch.search.builder.SearchSourceBuilder.ScriptField)

Example 3 with ScriptField

use of org.opensearch.search.builder.SearchSourceBuilder.ScriptField in project OpenSearch by opensearch-project.

the class TopHitsAggregationBuilder method parse.

public static TopHitsAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
    TopHitsAggregationBuilder factory = new TopHitsAggregationBuilder(aggregationName);
    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 (SearchSourceBuilder.FROM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.from(parser.intValue());
            } else if (SearchSourceBuilder.SIZE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.size(parser.intValue());
            } else if (SearchSourceBuilder.VERSION_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.version(parser.booleanValue());
            } else if (SearchSourceBuilder.SEQ_NO_PRIMARY_TERM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.seqNoAndPrimaryTerm(parser.booleanValue());
            } else if (SearchSourceBuilder.EXPLAIN_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.explain(parser.booleanValue());
            } else if (SearchSourceBuilder.TRACK_SCORES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.trackScores(parser.booleanValue());
            } else if (SearchSourceBuilder._SOURCE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.fetchSource(FetchSourceContext.fromXContent(parser));
            } else if (SearchSourceBuilder.STORED_FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.storedFieldsContext = StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), parser);
            } else if (SearchSourceBuilder.SORT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.sort(parser.text());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (SearchSourceBuilder._SOURCE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.fetchSource(FetchSourceContext.fromXContent(parser));
            } else if (SearchSourceBuilder.SCRIPT_FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                List<ScriptField> scriptFields = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    String scriptFieldName = parser.currentName();
                    token = parser.nextToken();
                    if (token == XContentParser.Token.START_OBJECT) {
                        Script script = null;
                        boolean ignoreFailure = false;
                        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                            if (token == XContentParser.Token.FIELD_NAME) {
                                currentFieldName = parser.currentName();
                            } else if (token.isValue()) {
                                if (SearchSourceBuilder.SCRIPT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                                    script = Script.parse(parser);
                                } else if (SearchSourceBuilder.IGNORE_FAILURE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                                    ignoreFailure = 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 (SearchSourceBuilder.SCRIPT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                                    script = Script.parse(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());
                            }
                        }
                        scriptFields.add(new ScriptField(scriptFieldName, script, ignoreFailure));
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] in [" + currentFieldName + "] but found [" + token + "]", parser.getTokenLocation());
                    }
                }
                factory.scriptFields(scriptFields);
            } else if (SearchSourceBuilder.HIGHLIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.highlighter(HighlightBuilder.fromXContent(parser));
            } else if (SearchSourceBuilder.SORT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                List<SortBuilder<?>> sorts = SortBuilder.fromXContent(parser);
                factory.sorts(sorts);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (SearchSourceBuilder.STORED_FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.storedFieldsContext = StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), parser);
            } else if (SearchSourceBuilder.DOCVALUE_FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    FieldAndFormat ff = FieldAndFormat.fromXContent(parser);
                    factory.docValueField(ff.field, ff.format);
                }
            } else if (SearchSourceBuilder.FETCH_FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    FieldAndFormat ff = FieldAndFormat.fromXContent(parser);
                    factory.fetchField(ff.field, ff.format);
                }
            } else if (SearchSourceBuilder.SORT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                List<SortBuilder<?>> sorts = SortBuilder.fromXContent(parser);
                factory.sorts(sorts);
            } else if (SearchSourceBuilder._SOURCE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                factory.fetchSource(FetchSourceContext.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());
        }
    }
    return factory;
}
Also used : FieldScript(org.opensearch.script.FieldScript) Script(org.opensearch.script.Script) SortBuilder(org.opensearch.search.sort.SortBuilder) ScoreSortBuilder(org.opensearch.search.sort.ScoreSortBuilder) ScriptField(org.opensearch.search.builder.SearchSourceBuilder.ScriptField) ParsingException(org.opensearch.common.ParsingException) ArrayList(java.util.ArrayList) List(java.util.List) XContentParser(org.opensearch.common.xcontent.XContentParser) FieldAndFormat(org.opensearch.search.fetch.subphase.FieldAndFormat)

Example 4 with ScriptField

use of org.opensearch.search.builder.SearchSourceBuilder.ScriptField in project OpenSearch by opensearch-project.

the class TopHitsAggregationBuilder method internalXContent.

@Override
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.field(SearchSourceBuilder.FROM_FIELD.getPreferredName(), from);
    builder.field(SearchSourceBuilder.SIZE_FIELD.getPreferredName(), size);
    builder.field(SearchSourceBuilder.VERSION_FIELD.getPreferredName(), version);
    builder.field(SearchSourceBuilder.SEQ_NO_PRIMARY_TERM_FIELD.getPreferredName(), seqNoAndPrimaryTerm);
    builder.field(SearchSourceBuilder.EXPLAIN_FIELD.getPreferredName(), explain);
    if (fetchSourceContext != null) {
        builder.field(SearchSourceBuilder._SOURCE_FIELD.getPreferredName(), fetchSourceContext);
    }
    if (storedFieldsContext != null) {
        storedFieldsContext.toXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), builder);
    }
    if (docValueFields != null) {
        builder.startArray(SearchSourceBuilder.DOCVALUE_FIELDS_FIELD.getPreferredName());
        for (FieldAndFormat docValueField : docValueFields) {
            docValueField.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (fetchFields != null) {
        builder.startArray(SearchSourceBuilder.FETCH_FIELDS_FIELD.getPreferredName());
        for (FieldAndFormat docValueField : fetchFields) {
            docValueField.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (scriptFields != null) {
        builder.startObject(SearchSourceBuilder.SCRIPT_FIELDS_FIELD.getPreferredName());
        for (ScriptField scriptField : scriptFields) {
            scriptField.toXContent(builder, params);
        }
        builder.endObject();
    }
    if (sorts != null) {
        builder.startArray(SearchSourceBuilder.SORT_FIELD.getPreferredName());
        for (SortBuilder<?> sort : sorts) {
            sort.toXContent(builder, params);
        }
        builder.endArray();
    }
    if (trackScores) {
        builder.field(SearchSourceBuilder.TRACK_SCORES_FIELD.getPreferredName(), true);
    }
    if (highlightBuilder != null) {
        builder.field(SearchSourceBuilder.HIGHLIGHT_FIELD.getPreferredName(), highlightBuilder);
    }
    builder.endObject();
    return builder;
}
Also used : ScriptField(org.opensearch.search.builder.SearchSourceBuilder.ScriptField) FieldAndFormat(org.opensearch.search.fetch.subphase.FieldAndFormat)

Aggregations

ScriptField (org.opensearch.search.builder.SearchSourceBuilder.ScriptField)4 FieldAndFormat (org.opensearch.search.fetch.subphase.FieldAndFormat)3 ArrayList (java.util.ArrayList)2 FieldScript (org.opensearch.script.FieldScript)2 List (java.util.List)1 ParsingException (org.opensearch.common.ParsingException)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 Script (org.opensearch.script.Script)1 ScriptFieldsContext (org.opensearch.search.fetch.subphase.ScriptFieldsContext)1 ScoreSortBuilder (org.opensearch.search.sort.ScoreSortBuilder)1 SortAndFormats (org.opensearch.search.sort.SortAndFormats)1 SortBuilder (org.opensearch.search.sort.SortBuilder)1