You are correct. In that case, instead of subtracting the scheduled time from the record time, you would need to subtract the time the first question was responded from the record time. Here we assume the time the first question was responded is the time the survey was opened.
For this purpose, change the formula to this:
return doc['record_time'].date.getMillis() - doc['resp_time'].date.getMillis();
As Kibana stores each response as a separate record, and the above formula is calculated for each record separately (therefore, for each question separately), the above formula calculates how long after user responded to each question, the survey was completed.
In this case, when you go back to the “Discover” tab of Kibana, you would need to filter the records to show only first question, e.g. “q_id is 1” or any other ID is used for the first question. This will give you the time the participant spent responding the survey.
Hope this helps. Let me know if I should clarify any part of this.