Skip to content

Commit b14e662

Browse files
authored
Backport 4580 to 8.18 (#4613)
* backporting code * generated java examples * rereapply manual fixes * fix tabs * pretty
1 parent 99a9dee commit b14e662

File tree

387 files changed

+4089
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

387 files changed

+4089
-1
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ generate-language-examples:
7474
@node docs/examples/generate-language-examples.js
7575
@npm run format:fix-examples --prefix compiler
7676

77+
generate-language-examples-with-java:
78+
@node docs/examples/generate-language-examples.js java
79+
@npm run format:fix-examples --prefix compiler
80+
7781
lint-docs: ## Lint the OpenAPI documents after overlays
7882
@npx @redocly/cli lint "output/openapi/elasticsearch-*.json" --config "docs/linters/redocly.yaml" --format stylish --max-problems 500
7983

docs/examples/generate-language-examples.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const fs = require('fs');
2121
const path = require('path');
2222
const { parseDocument: yamlParseDocument } = require('yaml');
2323
const { convertRequests, loadSchema } = require('@elastic/request-converter');
24+
const {parseRequest} = require("@elastic/request-converter/dist/parse");
25+
const {JavaCaller} = require("java-caller");
2426

2527
const LANGUAGES = ['Python', 'JavaScript', 'Ruby', 'PHP', 'curl'];
2628

@@ -44,6 +46,50 @@ async function generateLanguages(example) {
4446
});
4547
}
4648
data.alternatives = alternatives.concat((data.alternatives ?? []).filter(pair => !LANGUAGES.includes(pair.language)));
49+
50+
// specific java example generator
51+
if (process.argv[2] === "java") {
52+
const partialRequest = await parseRequest(request);
53+
const java = new JavaCaller({
54+
minimumJavaVersion: 21,
55+
jar: "path/to/converter/jar/java-es-request-converter-1.0-SNAPSHOT.jar",
56+
});
57+
58+
let correctParams = getCodeGenParamNames(partialRequest.params, partialRequest.request);
59+
let body = partialRequest.body;
60+
if (!body) {
61+
body = {}
62+
}
63+
64+
let javaReqs = [];
65+
const javaParsedRequest = {
66+
api: partialRequest.api,
67+
params: correctParams,
68+
query: partialRequest.query,
69+
body: body,
70+
};
71+
javaReqs.push(javaParsedRequest)
72+
73+
let args = [];
74+
args.push(JSON.stringify(javaReqs));
75+
76+
const {status, stdout, stderr} = await java.run(args);
77+
if (status) {
78+
console.log(stderr);
79+
console.log(JSON.stringify(javaReqs));
80+
}
81+
else {
82+
const alternative_java = [];
83+
alternative_java.push({
84+
language: "Java",
85+
code: stdout,
86+
});
87+
// replace old java examples
88+
data.alternatives = data.alternatives.filter(pair => pair.language !== "Java");
89+
data.alternatives = data.alternatives.concat(alternative_java);
90+
}
91+
}
92+
4793
doc.delete('alternatives');
4894
doc.add(doc.createPair('alternatives', data.alternatives));
4995
await fs.promises.writeFile(example, doc.toString({lineWidth: 132}));
@@ -61,6 +107,23 @@ async function* walkExamples(dir) {
61107
}
62108
}
63109

110+
function getCodeGenParamNames(
111+
params,
112+
request,
113+
){
114+
for (const [key, value] of Object.entries(params)) {
115+
if (request?.path) {
116+
for (const prop of request.path) {
117+
if (prop.name === key && prop.codegenName !== undefined) {
118+
delete params[key];
119+
params[prop.codegenName] = value;
120+
}
121+
}
122+
}
123+
}
124+
return params;
125+
}
126+
64127
async function main() {
65128
let count = 0;
66129
let errors = 0;

docs/examples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@elastic/request-converter": "^9.1.1",
77
"@redocly/cli": "^1.34.3",
88
"@stoplight/spectral-cli": "^6.14.2",
9-
"yaml": "^2.8.0"
9+
"yaml": "^2.8.0",
10+
"java-caller": "^4.1.1"
1011
}
1112
}

specification/_global/clear_scroll/examples/request/ClearScrollRequestExample1.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ alternatives:
3535
code:
3636
'curl -X DELETE -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
3737
''{"scroll_id":"DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ=="}'' "$ELASTICSEARCH_URL/_search/scroll"'
38+
- language: Java
39+
code: |
40+
client.clearScroll(c -> c
41+
.scrollId("DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==")
42+
);

specification/_global/close_point_in_time/examples/request/ClosePointInTimeRequestExample1.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ alternatives:
3636
"curl -X DELETE -H \"Authorization: ApiKey $ELASTIC_API_KEY\" -H \"Content-Type: application/json\" -d
3737
'{\"id\":\"46ToAwMDaWR5BXV1aWQyKwZub2RlXzMAAAAAAAAAACoBYwADaWR4BXV1aWQxAgZub2RlXzEAAAAAAAAAAAEBYQADaWR5BXV1aWQyKgZub2RlXzIAAA\
3838
AAAAAAAAwBYgACBXV1aWQyAAAFdXVpZDEAAQltYXRjaF9hbGw_gAAAAA==\"}' \"$ELASTICSEARCH_URL/_pit\""
39+
- language: Java
40+
code: >
41+
client.closePointInTime(c -> c
42+
.id("46ToAwMDaWR5BXV1aWQyKwZub2RlXzMAAAAAAAAAACoBYwADaWR4BXV1aWQxAgZub2RlXzEAAAAAAAAAAAEBYQADaWR5BXV1aWQyKgZub2RlXzIAAAAAAAAAAAwBYgACBXV1aWQyAAAFdXVpZDEAAQltYXRjaF9hbGw_gAAAAA==")
43+
);

specification/_global/count/examples/request/CountRequestExample1.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,14 @@ alternatives:
5959
code:
6060
'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
6161
''{"query":{"term":{"user.id":"kimchy"}}}'' "$ELASTICSEARCH_URL/my-index-000001/_count"'
62+
- language: Java
63+
code: |
64+
client.count(c -> c
65+
.index("my-index-000001")
66+
.query(q -> q
67+
.term(t -> t
68+
.field("user.id")
69+
.value(FieldValue.of("kimchy"))
70+
)
71+
)
72+
);

specification/_global/create/examples/request/CreateRequestExample1.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,10 @@ alternatives:
6969
'curl -X PUT -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
7070
''{"@timestamp":"2099-11-15T13:12:00","message":"GET /search HTTP/1.1 200 1070000","user":{"id":"kimchy"}}''
7171
"$ELASTICSEARCH_URL/my-index-000001/_create/1"'
72+
- language: Java
73+
code: >
74+
client.create(c -> c
75+
.id("1")
76+
.index("my-index-000001")
77+
.document(JsonData.fromJson("{\"@timestamp\":\"2099-11-15T13:12:00\",\"message\":\"GET /search HTTP/1.1 200 1070000\",\"user\":{\"id\":\"kimchy\"}}"))
78+
);

specification/_global/delete_by_query/examples/request/DeleteByQueryRequestExample1.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ alternatives:
4949
code:
5050
'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
5151
''{"query":{"match_all":{}}}'' "$ELASTICSEARCH_URL/my-index-000001,my-index-000002/_delete_by_query"'
52+
- language: Java
53+
code: |
54+
client.deleteByQuery(d -> d
55+
.index(List.of("my-index-000001","my-index-000002"))
56+
.query(q -> q
57+
.matchAll(m -> m)
58+
)
59+
);

specification/_global/delete_by_query/examples/request/DeleteByQueryRequestExample2.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,15 @@ alternatives:
6464
code:
6565
'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
6666
''{"query":{"term":{"user.id":"kimchy"}},"max_docs":1}'' "$ELASTICSEARCH_URL/my-index-000001/_delete_by_query"'
67+
- language: Java
68+
code: |
69+
client.deleteByQuery(d -> d
70+
.index("my-index-000001")
71+
.maxDocs(1L)
72+
.query(q -> q
73+
.term(t -> t
74+
.field("user.id")
75+
.value(FieldValue.of("kimchy"))
76+
)
77+
)
78+
);

specification/_global/delete_by_query/examples/request/DeleteByQueryRequestExample3.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,20 @@ alternatives:
9191
'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
9292
''{"slice":{"id":0,"max":2},"query":{"range":{"http.response.bytes":{"lt":2000000}}}}''
9393
"$ELASTICSEARCH_URL/my-index-000001/_delete_by_query"'
94+
- language: Java
95+
code: |
96+
client.deleteByQuery(d -> d
97+
.index("my-index-000001")
98+
.query(q -> q
99+
.range(r -> r
100+
.untyped(u -> u
101+
.field("http.response.bytes")
102+
.lt(JsonData.fromJson("2000000"))
103+
)
104+
)
105+
)
106+
.slice(s -> s
107+
.id("0")
108+
.max(2)
109+
)
110+
);

specification/_global/delete_by_query/examples/request/DeleteByQueryRequestExample4.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,20 @@ alternatives:
8080
'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
8181
''{"query":{"range":{"http.response.bytes":{"lt":2000000}}}}''
8282
"$ELASTICSEARCH_URL/my-index-000001/_delete_by_query?refresh&slices=5"'
83+
- language: Java
84+
code: |
85+
client.deleteByQuery(d -> d
86+
.index("my-index-000001")
87+
.query(q -> q
88+
.range(r -> r
89+
.untyped(u -> u
90+
.field("http.response.bytes")
91+
.lt(JsonData.fromJson("2000000"))
92+
)
93+
)
94+
)
95+
.refresh(true)
96+
.slices(s -> s
97+
.value(5)
98+
)
99+
);

specification/_global/explain/examples/request/ExplainRequestExample1.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ alternatives:
6363
code:
6464
'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
6565
''{"query":{"match":{"message":"elasticsearch"}}}'' "$ELASTICSEARCH_URL/my-index-000001/_explain/0"'
66+
- language: Java
67+
code: |
68+
client.explain(e -> e
69+
.id("0")
70+
.index("my-index-000001")
71+
.query(q -> q
72+
.match(m -> m
73+
.field("message")
74+
.query(FieldValue.of("elasticsearch"))
75+
)
76+
)
77+
);

specification/_global/get_script_context/examples/request/GetScriptContextRequestExample1.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ alternatives:
1010
code: $resp = $client->getScriptContext();
1111
- language: curl
1212
code: 'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" "$ELASTICSEARCH_URL/_script_context"'
13+
- language: Java
14+
code: |
15+
client.getScriptContext();

specification/_global/get_script_languages/examples/request/GetScriptLanguagesRequestExample1.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ alternatives:
1010
code: $resp = $client->getScriptLanguages();
1111
- language: curl
1212
code: 'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" "$ELASTICSEARCH_URL/_script_language"'
13+
- language: Java
14+
code: |
15+
client.getScriptLanguages();

specification/_global/index/examples/request/IndexRequestExample1.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ alternatives:
6666
'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
6767
''{"@timestamp":"2099-11-15T13:12:00","message":"GET /search HTTP/1.1 200 1070000","user":{"id":"kimchy"}}''
6868
"$ELASTICSEARCH_URL/my-index-000001/_doc/"'
69+
- language: Java
70+
code: >
71+
client.index(i -> i
72+
.index("my-index-000001")
73+
.document(JsonData.fromJson("{\"@timestamp\":\"2099-11-15T13:12:00\",\"message\":\"GET /search HTTP/1.1 200 1070000\",\"user\":{\"id\":\"kimchy\"}}"))
74+
);

specification/_global/index/examples/request/IndexRequestExample2.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,10 @@ alternatives:
6969
'curl -X PUT -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
7070
''{"@timestamp":"2099-11-15T13:12:00","message":"GET /search HTTP/1.1 200 1070000","user":{"id":"kimchy"}}''
7171
"$ELASTICSEARCH_URL/my-index-000001/_doc/1"'
72+
- language: Java
73+
code: >
74+
client.index(i -> i
75+
.id("1")
76+
.index("my-index-000001")
77+
.document(JsonData.fromJson("{\"@timestamp\":\"2099-11-15T13:12:00\",\"message\":\"GET /search HTTP/1.1 200 1070000\",\"user\":{\"id\":\"kimchy\"}}"))
78+
);

specification/_global/info/examples/request/RootNodeInfoRequestExample1.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ alternatives:
1010
code: $resp = $client->info();
1111
- language: curl
1212
code: 'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" "$ELASTICSEARCH_URL/"'
13+
- language: Java
14+
code: |
15+
client.info();

specification/_global/mget/examples/request/MultiGetRequestExample1.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,11 @@ alternatives:
7676
code:
7777
'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
7878
''{"docs":[{"_id":"1"},{"_id":"2"}]}'' "$ELASTICSEARCH_URL/my-index-000001/_mget"'
79+
- language: Java
80+
code: |
81+
client.mget(m -> m
82+
.docs(List.of(MultiGetOperation.of(mu -> mu
83+
.id("1")),MultiGetOperation.of(mu -> mu
84+
.id("2"))))
85+
.index("my-index-000001")
86+
);

specification/_global/mget/examples/request/MultiGetRequestExample3.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,14 @@ alternatives:
110110
"curl -X GET -H \"Authorization: ApiKey $ELASTIC_API_KEY\" -H \"Content-Type: application/json\" -d
111111
'{\"docs\":[{\"_index\":\"test\",\"_id\":\"1\",\"stored_fields\":[\"field1\",\"field2\"]},{\"_index\":\"test\",\"_id\":\"2\",\
112112
\"stored_fields\":[\"field3\",\"field4\"]}]}' \"$ELASTICSEARCH_URL/_mget\""
113+
- language: Java
114+
code: |
115+
client.mget(m -> m
116+
.docs(List.of(MultiGetOperation.of(mu -> mu
117+
.id("1")
118+
.index("test")
119+
.storedFields(List.of("field1","field2"))),MultiGetOperation.of(mu -> mu
120+
.id("2")
121+
.index("test")
122+
.storedFields(List.of("field3","field4")))))
123+
);

specification/_global/mget/examples/request/MultiGetRequestExample4.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,14 @@ alternatives:
9393
'curl -X GET -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
9494
''{"docs":[{"_index":"test","_id":"1","routing":"key2"},{"_index":"test","_id":"2"}]}''
9595
"$ELASTICSEARCH_URL/_mget?routing=key1"'
96+
- language: Java
97+
code: |
98+
client.mget(m -> m
99+
.docs(List.of(MultiGetOperation.of(mu -> mu
100+
.id("1")
101+
.index("test")
102+
.routing("key2")),MultiGetOperation.of(mu -> mu
103+
.id("2")
104+
.index("test"))))
105+
.routing("key1")
106+
);

specification/_global/mtermvectors/examples/request/MultiTermVectorsRequestExample1.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,13 @@ alternatives:
9595
'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
9696
''{"docs":[{"_id":"2","fields":["message"],"term_statistics":true},{"_id":"1"}]}''
9797
"$ELASTICSEARCH_URL/my-index-000001/_mtermvectors"'
98+
- language: Java
99+
code: |
100+
client.mtermvectors(m -> m
101+
.docs(List.of(MultiTermVectorsOperation.of(mu -> mu
102+
.id("2")
103+
.fields("message")
104+
.termStatistics(true)),MultiTermVectorsOperation.of(mu -> mu
105+
.id("1"))))
106+
.index("my-index-000001")
107+
);

specification/_global/mtermvectors/examples/request/MultiTermVectorsRequestExample2.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,9 @@ alternatives:
6969
'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
7070
''{"ids":["1","2"],"fields":["message"],"term_statistics":true}''
7171
"$ELASTICSEARCH_URL/my-index-000001/_mtermvectors"'
72+
- language: Java
73+
code: |
74+
client.mtermvectors(m -> m
75+
.ids(List.of("1","2"))
76+
.index("my-index-000001")
77+
);

specification/_global/mtermvectors/examples/request/MultiTermVectorsRequestExample3.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,12 @@ alternatives:
103103
'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
104104
''{"docs":[{"_index":"my-index-000001","doc":{"message":"test test
105105
test"}},{"_index":"my-index-000001","doc":{"message":"Another test ..."}}]}'' "$ELASTICSEARCH_URL/_mtermvectors"'
106+
- language: Java
107+
code: |
108+
client.mtermvectors(m -> m
109+
.docs(List.of(MultiTermVectorsOperation.of(mu -> mu
110+
.index("my-index-000001")
111+
.doc(JsonData.fromJson("{\"message\":\"test test test\"}"))),MultiTermVectorsOperation.of(mu -> mu
112+
.index("my-index-000001")
113+
.doc(JsonData.fromJson("{\"message\":\"Another test ...\"}")))))
114+
);

specification/_global/open_point_in_time/examples/request/OpenPointInTimeRequestExample1.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ alternatives:
3131
- language: curl
3232
code: 'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY"
3333
"$ELASTICSEARCH_URL/my-index-000001/_pit?keep_alive=1m&allow_partial_search_results=true"'
34+
- language: Java
35+
code: |
36+
client.openPointInTime(o -> o
37+
.allowPartialSearchResults(true)
38+
.index("my-index-000001")
39+
.keepAlive(k -> k
40+
.offset(1)
41+
)
42+
);

specification/_global/put_script/examples/request/PutScriptRequestExample2.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,12 @@ alternatives:
5656
'curl -X PUT -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
5757
''{"script":{"lang":"painless","source":"Math.log(_score * 2) + params[''"''"''my_modifier''"''"'']"}}''
5858
"$ELASTICSEARCH_URL/_scripts/my-stored-script"'
59+
- language: Java
60+
code: |
61+
client.putScript(p -> p
62+
.id("my-stored-script")
63+
.script(s -> s
64+
.lang("painless")
65+
.source("Math.log(_score * 2) + params['my_modifier']")
66+
)
67+
);

0 commit comments

Comments
 (0)