Filter Documents based on data of other documents

I have an index where documents will be of two types such as Type1 and Type2
{
{ "id":1,
"SourceType": "Type1",
"ItemNumber": "AS5888",
"AgreementNumber": null
}

{
"id":2,
"SourceType": "Type2",
"ItemNumber": "AS5888",
"AgreementNumber": "Agreement1"
}

{
"id":3,
"SourceType": "Type2",
"ItemNumber": "AS5888",
"AgreementNumber": "Agreement2"
}

{
"id":4,
"SourceType": "Type1",
"ItemNumber": "AS5889",
"AgreementNumber": null
}
}
If i search for "AS5888", only it should return type2 documents(id 2 and 3).
If i search for "AS5889", then it should return type1 documents(id 4) because type2 document is not present.

When we search, the type1 document should return only if type2 document is not present.

I don't think this is something you can solve with the ES query DSL, but rather need to do in your client application logic. Search "xyz" for docs of "type2" first, then only rerun that search when there are no results on docs of "type1". But maybe I'm wrong and somebody has a better idea?

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