You can use runtime mapping with painless script for archiving your logic. But this is not recommended as it might impact the performance if you have large dataset.
Lets consider, you have data like below in Elasticsearch index:
"hits": [
{
"_index": "test1",
"_id": "1",
"_score": 1,
"_source": {
"id": "1",
"cardaccdetails": [
{
"mkt_code": "101",
"cid": "A"
},
{
"mkt_code": "201",
"cid": "B"
}
]
}
},
{
"_index": "test1",
"_id": "2",
"_score": 1,
"_source": {
"id": "1",
"cardaccdetails": [
{
"mkt_code": "101",
"cid": "B"
},
{
"mkt_code": "201",
"cid": "A"
}
]
}
}
]
You can use below query:
POST test1/_search
{
"runtime_mappings": {
"isMatch": {
"type": "boolean",
"script": {
"source": """
for (item in params._source['cardaccdetails']){
if(item['mkt_code']== '101' && item['cid']== 'A')
emit(true);
}
emit(false);
"""
}
}
},
"query": {
"match": {
"isMatch": "true"
}
}
}
This will return only one document which is having 101 and A into same object inside array.
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "test1",
"_id": "1",
"_score": 1,
"_source": {
"id": "1",
"cardaccdetails": [
{
"mkt_code": "101",
"cid": "A"
},
{
"mkt_code": "201",
"cid": "B"
}
]
}
}
]
}
}
You can put this query as Search template and pass value as parameter.