You can modify your search so that it returns pair(s) of fields called earliest and latest.
You can then use it as a subsearch which will be expanded in runtime.
For example, this will search from index A only during those periods each day that there are events in index B
index=A
[ index=B
| bin _time as day span=1d
| stats earliest_time as earliest latest_time as latest by day
| table earliest latest ]
All typical caveats regarding subsearches apply.