Search for a field and get only the JsonObejct below it

Hello everyone!
I'm quite new to ES and have a question that's been bothering me for a few days:
I'm trying to search for a certain field and just want to output the Json-Object below it. Here is a short example:

-data:

....
source:
{
"Test":
{"ABCD":
[ { "Fruit":
[ {"Type": "Banana"},
{"Type": "Cherry"}
....
]},
{"Vegetables":
[{"Type": "Carrot"}]
...
}]
}
}

-desired output:
{ "Fruit":
[ {"Type": "Banana"},
{"Type": "Cherry"}]}

I use the java High-Level-api and have already tried several things. E.G.

SearchResponse sr = client.getClient().search(
                    new SearchRequest(index).searchType(SearchType.DFS_QUERY_THEN_FETCH)
                    .source(new SearchSourceBuilder()
                            .fetchSource(new String[] {"Test.ABCD.*.Fruit"}, null)), RequestOptions.DEFAULT);

But here, for example, I get the output:
{
"Test":
{"ABCD":
[ { "Fruit":
[ {"Type": "Banana"},
{"Type": "Cherry"}
]
}]
}
}
But I'd like only { "Fruit": [ {"Type": "Banana"},{"Type": "Cherry"}]}
Is that possible somehow? (I already tried it with thisscript:

.scriptField("Fruit", new Script("params['_source']['Test']['ABCD']['Fruit']"

, but I didn't manage that either...

Thank you!! :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.