Search in sources :

Example 1 with StoredFieldsContext

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

the class RestSearchAction method parseSearchSource.

/**
 * Parses the rest request on top of the SearchSourceBuilder, preserving
 * values that are not overridden by the rest request.
 */
private static void parseSearchSource(final SearchSourceBuilder searchSourceBuilder, RestRequest request, IntConsumer setSize) {
    QueryBuilder queryBuilder = RestActions.urlParamsToQueryBuilder(request);
    if (queryBuilder != null) {
        searchSourceBuilder.query(queryBuilder);
    }
    int from = request.paramAsInt("from", -1);
    if (from != -1) {
        searchSourceBuilder.from(from);
    }
    int size = request.paramAsInt("size", -1);
    if (size != -1) {
        setSize.accept(size);
    }
    if (request.hasParam("explain")) {
        searchSourceBuilder.explain(request.paramAsBoolean("explain", null));
    }
    if (request.hasParam("version")) {
        searchSourceBuilder.version(request.paramAsBoolean("version", null));
    }
    if (request.hasParam("seq_no_primary_term")) {
        searchSourceBuilder.seqNoAndPrimaryTerm(request.paramAsBoolean("seq_no_primary_term", null));
    }
    if (request.hasParam("timeout")) {
        searchSourceBuilder.timeout(request.paramAsTime("timeout", null));
    }
    if (request.hasParam("terminate_after")) {
        int terminateAfter = request.paramAsInt("terminate_after", SearchContext.DEFAULT_TERMINATE_AFTER);
        if (terminateAfter < 0) {
            throw new IllegalArgumentException("terminateAfter must be > 0");
        } else if (terminateAfter > 0) {
            searchSourceBuilder.terminateAfter(terminateAfter);
        }
    }
    StoredFieldsContext storedFieldsContext = StoredFieldsContext.fromRestRequest(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), request);
    if (storedFieldsContext != null) {
        searchSourceBuilder.storedFields(storedFieldsContext);
    }
    String sDocValueFields = request.param("docvalue_fields");
    if (sDocValueFields != null) {
        if (Strings.hasText(sDocValueFields)) {
            String[] sFields = Strings.splitStringByCommaToArray(sDocValueFields);
            for (String field : sFields) {
                searchSourceBuilder.docValueField(field, null);
            }
        }
    }
    FetchSourceContext fetchSourceContext = FetchSourceContext.parseFromRestRequest(request);
    if (fetchSourceContext != null) {
        searchSourceBuilder.fetchSource(fetchSourceContext);
    }
    if (request.hasParam("track_scores")) {
        searchSourceBuilder.trackScores(request.paramAsBoolean("track_scores", false));
    }
    if (request.hasParam("track_total_hits")) {
        if (Booleans.isBoolean(request.param("track_total_hits"))) {
            searchSourceBuilder.trackTotalHits(request.paramAsBoolean("track_total_hits", true));
        } else {
            searchSourceBuilder.trackTotalHitsUpTo(request.paramAsInt("track_total_hits", SearchContext.DEFAULT_TRACK_TOTAL_HITS_UP_TO));
        }
    }
    String sSorts = request.param("sort");
    if (sSorts != null) {
        String[] sorts = Strings.splitStringByCommaToArray(sSorts);
        for (String sort : sorts) {
            int delimiter = sort.lastIndexOf(":");
            if (delimiter != -1) {
                String sortField = sort.substring(0, delimiter);
                String reverse = sort.substring(delimiter + 1);
                if ("asc".equals(reverse)) {
                    searchSourceBuilder.sort(sortField, SortOrder.ASC);
                } else if ("desc".equals(reverse)) {
                    searchSourceBuilder.sort(sortField, SortOrder.DESC);
                }
            } else {
                searchSourceBuilder.sort(sort);
            }
        }
    }
    String sStats = request.param("stats");
    if (sStats != null) {
        searchSourceBuilder.stats(Arrays.asList(Strings.splitStringByCommaToArray(sStats)));
    }
    String suggestField = request.param("suggest_field");
    if (suggestField != null) {
        String suggestText = request.param("suggest_text", request.param("q"));
        int suggestSize = request.paramAsInt("suggest_size", 5);
        String suggestMode = request.param("suggest_mode");
        searchSourceBuilder.suggest(new SuggestBuilder().addSuggestion(suggestField, termSuggestion(suggestField).text(suggestText).size(suggestSize).suggestMode(SuggestMode.resolve(suggestMode))));
    }
}
Also used : StoredFieldsContext(org.opensearch.search.fetch.StoredFieldsContext) SuggestBuilder(org.opensearch.search.suggest.SuggestBuilder) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) QueryBuilder(org.opensearch.index.query.QueryBuilder)

Aggregations

QueryBuilder (org.opensearch.index.query.QueryBuilder)1 StoredFieldsContext (org.opensearch.search.fetch.StoredFieldsContext)1 FetchSourceContext (org.opensearch.search.fetch.subphase.FetchSourceContext)1 SuggestBuilder (org.opensearch.search.suggest.SuggestBuilder)1