HLRC - add support for source exists API (#34519)
HLRC - add support for source exists API API re-uses the GetRequest object (following the precedent set by the plain “exists” api). Relates to #27205
This commit is contained in:
parent
306f1d78f8
commit
3181083781
|
@ -261,6 +261,18 @@ final class RequestConverters {
|
|||
|
||||
return request;
|
||||
}
|
||||
|
||||
static Request sourceExists(GetRequest getRequest) {
|
||||
Request request = new Request(HttpHead.METHOD_NAME, endpoint(getRequest.index(), getRequest.type(), getRequest.id(), "_source"));
|
||||
|
||||
Params parameters = new Params(request);
|
||||
parameters.withPreference(getRequest.preference());
|
||||
parameters.withRouting(getRequest.routing());
|
||||
parameters.withRefresh(getRequest.refresh());
|
||||
parameters.withRealtime(getRequest.realtime());
|
||||
// Version params are not currently supported by the source exists API so are not passed
|
||||
return request;
|
||||
}
|
||||
|
||||
static Request multiGet(MultiGetRequest multiGetRequest) throws IOException {
|
||||
Request request = new Request(HttpPost.METHOD_NAME, "/_mget");
|
||||
|
|
|
@ -727,6 +727,32 @@ public class RestHighLevelClient implements Closeable {
|
|||
emptySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for the existence of a document with a "_source" field. Returns true if it exists, false otherwise.
|
||||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Source exists API
|
||||
* on elastic.co</a>
|
||||
* @param getRequest the request
|
||||
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
|
||||
* @return <code>true</code> if the document and _source field exists, <code>false</code> otherwise
|
||||
* @throws IOException in case there is a problem sending the request
|
||||
*/
|
||||
public boolean existsSource(GetRequest getRequest, RequestOptions options) throws IOException {
|
||||
return performRequest(getRequest, RequestConverters::sourceExists, options, RestHighLevelClient::convertExistsResponse, emptySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously checks for the existence of a document with a "_source" field. Returns true if it exists, false otherwise.
|
||||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Source exists API
|
||||
* on elastic.co</a>
|
||||
* @param getRequest the request
|
||||
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
|
||||
* @param listener the listener to be notified upon request completion
|
||||
*/
|
||||
public final void existsSourceAsync(GetRequest getRequest, RequestOptions options, ActionListener<Boolean> listener) {
|
||||
performRequestAsync(getRequest, RequestConverters::sourceExists, options, RestHighLevelClient::convertExistsResponse, listener,
|
||||
emptySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Index a document using the Index API.
|
||||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html">Index API on elastic.co</a>
|
||||
|
|
|
@ -194,6 +194,61 @@ public class CrudIT extends ESRestHighLevelClientTestCase {
|
|||
assertFalse(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
|
||||
}
|
||||
}
|
||||
|
||||
public void testSourceExists() throws IOException {
|
||||
{
|
||||
GetRequest getRequest = new GetRequest("index", "type", "id");
|
||||
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
|
||||
}
|
||||
IndexRequest index = new IndexRequest("index", "type", "id");
|
||||
index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON);
|
||||
index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
|
||||
highLevelClient().index(index, RequestOptions.DEFAULT);
|
||||
{
|
||||
GetRequest getRequest = new GetRequest("index", "type", "id");
|
||||
assertTrue(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
|
||||
}
|
||||
{
|
||||
GetRequest getRequest = new GetRequest("index", "type", "does_not_exist");
|
||||
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
|
||||
}
|
||||
{
|
||||
GetRequest getRequest = new GetRequest("index", "type", "does_not_exist").version(1);
|
||||
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
|
||||
}
|
||||
}
|
||||
|
||||
public void testSourceDoesNotExist() throws IOException {
|
||||
final String noSourceIndex = "no_source";
|
||||
{
|
||||
// Prepare
|
||||
Settings settings = Settings.builder()
|
||||
.put("number_of_shards", 1)
|
||||
.put("number_of_replicas", 0)
|
||||
.build();
|
||||
String mapping = "\"_doc\": { \"_source\": {\n" +
|
||||
" \"enabled\": false\n" +
|
||||
" } }";
|
||||
createIndex(noSourceIndex, settings, mapping);
|
||||
assertEquals(
|
||||
RestStatus.OK,
|
||||
highLevelClient().bulk(
|
||||
new BulkRequest()
|
||||
.add(new IndexRequest(noSourceIndex, "_doc", "1")
|
||||
.source(Collections.singletonMap("foo", 1), XContentType.JSON))
|
||||
.add(new IndexRequest(noSourceIndex, "_doc", "2")
|
||||
.source(Collections.singletonMap("foo", 2), XContentType.JSON))
|
||||
.setRefreshPolicy(RefreshPolicy.IMMEDIATE),
|
||||
RequestOptions.DEFAULT
|
||||
).status()
|
||||
);
|
||||
}
|
||||
{
|
||||
GetRequest getRequest = new GetRequest(noSourceIndex, "_doc", "1");
|
||||
assertTrue(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
|
||||
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
|
||||
}
|
||||
}
|
||||
|
||||
public void testGet() throws IOException {
|
||||
{
|
||||
|
|
|
@ -650,7 +650,6 @@ public class RestHighLevelClientTests extends ESTestCase {
|
|||
"cluster.remote_info",
|
||||
"count",
|
||||
"create",
|
||||
"exists_source",
|
||||
"get_source",
|
||||
"indices.delete_alias",
|
||||
"indices.delete_template",
|
||||
|
|
|
@ -29,3 +29,10 @@ include-tagged::{doc-tests-file}[{api}-request]
|
|||
<5> Disable fetching stored fields.
|
||||
|
||||
include::../execution.asciidoc[]
|
||||
|
||||
|
||||
==== Source exists request
|
||||
A variant of the exists request is `existsSource` method which has the additional check
|
||||
that the document in question has stored the `source`. If the mapping for the index has opted
|
||||
to remove support for storing JSON source in documents then this method will return false
|
||||
for documents in this index.
|
||||
|
|
Loading…
Reference in New Issue