Search in sources :

Example 1 with HighlightBuilderTests

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilderTests 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

ArrayList (java.util.ArrayList)1 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)1 FetchSourceContext (org.opensearch.search.fetch.subphase.FetchSourceContext)1 FieldAndFormat (org.opensearch.search.fetch.subphase.FieldAndFormat)1 HighlightBuilderTests (org.opensearch.search.fetch.subphase.highlight.HighlightBuilderTests)1 SortBuilder (org.opensearch.search.sort.SortBuilder)1