use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.
the class RestMultiGetAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
MultiGetRequest multiGetRequest = new MultiGetRequest();
multiGetRequest.refresh(request.paramAsBoolean("refresh", multiGetRequest.refresh()));
multiGetRequest.preference(request.param("preference"));
multiGetRequest.realtime(request.paramAsBoolean("realtime", multiGetRequest.realtime()));
if (request.param("fields") != null) {
throw new IllegalArgumentException("The parameter [fields] is no longer supported, " + "please use [stored_fields] to retrieve stored fields or _source filtering if the field is not stored");
}
String[] sFields = null;
String sField = request.param("stored_fields");
if (sField != null) {
sFields = Strings.splitStringByCommaToArray(sField);
}
FetchSourceContext defaultFetchSource = FetchSourceContext.parseFromRestRequest(request);
try (XContentParser parser = request.contentOrSourceParamParser()) {
multiGetRequest.add(request.param("index"), request.param("type"), sFields, defaultFetchSource, request.param("routing"), parser, allowExplicitIndex);
}
return channel -> client.multiGet(multiGetRequest, new RestToXContentListener<>(channel));
}
use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.
the class RequestTests method randomizeFetchSourceContextParams.
/**
* Randomize the {@link FetchSourceContext} request parameters.
*/
private static void randomizeFetchSourceContextParams(Consumer<FetchSourceContext> consumer, Map<String, String> expectedParams) {
if (randomBoolean()) {
if (randomBoolean()) {
boolean fetchSource = randomBoolean();
consumer.accept(new FetchSourceContext(fetchSource));
if (fetchSource == false) {
expectedParams.put("_source", "false");
}
} else {
int numIncludes = randomIntBetween(0, 5);
String[] includes = new String[numIncludes];
StringBuilder includesParam = new StringBuilder();
for (int i = 0; i < numIncludes; i++) {
String include = randomAsciiOfLengthBetween(3, 10);
includes[i] = include;
includesParam.append(include);
if (i < numIncludes - 1) {
includesParam.append(",");
}
}
if (numIncludes > 0) {
expectedParams.put("_source_include", includesParam.toString());
}
int numExcludes = randomIntBetween(0, 5);
String[] excludes = new String[numExcludes];
StringBuilder excludesParam = new StringBuilder();
for (int i = 0; i < numExcludes; i++) {
String exclude = randomAsciiOfLengthBetween(3, 10);
excludes[i] = exclude;
excludesParam.append(exclude);
if (i < numExcludes - 1) {
excludesParam.append(",");
}
}
if (numExcludes > 0) {
expectedParams.put("_source_exclude", excludesParam.toString());
}
consumer.accept(new FetchSourceContext(true, includes, excludes));
}
}
}
use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project elasticsearch by elastic.
the class ExplainRequestTests method testSerialize.
public void testSerialize() throws IOException {
try (BytesStreamOutput output = new BytesStreamOutput()) {
ExplainRequest request = new ExplainRequest("index", "type", "id");
request.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1.*" }, new String[] { "field2.*" }));
request.filteringAlias(new AliasFilter(QueryBuilders.termQuery("filter_field", "value"), new String[] { "alias0", "alias1" }));
request.preference("the_preference");
request.query(QueryBuilders.termQuery("field", "value"));
request.storedFields(new String[] { "field1", "field2" });
request.routing("some_routing");
request.writeTo(output);
try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) {
ExplainRequest readRequest = new ExplainRequest();
readRequest.readFrom(in);
assertEquals(request.filteringAlias(), readRequest.filteringAlias());
assertArrayEquals(request.storedFields(), readRequest.storedFields());
assertEquals(request.preference(), readRequest.preference());
assertEquals(request.query(), readRequest.query());
assertEquals(request.routing(), readRequest.routing());
assertEquals(request.fetchSourceContext(), readRequest.fetchSourceContext());
}
}
}
use of org.elasticsearch.search.fetch.subphase.FetchSourceContext in project gora by apache.
the class ElasticsearchStore method exists.
@Override
public boolean exists(K key) throws GoraException {
GetRequest getRequest = new GetRequest(elasticsearchMapping.getIndexName(), (String) key);
getRequest.fetchSourceContext(new FetchSourceContext(false)).storedFields("_none_");
try {
return client.exists(getRequest, RequestOptions.DEFAULT);
} catch (IOException ex) {
throw new GoraException(ex);
}
}
Aggregations