Skip to content

Commit 36ef512

Browse files
committed
Revert "Add documentation for plugin-defined custom variants"
This reverts commit e73d8e3.
1 parent e73d8e3 commit 36ef512

File tree

3 files changed

+3
-161
lines changed

3 files changed

+3
-161
lines changed

docs/api-conventions/variant-types.asciidoc

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -57,50 +57,4 @@ include-tagged::{doc-tests-src}/api_conventions/ApiConventionsTest.java[variant-
5757
<2> Test a larger set of variant kinds.
5858
<3> Get the kind and value held by the variant object.
5959

60-
[discrete]
61-
[[variant-types-custom]]
62-
==== Custom extensions provided by {es} plugins
63-
64-
{es} accepts plugins that can extend the available variants for a number of types. This includes queries, aggregations, text analyzers and tokenizers, ingest processors, etc.
65-
66-
The {java-client} classes for these types accept a `_custom` variant in addition to the builtin ones. This allows you to use these plugin-defined extensions by providing arbitrary JSON in requests, and also receive arbitrary JSON produced by the plugins in responses.
67-
68-
In the examples below we use a hypothetical plugin that adds a `sphere-distance` aggregation that groups documents containing 3D coordinates according to their distance to a reference location.
69-
70-
To create a custom aggregation, use the `_custom()` aggregation type and provide its identifier, defined by the plugin, and parameters. The parameters can be any object or value that can be serialized to JSON. In the example below we use a simple map:
71-
72-
["source","java"]
73-
--------------------------------------------------
74-
include-tagged::{doc-tests-src}/api_conventions/ApiConventionsTest.java[custom-variant-creation]
75-
--------------------------------------------------
76-
<1> Parameters for the custom aggregation.
77-
<2> Create a custom aggregation named `neighbors` of kind `sphere-distance` with its parameters.
78-
79-
The results of custom variants are returned as raw JSON represented by a `JsonData` object. You can then traverse the JSON tree to get the data. Since this is not always convenient, you can also define classes that represent that JSON data and deserialize them from the raw JSON.
80-
81-
Traversing the JSON tree:
82-
83-
["source","java"]
84-
--------------------------------------------------
85-
include-tagged::{doc-tests-src}/api_conventions/ApiConventionsTest.java[custom-variant-navigation-json]
86-
--------------------------------------------------
87-
<1> Use `Void` if you're only interested in aggregation results, not search hits (see also <<aggregations>>).
88-
<2> Get the `neighbors` aggregation result as custom JSON result.
89-
<3> Traverse the JSON tree to extract the result data.
90-
91-
Using a class that represents the custom aggregation results:
92-
93-
["source","java"]
94-
--------------------------------------------------
95-
include-tagged::{doc-tests-src}/api_conventions/ApiConventionsTest.java[custom-variant-navigation-typed]
96-
--------------------------------------------------
97-
<1> Deserialize the custom JSON to a dedicated `SphereDistanceAggregate` class.
98-
99-
Where `SphereDistanceAggregate` can be defined as follows:
100-
["source","java"]
101-
--------------------------------------------------
102-
include-tagged::{doc-tests-src}/api_conventions/ApiConventionsTest.java[custom-variant-types]
103-
--------------------------------------------------
104-
105-
10660
{doc-tests-blurb}

java-client/src/test/java/co/elastic/clients/documentation/DocTestsTransport.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import co.elastic.clients.transport.ElasticsearchTransport;
2525
import co.elastic.clients.transport.Endpoint;
2626
import co.elastic.clients.transport.TransportOptions;
27-
import com.fasterxml.jackson.databind.ObjectMapper;
28-
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
2927

3028
import javax.annotation.Nullable;
3129
import java.io.IOException;
@@ -42,9 +40,7 @@
4240
*/
4341
public class DocTestsTransport implements ElasticsearchTransport {
4442

45-
private final JsonpMapper mapper = new JacksonJsonpMapper(
46-
new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
47-
);
43+
private final JsonpMapper mapper = new JacksonJsonpMapper();
4844

4945
private final ThreadLocal<Object> result = new ThreadLocal<>();
5046

java-client/src/test/java/co/elastic/clients/documentation/api_conventions/ApiConventionsTest.java

Lines changed: 2 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,13 @@
3131
import co.elastic.clients.elasticsearch.indices.Alias;
3232
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
3333
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
34-
import co.elastic.clients.json.JsonData;
34+
import co.elastic.clients.transport.ElasticsearchTransport;
3535
import co.elastic.clients.util.ApiTypeHelper;
36-
import com.fasterxml.jackson.annotation.JsonCreator;
37-
import com.fasterxml.jackson.annotation.JsonProperty;
38-
import com.fasterxml.jackson.databind.ObjectMapper;
39-
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
40-
import jakarta.json.JsonArray;
41-
import jakarta.json.JsonObject;
42-
import jakarta.json.JsonValue;
4336
import org.junit.jupiter.api.Assertions;
4437
import org.junit.jupiter.api.Test;
4538
import org.slf4j.Logger;
4639
import org.slf4j.LoggerFactory;
4740

48-
import java.io.StringReader;
4941
import java.util.Arrays;
5042
import java.util.HashMap;
5143
import java.util.List;
@@ -55,7 +47,7 @@ public class ApiConventionsTest extends Assertions {
5547

5648
private static class SomeApplicationData {}
5749

58-
private DocTestsTransport transport = new DocTestsTransport();
50+
private ElasticsearchTransport transport = new DocTestsTransport();
5951
Logger logger = LoggerFactory.getLogger(this.getClass());
6052

6153
public void blockingAndAsync() throws Exception {
@@ -129,8 +121,6 @@ public void builderLambdasShort() throws Exception {
129121
}
130122

131123
public void builderIntervals() throws Exception {
132-
ObjectMapper mapper = new ObjectMapper();
133-
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
134124
ElasticsearchClient client = new ElasticsearchClient(transport);
135125

136126
//tag::builder-intervals
@@ -203,104 +193,6 @@ public void variantCreation() {
203193
//end::variant-kind
204194
}
205195

206-
//tag::custom-variant-types
207-
public static class SphereDistanceAggregate {
208-
private final List<Bucket> buckets;
209-
@JsonCreator
210-
public SphereDistanceAggregate(
211-
@JsonProperty("buckets") List<Bucket> buckets
212-
) {
213-
this.buckets = buckets;
214-
}
215-
public List<Bucket> buckets() {
216-
return buckets;
217-
};
218-
}
219-
220-
public static class Bucket {
221-
private final double key;
222-
private final double docCount;
223-
@JsonCreator
224-
public Bucket(
225-
@JsonProperty("key") double key,
226-
@JsonProperty("doc_count") double docCount) {
227-
this.key = key;
228-
this.docCount = docCount;
229-
}
230-
public double key() {
231-
return key;
232-
}
233-
public double docCount() {
234-
return docCount;
235-
}
236-
}
237-
//end::custom-variant-types
238-
239-
@Test
240-
public void customVariants() throws Exception {
241-
242-
ElasticsearchClient esClient = new ElasticsearchClient(transport);
243-
244-
String json = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0},\n" +
245-
"\"hits\":{\"total\":{\"relation\":\"eq\",\"value\":0},\"hits\":[]},\n" +
246-
"\"aggregations\":{\"sphere-distance#neighbors\":{\"buckets\":[{\"key\": 1.0,\"doc_count\":1}]}}}";
247-
248-
transport.setResult(SearchResponse.of(b -> b.withJson(
249-
transport.jsonpMapper().jsonProvider().createParser(new StringReader(json)),
250-
transport.jsonpMapper())
251-
));
252-
253-
//tag::custom-variant-creation
254-
Map<String, Object> params = new HashMap<>(); // <1>
255-
params.put("interval", 10);
256-
params.put("scale", "log");
257-
params.put("origin", new Double[]{145.0, 12.5, 1649.0});
258-
259-
SearchRequest request = SearchRequest.of(r -> r
260-
.index("stars")
261-
.aggregations("neighbors", agg -> agg
262-
._custom("sphere-distance", params) // <2>
263-
)
264-
);
265-
//end::custom-variant-creation
266-
267-
{
268-
//tag::custom-variant-navigation-json
269-
SearchResponse<Void> response = esClient.search(request, Void.class); // <1>
270-
271-
JsonData neighbors = response
272-
.aggregations().get("neighbors")
273-
._custom(); // <2>
274-
275-
JsonArray buckets = neighbors.toJson() // <3>
276-
.asJsonObject()
277-
.getJsonArray("buckets");
278-
279-
for (JsonValue item : buckets) {
280-
JsonObject bucket = item.asJsonObject();
281-
double key = bucket.getJsonNumber("key").doubleValue();
282-
double docCount = bucket.getJsonNumber("doc_count").longValue();
283-
doSomething(key, docCount);
284-
}
285-
//end::custom-variant-navigation-json
286-
}
287-
288-
{
289-
//tag::custom-variant-navigation-typed
290-
SearchResponse<Void> response = esClient.search(request, Void.class);
291-
292-
SphereDistanceAggregate neighbors = response
293-
.aggregations().get("neighbors")
294-
._custom()
295-
.to(SphereDistanceAggregate.class); // <1>
296-
297-
for (Bucket bucket : neighbors.buckets()) {
298-
doSomething(bucket.key(), bucket.docCount());
299-
}
300-
//end::custom-variant-navigation-typed
301-
}
302-
}
303-
304196
@Test
305197
public void collections() {
306198
//tag::collections-list

0 commit comments

Comments
 (0)