You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: api-design-guidelines/data-modelling.md
+12-12
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ JSON objects must be conceptually treated as **unordered**. Maintaining order of
8
8
9
9
If preservation of order is important, such ordering information should be represented separately. The examples below shows a plain (unordered) object alongside three alternative representations which also provide ordering detail.
10
10
11
-
```json
11
+
```yaml
12
12
// Plain object (unordered)
13
13
{
14
14
"one": "eins",
@@ -49,15 +49,15 @@ JSON objects and query string parameters must not use duplicate keys. The behavi
49
49
Nulls are sometimes used to mark a value as "missing" or "not available". Instead of returning `null` as a value in JSON prefer to omit the field in the response. Empty strings (`""`) should similarly not be used to denote a missing string value as they can be confused for an explicit value of the empty string. It is fine to accept `null` in the request if they have special semantics such as unsetting a value. Otherwise fields that would have null or empty string values should also be omitted in the request.
50
50
51
51
- DON'T:
52
-
```json
52
+
```yaml
53
53
{"key1": 1, "key2": null}
54
54
```
55
55
- DON'T:
56
-
```json
56
+
```yaml
57
57
{"key1": 1, "key2": ""}
58
58
```
59
59
- DO:
60
-
```json
60
+
```yaml
61
61
{"key1": 1}
62
62
```
63
63
@@ -107,35 +107,35 @@ Modelling objects that are a mix between static and dynamic keys is more complex
107
107
108
108
An example of mixed static and dynamic keys can be seen in the `indices.field_usage_stats` endpoint response:
109
109
110
-
```json
110
+
```yaml
111
111
{
112
112
"_shards": {
113
113
"total": 1,
114
114
"successful": 1,
115
115
"failed": 0
116
116
},
117
-
"my-index": {...}
117
+
"my-index": {...}
118
118
}
119
119
```
120
120
121
121
The key `"my-index"` is user-defined wheras `"_shards"` is static. This API would be easier to model by keeping all dynamic keys in their own object:
122
122
123
-
```json
123
+
```yaml
124
124
{
125
125
"_shards": {
126
126
"total": 1,
127
127
"successful": 1,
128
128
"failed": 0
129
129
},
130
130
"indices": {
131
-
"my-index": {...}
131
+
"my-index": {...}
132
132
}
133
133
}
134
134
```
135
135
136
136
Or better yet, to completely avoid using dynamic keys the user-defined value can be a property value within the object itself:
137
137
138
-
```json
138
+
```yaml
139
139
{
140
140
"_shards": {
141
141
"total": 1,
@@ -152,7 +152,7 @@ Or better yet, to completely avoid using dynamic keys the user-defined value can
152
152
153
153
Sometimes an API accepts an object but the keys are determined by what "kind/variant" of the object is intended. An example of this is aggregations, queries, and pipeline steps. There are two ways the Elasticsearch API handles this situation. The first method is using an **internal variant type** property like "type" with analyzers:
154
154
155
-
```json
155
+
```yaml
156
156
{
157
157
"type": "snowball",
158
158
"stopwords": ["if", "and", "but"]
@@ -161,7 +161,7 @@ Sometimes an API accepts an object but the keys are determined by what "kind/var
161
161
162
162
The second is using **external variants** where the inner object is wrapped with an object with a single key containing the kind of the inner object. This example changes the analyzer from above to use an external variant:
163
163
164
-
```json
164
+
```yaml
165
165
{
166
166
"snowball": {
167
167
"stopwords": ["if", "and", "but"]
@@ -254,7 +254,7 @@ Many of our APIs support users supplying values in multiple different formats. F
254
254
255
255
For the sake of user experience we should accept values in different formats. For values that accept multiple formats the format should be explicitly set by users so Elasticsearch can interpret and validate the value based on the given format. For example:
Copy file name to clipboardexpand all lines: api-design-guidelines/naming.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ Some programming languages place special meaning on underscore prefixes and suff
54
54
55
55
See the below example with a JSON object with two properties `nodes` and `_nodes`:
56
56
57
-
```json
57
+
```yaml
58
58
{
59
59
"nodes": {...},
60
60
"_nodes": {...}
@@ -63,7 +63,7 @@ See the below example with a JSON object with two properties `nodes` and `_nodes
63
63
64
64
For this API the generated structure in some programming languages would have a clash between the names of the two properties which would require inventing a name for one of the properties. Diversions from the API as it is on the wire are likely to cause confusion for users and should be avoided if possible. Instead these properties could be named more semantically as follows:
Copy file name to clipboardexpand all lines: api-design-guidelines/requests-responses.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -111,7 +111,7 @@ This principle allows for simpler consumer code that neither has to remember sta
111
111
112
112
An example of this is the datafeeds API which accepts either a string or list of strings for indices but always returns a list of strings:
113
113
114
-
```json
114
+
```yaml
115
115
PUT /_ml/datafeeds/feed-id
116
116
{
117
117
"indices": "index-name", // Input is a string.
@@ -139,7 +139,7 @@ The `name` of the function is typically restricted to alphanumeric characters an
139
139
140
140
Functions also only allow a single return / response type which is the "nominal successful response". Error responses can vary based on status code but at a minimum should match the following structure:
0 commit comments