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+
);

0 commit comments

Comments
 (0)