How to compare two documents from two different indexes using script

Hi, I am searching for similar fields in two different indexes. My goal is to if those fields match, then I know there is a correlation between those two documents.

I do search first with accountNumber, and I group them with another identifier, so that is the reason I am doing what I am doing in aggregations. this is just a part of that bigger query.

For example, in Index A1

{
"_id": 1,
"refNumber": 123
"uID": 456
}

and in index B2

{
"_id": 1,
"refNumber": 6456456,
"uId": 123
}

So in the above two documents, the 'refNumber' in A1 index is equal to "uId" in the document with index B2. So here is what I am trying

GET/A1, B2/_search
{
"size": 0,
"aggs": {
"correlatedDocuments: {
"terms": {
"script": {
"source": "if(doc['A1.refNumber.keyword'].value == doc['B2.uId.keyword].value ) { return 1}"
}
}
}
}
}

obviously that does not work. How to solve this kind of problem? it does not have to be Painless script. I just want to connect the documents based on those two key values in two different indexes.

The data remodeling is not an option. May be if I find a solution that needs a change, then I can push for the change but is not an easy task.

Any help would be appreciated

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