Hello, I am trying to do the following. Suppose there is an index called my-index with the following mapping:
{
"my-index": {
"mappings": {
"properties": {
"fields": {
"type": "nested",
"properties": {
"id": {
"type": "keyword"
},
"names": {
"type": "keyword"
}
}
}
}
}
}
}
What I am trying to do is sort the documents based on fields[fieldId].names, where fieldId will be given in the params dynamically. This is what I've tried from Python shell.
import requests
headers = {
'Content-Type': 'application/json',
}
json_data = {
'script': {
'source': """
// Simplified version of an attempt
if (doc['fields'] == null || doc['fields'].length == 0) {
return false;
}
return true;
""",
'params': {
'fieldId': 'id1'
},
'lang': 'painless',
},
'context': 'filter',
'context_setup': {
'index': 'my-index',
'document': {
'fields': [
{
'id': 'id1',
'names': 'CF1,CF3',
},
{
'id': 'id2',
'names': [],
},
{
'id': 'id3',
'names': 'hello,world,lol,zzz',
},
],
},
},
}
requests.post('http://localhost:9200/_scripts/painless/_execute?pretty', headers=headers, json=json_data).json()
I did not write the full query in the source. The main problem I am having is that whenever I try to access doc['fields']
, it's throwing an error no matter what I try. And I can't figure out why it's happening and how to solve it. I can access doc['field.id']
and doc['field.names']
but not doc['field']
. Any idea why I could be getting the following error and how to solve it? Any help would be greatly appreciated, thanks.
{'error': {'root_cause': [{'type': 'script_exception', 'reason': 'runtime error', 'script_stack': ['org.opensearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:87)', 'org.opensearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:55)', "if (doc['fields'] == null || doc['fields'].length == 0) {\n ", ' ^---- HERE'], 'script': ' ...', 'lang': 'painless', 'position': {'offset': 69, 'start': 61, 'end': 133}}], 'type': 'script_exception', 'reason': 'runtime error', 'script_stack': ['org.opensearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:87)', 'org.opensearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:55)', "if (doc['fields'] == null || doc['fields'].length == 0) {\n ", ' ^---- HERE'], 'script': ' ...', 'lang': 'painless', 'position': {'offset': 69, 'start': 61, 'end': 133}, 'caused_by': {'type': 'illegal_argument_exception', 'reason': 'No field found for [fields] in mapping'}}, 'status': 400}