From 67d5f70f09ea1037d3916655acbbeeac760e4918 Mon Sep 17 00:00:00 2001 From: markharwood Date: Tue, 16 Oct 2018 13:56:19 +0100 Subject: [PATCH 1/4] =?UTF-8?q?HLRC=20-=20add=20support=20for=20source=20e?= =?UTF-8?q?xists=20API=20API=20re-uses=20the=20GetRequest=20object=20(foll?= =?UTF-8?q?owing=20the=20precedent=20set=20by=20the=20plain=20=E2=80=9Cexi?= =?UTF-8?q?sts=E2=80=9D=20api).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Relates to #33686 --- .../client/RequestConverters.java | 12 ++++ .../client/RestHighLevelClient.java | 26 +++++++++ .../java/org/elasticsearch/client/CrudIT.java | 55 +++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index 106caea027e27..2ff944b0a5343 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -261,6 +261,18 @@ private static Request getStyleRequest(String method, GetRequest getRequest) { 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"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java index 342e3efbb6a35..daa3ba5670666 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java @@ -727,6 +727,32 @@ public final void existsAsync(GetRequest getRequest, RequestOptions options, Act emptySet()); } + /** + * Checks for the existence of a document with a _source field. Returns true if it exists, false otherwise. + * See Source exists API + * on elastic.co + * @param getRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return true if the document and _source field exists, false otherwise + * @throws IOException in case there is a problem sending the request + */ + public boolean sourceExists(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 Source exists API + * on elastic.co + * @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 sourceExistsAsync(GetRequest getRequest, RequestOptions options, ActionListener listener) { + performRequestAsync(getRequest, RequestConverters::sourceExists, options, RestHighLevelClient::convertExistsResponse, listener, + emptySet()); + } + /** * Index a document using the Index API. * See Index API on elastic.co diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java index e679a85f67f0c..b06e71268ffe9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java @@ -194,6 +194,61 @@ public void testExists() throws IOException { assertFalse(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync)); } } + + public void testSourceExists() throws IOException { + { + GetRequest getRequest = new GetRequest("index", "type", "id"); + assertFalse(execute(getRequest, highLevelClient()::sourceExists, highLevelClient()::sourceExistsAsync)); + } + 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()::sourceExists, highLevelClient()::sourceExistsAsync)); + } + { + GetRequest getRequest = new GetRequest("index", "type", "does_not_exist"); + assertFalse(execute(getRequest, highLevelClient()::sourceExists, highLevelClient()::sourceExistsAsync)); + } + { + GetRequest getRequest = new GetRequest("index", "type", "does_not_exist").version(1); + assertFalse(execute(getRequest, highLevelClient()::sourceExists, highLevelClient()::sourceExistsAsync)); + } + } + + 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()::sourceExists, highLevelClient()::sourceExistsAsync)); + } + } public void testGet() throws IOException { { From 347b89c3d4dd9692a60a1b4e29cf8b341f2e51c2 Mon Sep 17 00:00:00 2001 From: markharwood Date: Tue, 16 Oct 2018 16:40:27 +0100 Subject: [PATCH 2/4] Naming convention fix --- .../org/elasticsearch/client/RestHighLevelClient.java | 4 ++-- .../src/test/java/org/elasticsearch/client/CrudIT.java | 10 +++++----- .../elasticsearch/client/RestHighLevelClientTests.java | 1 - 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java index daa3ba5670666..fa184ef06852c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java @@ -736,7 +736,7 @@ public final void existsAsync(GetRequest getRequest, RequestOptions options, Act * @return true if the document and _source field exists, false otherwise * @throws IOException in case there is a problem sending the request */ - public boolean sourceExists(GetRequest getRequest, RequestOptions options) throws IOException { + public boolean existsSource(GetRequest getRequest, RequestOptions options) throws IOException { return performRequest(getRequest, RequestConverters::sourceExists, options, RestHighLevelClient::convertExistsResponse, emptySet()); } @@ -748,7 +748,7 @@ public boolean sourceExists(GetRequest getRequest, RequestOptions options) throw * @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 sourceExistsAsync(GetRequest getRequest, RequestOptions options, ActionListener listener) { + public final void existsSourceAsync(GetRequest getRequest, RequestOptions options, ActionListener listener) { performRequestAsync(getRequest, RequestConverters::sourceExists, options, RestHighLevelClient::convertExistsResponse, listener, emptySet()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java index b06e71268ffe9..1dd27cff0d92a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java @@ -198,7 +198,7 @@ public void testExists() throws IOException { public void testSourceExists() throws IOException { { GetRequest getRequest = new GetRequest("index", "type", "id"); - assertFalse(execute(getRequest, highLevelClient()::sourceExists, highLevelClient()::sourceExistsAsync)); + assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync)); } IndexRequest index = new IndexRequest("index", "type", "id"); index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON); @@ -206,15 +206,15 @@ public void testSourceExists() throws IOException { highLevelClient().index(index, RequestOptions.DEFAULT); { GetRequest getRequest = new GetRequest("index", "type", "id"); - assertTrue(execute(getRequest, highLevelClient()::sourceExists, highLevelClient()::sourceExistsAsync)); + assertTrue(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync)); } { GetRequest getRequest = new GetRequest("index", "type", "does_not_exist"); - assertFalse(execute(getRequest, highLevelClient()::sourceExists, highLevelClient()::sourceExistsAsync)); + assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync)); } { GetRequest getRequest = new GetRequest("index", "type", "does_not_exist").version(1); - assertFalse(execute(getRequest, highLevelClient()::sourceExists, highLevelClient()::sourceExistsAsync)); + assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync)); } } @@ -246,7 +246,7 @@ public void testSourceDoesNotExist() throws IOException { { GetRequest getRequest = new GetRequest(noSourceIndex, "_doc", "1"); assertTrue(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync)); - assertFalse(execute(getRequest, highLevelClient()::sourceExists, highLevelClient()::sourceExistsAsync)); + assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync)); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java index 8f4ec4cc0ccca..d40c3196e54f4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java @@ -650,7 +650,6 @@ public void testApiNamingConventions() throws Exception { "cluster.remote_info", "count", "create", - "exists_source", "get_source", "indices.delete_alias", "indices.delete_template", From 6ff188f9b8bbb03d3c242871bf92808c5f611b67 Mon Sep 17 00:00:00 2001 From: markharwood Date: Wed, 24 Oct 2018 12:28:31 +0100 Subject: [PATCH 3/4] =?UTF-8?q?Added=20reference=20in=20the=20=E2=80=9Cexi?= =?UTF-8?q?sts=E2=80=9D=20api=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/java-rest/high-level/document/exists.asciidoc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/java-rest/high-level/document/exists.asciidoc b/docs/java-rest/high-level/document/exists.asciidoc index ac6968d1f3752..3a09203bab6c6 100644 --- a/docs/java-rest/high-level/document/exists.asciidoc +++ b/docs/java-rest/high-level/document/exists.asciidoc @@ -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. From bb6da034a50f7043ee0af038d381c8cf0b92db4c Mon Sep 17 00:00:00 2001 From: markharwood Date: Fri, 26 Oct 2018 09:34:15 +0100 Subject: [PATCH 4/4] =?UTF-8?q?Fixed=20wrong=20url=20and=20added=20quotes?= =?UTF-8?q?=20around=20=E2=80=9C=5Fsource=E2=80=9D=20in=20javadocs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/elasticsearch/client/RestHighLevelClient.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java index fa184ef06852c..7e8a965361426 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java @@ -728,8 +728,8 @@ public final void existsAsync(GetRequest getRequest, RequestOptions options, Act } /** - * Checks for the existence of a document with a _source field. Returns true if it exists, false otherwise. - * See Source exists API + * Checks for the existence of a document with a "_source" field. Returns true if it exists, false otherwise. + * See Source exists API * on elastic.co * @param getRequest the request * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized @@ -741,8 +741,8 @@ public boolean existsSource(GetRequest getRequest, RequestOptions options) throw } /** - * Asynchronously checks for the existence of a document with a _source field. Returns true if it exists, false otherwise. - * See Source exists API + * Asynchronously checks for the existence of a document with a "_source" field. Returns true if it exists, false otherwise. + * See Source exists API * on elastic.co * @param getRequest the request * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized