Search in sources :

Example 21 with FetchSourceContext

use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.

the class UpdateRequest method fetchSource.

/**
     * Indicate that _source should be returned, with an
     * "include" and/or "exclude" set which can include simple wildcard
     * elements.
     *
     * @param includes
     *            An optional list of include (optionally wildcarded) pattern to
     *            filter the returned _source
     * @param excludes
     *            An optional list of exclude (optionally wildcarded) pattern to
     *            filter the returned _source
     */
public UpdateRequest fetchSource(@Nullable String[] includes, @Nullable String[] excludes) {
    FetchSourceContext context = this.fetchSourceContext == null ? FetchSourceContext.FETCH_SOURCE : this.fetchSourceContext;
    this.fetchSourceContext = new FetchSourceContext(context.fetchSource(), includes, excludes);
    return this;
}
Also used : FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext)

Example 22 with FetchSourceContext

use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.

the class TopHitsAggregationBuilder method fetchSource.

/**
     * Indicate that _source should be returned with every hit, with an
     * "include" and/or "exclude" set which can include simple wildcard
     * elements.
     *
     * @param includes
     *            An optional list of include (optionally wildcarded)
     *            pattern to filter the returned _source
     * @param excludes
     *            An optional list of exclude (optionally wildcarded)
     *            pattern to filter the returned _source
     */
public TopHitsAggregationBuilder fetchSource(@Nullable String[] includes, @Nullable String[] excludes) {
    FetchSourceContext fetchSourceContext = this.fetchSourceContext != null ? this.fetchSourceContext : FetchSourceContext.FETCH_SOURCE;
    this.fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes, excludes);
    return this;
}
Also used : FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext)

Example 23 with FetchSourceContext

use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.

the class InnerHitBuilderTests method randomInnerHits.

public static InnerHitBuilder randomInnerHits(boolean recursive, boolean includeQueryTypeOrPath) {
    InnerHitBuilder innerHits = new InnerHitBuilder();
    innerHits.setName(randomAsciiOfLengthBetween(1, 16));
    innerHits.setFrom(randomIntBetween(0, 128));
    innerHits.setSize(randomIntBetween(0, 128));
    innerHits.setExplain(randomBoolean());
    innerHits.setVersion(randomBoolean());
    innerHits.setTrackScores(randomBoolean());
    if (randomBoolean()) {
        innerHits.setStoredFieldNames(randomListStuff(16, () -> randomAsciiOfLengthBetween(1, 16)));
    }
    innerHits.setDocValueFields(randomListStuff(16, () -> randomAsciiOfLengthBetween(1, 16)));
    // 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;
    if (randomBoolean()) {
        randomFetchSourceContext = new FetchSourceContext(randomBoolean());
    } else {
        randomFetchSourceContext = new FetchSourceContext(true, generateRandomStringArray(12, 16, false), generateRandomStringArray(12, 16, false));
    }
    innerHits.setFetchSourceContext(randomFetchSourceContext);
    if (randomBoolean()) {
        innerHits.setSorts(randomListStuff(16, () -> SortBuilders.fieldSort(randomAsciiOfLengthBetween(5, 20)).order(randomFrom(SortOrder.values()))));
    }
    innerHits.setHighlightBuilder(HighlightBuilderTests.randomHighlighterBuilder());
    if (recursive && randomBoolean()) {
        int size = randomIntBetween(1, 16);
        for (int i = 0; i < size; i++) {
            innerHits.addChildInnerHit(randomInnerHits(false, includeQueryTypeOrPath));
        }
    }
    if (includeQueryTypeOrPath) {
        QueryBuilder query = new MatchQueryBuilder(randomAsciiOfLengthBetween(1, 16), randomAsciiOfLengthBetween(1, 16));
        if (randomBoolean()) {
            return new InnerHitBuilder(innerHits, randomAsciiOfLength(8), query, randomBoolean());
        } else {
            return new InnerHitBuilder(innerHits, query, randomAsciiOfLength(8), randomBoolean());
        }
    } else {
        return innerHits;
    }
}
Also used : HashMap(java.util.HashMap) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext)

Example 24 with FetchSourceContext

use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.

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.setTrackScores(!copy.isTrackScores()));
    modifiers.add(() -> copy.setName(randomValueOtherThan(copy.getName(), () -> randomAsciiOfLengthBetween(1, 16))));
    modifiers.add(() -> {
        if (randomBoolean()) {
            copy.setDocValueFields(randomValueOtherThan(copy.getDocValueFields(), () -> {
                return randomListStuff(16, () -> randomAsciiOfLengthBetween(1, 16));
            }));
        } else {
            copy.addDocValueField(randomAsciiOfLengthBetween(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(randomAsciiOfLengthBetween(5, 20)).order(randomFrom(SortOrder.values())));
                return builders;
            });
            copy.setSorts(sortBuilders);
        } else {
            copy.addSort(SortBuilders.fieldSort(randomAsciiOfLengthBetween(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, () -> randomAsciiOfLengthBetween(1, 16)));
            copy.setStoredFieldNames(newValues);
        } else {
            copy.getStoredFieldsContext().addFieldName(randomAsciiOfLengthBetween(1, 16));
        }
    });
    randomFrom(modifiers).run();
    return copy;
}
Also used : SortBuilder(org.elasticsearch.search.sort.SortBuilder) ArrayList(java.util.ArrayList) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) HighlightBuilderTests(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilderTests) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext)

Example 25 with FetchSourceContext

use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.

the class RestBulkAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    BulkRequest bulkRequest = Requests.bulkRequest();
    String defaultIndex = request.param("index");
    String defaultType = request.param("type");
    String defaultRouting = request.param("routing");
    FetchSourceContext defaultFetchSourceContext = FetchSourceContext.parseFromRestRequest(request);
    String fieldsParam = request.param("fields");
    if (fieldsParam != null) {
        DEPRECATION_LOGGER.deprecated("Deprecated field [fields] used, expected [_source] instead");
    }
    String[] defaultFields = fieldsParam != null ? Strings.commaDelimitedListToStringArray(fieldsParam) : null;
    String defaultPipeline = request.param("pipeline");
    String waitForActiveShards = request.param("wait_for_active_shards");
    if (waitForActiveShards != null) {
        bulkRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards));
    }
    bulkRequest.timeout(request.paramAsTime("timeout", BulkShardRequest.DEFAULT_TIMEOUT));
    bulkRequest.setRefreshPolicy(request.param("refresh"));
    bulkRequest.add(request.content(), defaultIndex, defaultType, defaultRouting, defaultFields, defaultFetchSourceContext, defaultPipeline, null, allowExplicitIndex, request.getXContentType());
    return channel -> client.bulk(bulkRequest, new RestStatusToXContentListener<>(channel));
}
Also used : Loggers(org.elasticsearch.common.logging.Loggers) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) DeprecationLogger(org.elasticsearch.common.logging.DeprecationLogger) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) PUT(org.elasticsearch.rest.RestRequest.Method.PUT) Strings(org.elasticsearch.common.Strings) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) BulkShardRequest(org.elasticsearch.action.bulk.BulkShardRequest) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Requests(org.elasticsearch.client.Requests) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) BulkRequest(org.elasticsearch.action.bulk.BulkRequest)

Aggregations

FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)29 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 XContentParser (org.elasticsearch.common.xcontent.XContentParser)4 NodeClient (org.elasticsearch.client.node.NodeClient)3 Strings (org.elasticsearch.common.Strings)3 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)3 StreamInput (org.elasticsearch.common.io.stream.StreamInput)3 Settings (org.elasticsearch.common.settings.Settings)3 VersionType (org.elasticsearch.index.VersionType)3 Map (java.util.Map)2 ExplainRequest (org.elasticsearch.action.explain.ExplainRequest)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 ActiveShardCount (org.elasticsearch.action.support.ActiveShardCount)2 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)2 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)2 DeprecationLogger (org.elasticsearch.common.logging.DeprecationLogger)2 Loggers (org.elasticsearch.common.logging.Loggers)2 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)2