Skip to content

Commit 964a4e4

Browse files
Avoid nested docs in painless execute api (#127991) (#128142)
Painless does not support accessing nested docs (except through _source). Yet the painless execute api indexes any nested docs that are found when parsing the sample document. This commit changes the ram indexing to only index the root document, ignoring any nested docs. fixes #41004 Co-authored-by: Elastic Machine <[email protected]>
1 parent 64005c4 commit 964a4e4

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

docs/changelog/127991.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 127991
2+
summary: Avoid nested docs in painless execute api
3+
area: Infra/Scripting
4+
type: bug
5+
issues:
6+
- 41004

modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,8 @@ private static Response prepareRamIndex(
828828
// This is a problem especially for indices that have no mappings, as no fields will be accessible, neither through doc
829829
// nor _source (if there are no mappings there are no metadata fields).
830830
ParsedDocument parsedDocument = documentMapper.parse(sourceToParse);
831-
indexWriter.addDocuments(parsedDocument.docs());
831+
// only index the root doc since nested docs are not supported in painless anyways
832+
indexWriter.addDocuments(List.of(parsedDocument.rootDoc()));
832833
try (IndexReader indexReader = DirectoryReader.open(indexWriter)) {
833834
final IndexSearcher searcher = new IndexSearcher(indexReader);
834835
searcher.setQueryCache(null);

modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteApiTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ public void testDefaults() throws IOException {
7070
assertThat(e.getCause().getMessage(), equalTo("cannot resolve symbol [doc]"));
7171
}
7272

73+
public void testNestedDocs() throws IOException {
74+
ScriptService scriptService = getInstanceFromNode(ScriptService.class);
75+
IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "rank", "type=long", "nested", "type=nested");
76+
77+
Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray("""
78+
{"rank": 4.0, "nested": [{"text": "foo"}, {"text": "bar"}]}"""), new MatchAllQueryBuilder());
79+
contextSetup.setXContentType(XContentType.JSON);
80+
Request request = new Request(new Script(ScriptType.INLINE, "painless", "doc['rank'].value", Map.of()), "score", contextSetup);
81+
Response response = innerShardOperation(request, scriptService, indexService);
82+
assertThat(response.getResult(), equalTo(4.0D));
83+
}
84+
7385
public void testFilterExecutionContext() throws IOException {
7486
ScriptService scriptService = getInstanceFromNode(ScriptService.class);
7587
IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "field", "type=long");

0 commit comments

Comments
 (0)