Search in sources :

Example 1 with FieldAndFormat

use of org.opensearch.search.fetch.subphase.FieldAndFormat 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 2 with FieldAndFormat

use of org.opensearch.search.fetch.subphase.FieldAndFormat 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 3 with FieldAndFormat

use of org.opensearch.search.fetch.subphase.FieldAndFormat 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 4 with FieldAndFormat

use of org.opensearch.search.fetch.subphase.FieldAndFormat in project OpenSearch by opensearch-project.

the class InnerHitBuilderTests method randomInnerHits.

public static InnerHitBuilder randomInnerHits() {
    InnerHitBuilder innerHits = new InnerHitBuilder();
    innerHits.setName(randomAlphaOfLengthBetween(5, 16));
    innerHits.setFrom(randomIntBetween(0, 32));
    innerHits.setSize(randomIntBetween(0, 32));
    innerHits.setExplain(randomBoolean());
    innerHits.setVersion(randomBoolean());
    innerHits.setSeqNoAndPrimaryTerm(randomBoolean());
    innerHits.setTrackScores(randomBoolean());
    if (randomBoolean()) {
        innerHits.setStoredFieldNames(randomListStuff(16, () -> randomAlphaOfLengthBetween(1, 16)));
    }
    innerHits.setDocValueFields(randomListStuff(16, () -> new FieldAndFormat(randomAlphaOfLengthBetween(1, 16), null)));
    innerHits.setFetchFields(randomListStuff(16, () -> new FieldAndFormat(randomAlphaOfLengthBetween(1, 16), null)));
    // Random script fields deduped on their field name.
    Map<String, SearchSourceBuilder.ScriptField> scriptFields = new HashMap<>();
    for (SearchSourceBuilder.ScriptField field : randomListStuff(16, InnerHitBuilderTests::randomScript)) {
        scriptFields.put(field.fieldName(), field);
    }
    innerHits.setScriptFields(new HashSet<>(scriptFields.values()));
    FetchSourceContext randomFetchSourceContext;
    int randomInt = randomIntBetween(0, 2);
    if (randomInt == 0) {
        randomFetchSourceContext = new FetchSourceContext(true, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY);
    } else if (randomInt == 1) {
        randomFetchSourceContext = new FetchSourceContext(true, generateRandomStringArray(12, 16, false), generateRandomStringArray(12, 16, false));
    } else {
        randomFetchSourceContext = new FetchSourceContext(randomBoolean());
    }
    innerHits.setFetchSourceContext(randomFetchSourceContext);
    if (randomBoolean()) {
        innerHits.setSorts(randomListStuff(16, () -> SortBuilders.fieldSort(randomAlphaOfLengthBetween(5, 20)).order(randomFrom(SortOrder.values()))));
    }
    innerHits.setHighlightBuilder(HighlightBuilderTests.randomHighlighterBuilder());
    return innerHits;
}
Also used : FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) HashMap(java.util.HashMap) FieldAndFormat(org.opensearch.search.fetch.subphase.FieldAndFormat) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 5 with FieldAndFormat

use of org.opensearch.search.fetch.subphase.FieldAndFormat in project OpenSearch by opensearch-project.

the class InnerHitBuilderTests method mutate.

static InnerHitBuilder mutate(InnerHitBuilder original) throws IOException {
    final InnerHitBuilder copy = serializedCopy(original);
    List<Runnable> modifiers = new ArrayList<>(12);
    modifiers.add(() -> copy.setFrom(randomValueOtherThan(copy.getFrom(), () -> randomIntBetween(0, 128))));
    modifiers.add(() -> copy.setSize(randomValueOtherThan(copy.getSize(), () -> randomIntBetween(0, 128))));
    modifiers.add(() -> copy.setExplain(!copy.isExplain()));
    modifiers.add(() -> copy.setVersion(!copy.isVersion()));
    modifiers.add(() -> copy.setSeqNoAndPrimaryTerm(!copy.isSeqNoAndPrimaryTerm()));
    modifiers.add(() -> copy.setTrackScores(!copy.isTrackScores()));
    modifiers.add(() -> copy.setName(randomValueOtherThan(copy.getName(), () -> randomAlphaOfLengthBetween(1, 16))));
    modifiers.add(() -> {
        if (randomBoolean()) {
            copy.setDocValueFields(randomValueOtherThan(copy.getDocValueFields(), () -> randomListStuff(16, () -> new FieldAndFormat(randomAlphaOfLengthBetween(1, 16), null))));
        } else {
            copy.addDocValueField(randomAlphaOfLengthBetween(1, 16));
        }
    });
    modifiers.add(() -> {
        if (randomBoolean()) {
            copy.setFetchFields(randomValueOtherThan(copy.getFetchFields(), () -> randomListStuff(16, () -> new FieldAndFormat(randomAlphaOfLengthBetween(1, 16), null))));
        } else {
            copy.addFetchField(randomAlphaOfLengthBetween(1, 16));
        }
    });
    modifiers.add(() -> {
        if (randomBoolean()) {
            copy.setScriptFields(randomValueOtherThan(copy.getScriptFields(), () -> {
                return new HashSet<>(randomListStuff(16, InnerHitBuilderTests::randomScript));
            }));
        } else {
            SearchSourceBuilder.ScriptField script = randomScript();
            copy.addScriptField(script.fieldName(), script.script());
        }
    });
    modifiers.add(() -> copy.setFetchSourceContext(randomValueOtherThan(copy.getFetchSourceContext(), () -> {
        FetchSourceContext randomFetchSourceContext;
        if (randomBoolean()) {
            randomFetchSourceContext = new FetchSourceContext(randomBoolean());
        } else {
            randomFetchSourceContext = new FetchSourceContext(true, generateRandomStringArray(12, 16, false), generateRandomStringArray(12, 16, false));
        }
        return randomFetchSourceContext;
    })));
    modifiers.add(() -> {
        if (randomBoolean()) {
            final List<SortBuilder<?>> sortBuilders = randomValueOtherThan(copy.getSorts(), () -> {
                List<SortBuilder<?>> builders = randomListStuff(16, () -> SortBuilders.fieldSort(randomAlphaOfLengthBetween(5, 20)).order(randomFrom(SortOrder.values())));
                return builders;
            });
            copy.setSorts(sortBuilders);
        } else {
            copy.addSort(SortBuilders.fieldSort(randomAlphaOfLengthBetween(5, 20)));
        }
    });
    modifiers.add(() -> copy.setHighlightBuilder(randomValueOtherThan(copy.getHighlightBuilder(), HighlightBuilderTests::randomHighlighterBuilder)));
    modifiers.add(() -> {
        if (copy.getStoredFieldsContext() == null || randomBoolean()) {
            List<String> previous = copy.getStoredFieldsContext() == null ? Collections.emptyList() : copy.getStoredFieldsContext().fieldNames();
            List<String> newValues = randomValueOtherThan(previous, () -> randomListStuff(1, 16, () -> randomAlphaOfLengthBetween(1, 16)));
            copy.setStoredFieldNames(newValues);
        } else {
            copy.getStoredFieldsContext().addFieldName(randomAlphaOfLengthBetween(1, 16));
        }
    });
    randomFrom(modifiers).run();
    return copy;
}
Also used : SortBuilder(org.opensearch.search.sort.SortBuilder) ArrayList(java.util.ArrayList) FieldAndFormat(org.opensearch.search.fetch.subphase.FieldAndFormat) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) HighlightBuilderTests(org.opensearch.search.fetch.subphase.highlight.HighlightBuilderTests) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext)

Aggregations

FieldAndFormat (org.opensearch.search.fetch.subphase.FieldAndFormat)6 ScriptField (org.opensearch.search.builder.SearchSourceBuilder.ScriptField)3 ArrayList (java.util.ArrayList)2 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)2 FetchSourceContext (org.opensearch.search.fetch.subphase.FetchSourceContext)2 SortBuilder (org.opensearch.search.sort.SortBuilder)2 HashMap (java.util.HashMap)1 List (java.util.List)1 ParsingException (org.opensearch.common.ParsingException)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 FieldScript (org.opensearch.script.FieldScript)1 Script (org.opensearch.script.Script)1 SearchExtBuilder (org.opensearch.search.SearchExtBuilder)1 HighlightBuilderTests (org.opensearch.search.fetch.subphase.highlight.HighlightBuilderTests)1 ScoreSortBuilder (org.opensearch.search.sort.ScoreSortBuilder)1