Skip to content

Commit 973939e

Browse files
committed
edit
1 parent 766c5a8 commit 973939e

12 files changed

+119
-160
lines changed

troubleshoot/elasticsearch/painless-array-list-manipulation-errors.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ products:
77
- id: elasticsearch
88
---
99

10-
# Troubleshoot array/list manipulation errors in Painless
10+
# Troubleshoot array manipulation errors in Painless
1111

12-
Follow these guidelines to avoid array access errors in your Painless script.
12+
Follow these guidelines to avoid array (list) access errors in your Painless scripts.
1313

14-
## Array index\_out\_of\_bounds\_exception
14+
An array `index_out_of_bounds_exception` occurs when a script tries to access an element at a position that does not exist in the array. For example, if an array has two elements, trying to access a third element triggers this exception.
1515

16-
Array index out of bounds exceptions occur when a script tries to access an element at a position that does not exist in the array. For example, if an array has two elements, trying to access a third element will trigger this exception.
17-
18-
### Sample error
16+
## Sample error
1917

2018
```json
2119
{
@@ -71,7 +69,7 @@ Array index out of bounds exceptions occur when a script tries to access an elem
7169
}
7270
```
7371

74-
### Problematic code
72+
## Problematic code
7573

7674
```json
7775
{
@@ -92,13 +90,13 @@ Array index out of bounds exceptions occur when a script tries to access an elem
9290
}
9391
```
9492

95-
### Root cause
93+
## Root cause
9694

9795
The error occurs because the script tries to access index 2 (the third element) in an array that only has two elements (indices 0, 1). Arrays in Painless are zero-indexed, so accessing an index greater than or equal to the array size causes an exception.
9896

99-
### Solution: Check array bounds before accessing
97+
## Solution: Check array bounds before accessing
10098

101-
Always verify array size before accessing specific indices:
99+
Always verify the size of an array before accessing specific indices:
102100

103101
```json
104102
GET blog_posts/_search
@@ -125,7 +123,7 @@ GET blog_posts/_search
125123
}
126124
```
127125

128-
### Sample document
126+
## Sample document
129127

130128
```json
131129
POST blog_posts/_doc
@@ -136,7 +134,7 @@ POST blog_posts/_doc
136134
}
137135
```
138136

139-
### Results
137+
## Results
140138

141139
```json
142140
{
@@ -159,8 +157,8 @@ POST blog_posts/_doc
159157
}
160158
```
161159

162-
### Notes
160+
## Notes
163161

164-
* **Array bounds:** Always check array size before accessing specific indices.
165-
* **Zero-indexed:** Remember that arrays start at index 0, so size() \- 1 is the last valid index.
166-
* **Empty arrays:** Handle cases where arrays might be completely empty (size() \== 0).
162+
* **Array bounds:** Always check the size of an array before accessing specific indices.
163+
* **Zero-indexed:** Remember that arrays start at index 0, so `size() - 1` is the last valid index.
164+
* **Empty arrays:** Handle cases where arrays might be completely empty (`size() == 0`).

troubleshoot/elasticsearch/painless-date-math-errors.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ products:
99

1010
# Troubleshoot date math errors in Painless
1111

12-
Follow these guidelines to avoid [date](elasticsearch://reference/scripting-languages/painless/using-datetime-in-painless.md) operation errors in your Painless script.
12+
Follow these guidelines to avoid [date](elasticsearch://reference/scripting-languages/painless/using-datetime-in-painless.md) operation errors in your Painless scripts.
1313

14-
## Runtime mappings context
14+
When you work with date fields in runtime mappings, accessing methods directly on the document field object can cause errors if the proper value accessor is not used.
1515

16-
### Dynamic method not found error
17-
18-
When working with date fields in runtime mappings, accessing methods directly on the document field object can cause errors if the proper value accessor is not used.
19-
20-
### Error
16+
## Error
2117

2218
```json
2319
{
@@ -76,7 +72,7 @@ When working with date fields in runtime mappings, accessing methods directly on
7672
}
7773
```
7874

79-
### Problematic code
75+
## Problematic code
8076

8177
```json
8278
"script": {
@@ -88,25 +84,25 @@ When working with date fields in runtime mappings, accessing methods directly on
8884
}
8985
```
9086

91-
### Root cause
87+
## Root cause
9288

9389
The script attempts to call `toInstant()` directly on a `ScriptDocValues.Dates` object. Date fields in Painless require accessing the `.value` property to get the actual date value before calling date methods.
9490

95-
### Solution
91+
## Solution
9692

9793
Access the date value using `.value` before calling date methods:
9894

9995
```json
10096
"script": {
10197
"lang": "painless",
10298
"source": """
103-
def orderDate = doc['order_date'].value; // added .value here
99+
def orderDate = doc['order_date'].value; // Appended `.value` to the method.
104100
emit(orderDate.toInstant().toEpochMilli() + 14400000);
105101
"""
106102
}
107103
```
108104

109-
### Notes
105+
## Notes
110106

111107
* Always use `.value` when accessing single values from document fields in Painless.
112108
* Check for empty fields when the field might not exist in all documents.

troubleshoot/elasticsearch/painless-field-not-found.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
---
2-
navigation_title: Field not found (mapping conflicts)
2+
navigation_title: Field not found errors
33
applies_to:
44
stack: ga
55
serverless: ga
66
products:
77
- id: elasticsearch
88
---
99

10-
# Troubleshoot field not found (mapping conflicts) in Painless
10+
# Troubleshoot field not found errors in Painless
1111

12-
Follow these guidelines to avoid field access errors in your Painless script.
12+
Follow these guidelines to avoid field access errors in your Painless scripts.
1313

14-
## A document doesn't have a value for a field
14+
When you work with document fields, attempting to access fields that don't exist in all documents or aren't properly mapped leads to field not found exceptions, causing script failures.
1515

16-
When working with document fields, attempting to access fields that don't exist in all documents or aren't properly mapped leads to field not found exceptions, causing script failures.
17-
18-
### Sample error
16+
## Sample error
1917

2018
```json
2119
{
@@ -78,7 +76,7 @@ When working with document fields, attempting to access fields that don't exist
7876
}
7977
```
8078

81-
### Problematic code
79+
## Problematic code
8280

8381
```json
8482
{
@@ -98,13 +96,13 @@ When working with document fields, attempting to access fields that don't exist
9896
}
9997
```
10098

101-
### Root cause
99+
## Root cause
102100

103101
A field not found exception occurs when a script tries to access a field that is not defined in the index mappings. If the field is defined in the mappings but has no value in some documents, the script will not fail as long as you first check whether the field has values.
104102

105-
For example, calling `doc['author_score'].value` directly on a document without that field will cause an error. The recommended approach is to use `doc[<field>].size()==0` to check if the field is missing in a document before accessing its value.
103+
For example, calling `doc['author_score'].value` directly on a document that does not contain that field causes an error. The recommended approach is to use `doc[<field>].size()==0` to check if the field is missing in a document before accessing its value.
106104

107-
### Sample documents
105+
## Sample documents
108106

109107
```json
110108
POST articles/_doc
@@ -123,9 +121,9 @@ POST articles/_doc
123121
}
124122
```
125123

126-
### Solution 1: Check field existence before accessing
124+
## Solution 1: Check field existence before accessing
127125

128-
Always verify field existence using `size()` before accessing field values:
126+
Always verify the existence of a field by using `size()` before accessing field values:
129127

130128
```json
131129
GET articles/_search
@@ -152,9 +150,9 @@ GET articles/_search
152150
}
153151
```
154152

155-
### Solution 2: New field API approach
153+
## Solution 2: New field API approach
156154

157-
The [field API](/explore-analyze/scripting/script-fields-api.md) provides a more elegant solution that handles missing values automatically by allowing you to specify default values. This approach is more concise and eliminates the need for explicit field existence checks:
155+
The [field API](/explore-analyze/scripting/script-fields-api.md) provides a more elegant solution that handles missing values automatically, by allowing you to specify default values. This approach is more concise and eliminates the need for explicit field existence checks:
158156

159157
```json
160158
GET articles/_search
@@ -178,9 +176,9 @@ GET articles/_search
178176
}
179177
```
180178

181-
### Solution 3: Use the $ shortcut in field API syntax
179+
## Solution 3: Use the $ shortcut in field API syntax
182180

183-
With the field API you can make the solution even more concise using the `$` shortcut:
181+
With the field API, you can make the solution even more concise using the `$` shortcut:
184182

185183
```json
186184
GET articles/_search
@@ -204,7 +202,7 @@ GET articles/_search
204202
}
205203
```
206204

207-
### Results
205+
## Results
208206

209207
```json
210208
{
@@ -238,12 +236,10 @@ GET articles/_search
238236
}
239237
```
240238

241-
### Notes
239+
## Notes
242240

243-
* **Field presence:** Always check if fields exist before accessing them using `.size() > 0`.
241+
* **Field presence:** Always check if fields exist before accessing them, using `.size() > 0`.
244242
* **Document variation:** Not all documents are guaranteed to have the same field structure.
245243
* **Mapping awareness:** A field must be defined in the index mappings to be accessed with doc values, and its value in each document must be validated.
246-
* **Field API:** The `field` API and `$` shortcut handle missing values gracefully with default values.
247-
* **Compatibility:** Some field types (like `text` or `geo`) [aren't yet supported](/explore-analyze/scripting/script-fields-api.md#_supported_mapped_field_types) by the field API; continue using `doc` for those.
248-
249-
244+
* **Field API:** The `field` API and `$` shortcut handle missing values gracefully, using default values.
245+
* **Compatibility:** Some field types (such as `text` or `geo`) [aren't yet supported](/explore-analyze/scripting/script-fields-api.md#_supported_mapped_field_types) by the field API; continue using `doc` for those.

troubleshoot/elasticsearch/painless-ingest-pipeline-failures.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ products:
99

1010
# Troubleshoot ingest pipeline failures in Painless
1111

12-
Follow these guidelines to avoid [ingest pipeline](elasticsearch://reference/scripting-languages/painless/painless-ingest-processor-context.md) script errors in your Painless script.
12+
Follow these guidelines to avoid [ingest pipeline](elasticsearch://reference/scripting-languages/painless/painless-ingest-processor-context.md) errors in your Painless scripts.
1313

14-
## Date conversion
14+
When you convert time strings to nanoseconds in ingest pipelines, attempting to perform arithmetic operations directly on string values without proper date parsing causes type casting errors.
1515

16-
### Class cast exception when converting date data types
17-
18-
When converting time strings to nanoseconds in ingest pipelines, attempting to perform arithmetic operations directly on string values without proper date parsing leads to type casting errors.
19-
20-
### Sample error
16+
## Sample error
2117

2218
```json
2319
{
@@ -64,7 +60,7 @@ When converting time strings to nanoseconds in ingest pipelines, attempting to p
6460
}
6561
```
6662

67-
### Problematic code
63+
## Problematic code
6864

6965
```json
7066
{
@@ -76,11 +72,11 @@ When converting time strings to nanoseconds in ingest pipelines, attempting to p
7672
}
7773
```
7874

79-
### Root cause
75+
## Root cause
8076

81-
When accessing fields via `ctx.time_field` in ingest pipelines, the values are not automatically parsed to their mapped field types. The script attempts to multiply a string value (time field) directly with an integer. Time strings like `"00:00:00.022"` remain as strings and need to be properly parsed as dates and converted to epoch milliseconds before performing arithmetic operations.
77+
When accessing fields using `ctx.time_field` in ingest pipelines, the values are not automatically parsed to their mapped field types. The script attempts to multiply a string value (time field) directly with an integer. Time strings such as `"00:00:00.022"` remain as strings. They need to be properly parsed as dates and converted to epoch milliseconds before performing arithmetic operations.
8278

83-
### Solution: Use `SimpleDateFormat` in the script processor
79+
## Solution: Use `SimpleDateFormat` in the script processor
8480

8581
Parse the time string directly using SimpleDateFormat and get epoch milliseconds:
8682

@@ -113,7 +109,7 @@ POST _ingest/pipeline/_simulate
113109
}
114110
```
115111

116-
### Result
112+
## Result
117113

118114
```json
119115
{
@@ -136,7 +132,7 @@ POST _ingest/pipeline/_simulate
136132
}
137133
```
138134

139-
### Note
135+
## Notes
140136

141-
* Time strings like `"HH:mm:ss.SSS"` must be explicitly parsed before arithmetic operations.
137+
* Time strings such as `"HH:mm:ss.SSS"` must be explicitly parsed before performaing arithmetic operations.
142138
* Using `SimpleDateFormat` in a script processor allows custom parsing.

troubleshoot/elasticsearch/painless-null-pointer-exceptions.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@ products:
99

1010
# Troubleshoot null pointer exceptions in Painless
1111

12-
Follow these guidelines to avoid null pointer exceptions in your Painless script.
12+
Follow these guidelines to avoid null pointer exceptions in your Painless scripts.
1313

14-
## Null\_pointer\_exception
14+
In Painless, field access methods vary depending on the script execution [context](elasticsearch://reference/scripting-languages/painless/painless-contexts.md). Using a field access pattern that is not valid for the current script context causes a `null_pointer_exception`.
1515

16-
Null pointer exceptions in Painless scripts often occur due to using field access patterns that are not valid for the current script context.
17-
18-
### Wrong field access patterns in script contexts
19-
20-
When writing Painless scripts in different [contexts](elasticsearch://reference/scripting-languages/painless/painless-contexts.md), using wrong field access patterns leads to null pointer exceptions because field access methods vary depending on the script execution context.
21-
22-
### Sample error
16+
## Sample error
2317

2418
```json
2519
{
@@ -109,7 +103,8 @@ POST products/_doc
109103

110104
### Root cause
111105

112-
A common cause of null pointer exceptions in Painless scripts is attempting to access document fields using incorrect access patterns for the specific [script context](elasticsearch://reference/scripting-languages/painless/painless-contexts.md). In Painless, field access methods are context-dependent. For more information, refer to "Painless syntax-context bridge" in the Explore and Analyze documentation.
106+
A common cause of null pointer exceptions in Painless scripts is attempting to access document fields using incorrect access patterns for the specific [script context](elasticsearch://reference/scripting-languages/painless/painless-contexts.md). To learn more about the context-dependent field access methods in Painless, refer to Painless syntax-context bridge in the Explore and Analyze documentation.
107+
% Link to come after E&A PR is merged.
113108

114109
The error occurs because `params['_source']` is not available in script filter contexts. In script filters, field values must be accessed through `doc` values, not through the `params['_source']` map.
115110

@@ -167,5 +162,3 @@ GET products/_search
167162
* **Context limitations:** Script filters cannot access `params['_source']` .
168163
* **Field mapping:** Use `.keyword` suffix for text fields when accessing via doc values.
169164

170-
For more details related to Painless context, refer to [Painless context documentation](elasticsearch://reference/scripting-languages/painless/painless-contexts.md) and Painless syntax-context bridge.
171-

0 commit comments

Comments
 (0)