Skip to content

Commit d6ffe01

Browse files
authored
Avoid nested docs in painless execute api (#127991)
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
1 parent 6052c5b commit d6ffe01

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
@@ -831,7 +831,8 @@ private static Response prepareRamIndex(
831831
// This is a problem especially for indices that have no mappings, as no fields will be accessible, neither through doc
832832
// nor _source (if there are no mappings there are no metadata fields).
833833
ParsedDocument parsedDocument = documentMapper.parse(sourceToParse);
834-
indexWriter.addDocuments(parsedDocument.docs());
834+
// only index the root doc since nested docs are not supported in painless anyways
835+
indexWriter.addDocuments(List.of(parsedDocument.rootDoc()));
835836
try (IndexReader indexReader = DirectoryReader.open(indexWriter)) {
836837
final IndexSearcher searcher = new IndexSearcher(indexReader);
837838
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)