Hello world,
I'm trying to query with a filter to restrict array size. (Only get the results that have more than 1 object inside the transactions array)
I can't manage to get it working. I hope some people here have some ideas to help me.
My mapping is the following:
{
"properties": {
"userId": {"type": "keyword"},
"shopId": {"type": "keyword"},
"transactions": {
"type": "nested",
"properties": {
"date": {"type": "text"},
"commissionStatus": {"type": "text"},
"commissionAmount": {"type": "float"},
"commissionAmountTicker": {"type": "text"},
"saleAmount": {"type": "float"},
"saleCurrencyTicker": {"type": "text"},
"id": {"type": "text"},
"type": {"type": "text"},
"network": {"type": "text"}
}
}
}
}
I'm trying to do the following query:
{
"query": {
"bool": {
"must": [{
"match": {"shopId":"5109468509503488"}
}],
"filter": [{
"range" : {
"@timestamp" : {
"gte": "now-7d/d",
"lte": "now/d"
}
}
},
{
"script": {
"script": {
"source": "doc['transactions'].length >= 1",
"lang": "painless"
}
}
}]
}
},
"size": 1
}
But I get an error:
"failed_shards": [
{
"shard": 0,
"index": "transactions",
"node": "TpzdGPfjTjejIAnk4mSgEg",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
"doc['transactions'].length >= 1",
" ^---- HERE"
],
"script": "doc['transactions'].length >= 1",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [transactions] in mapping with types "
}
}
}
]
Thank you for your help.