Skip to content

Commit ce44715

Browse files
committed
BUG: any_text_field search was very slow when combining query with all attachments. Make attachments search apart but combined
1 parent 17c6134 commit ce44715

1 file changed

Lines changed: 23 additions & 16 deletions

File tree

src/Controllers/TicketsController.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,26 +194,33 @@ public function getTicketCollectionFrom($ticketList)
194194
}
195195

196196
if (isset($search_fields['any_text_field'])) {
197-
$collection->where(function ($query) use ($search_fields) {
197+
// Coincidence in Ticket / Comment attachments
198+
$attachments_col = Attachment::where('original_filename', 'like', '%' . $search_fields['any_text_field'] . '%')
199+
->orWhere('new_filename', 'like', '%' . $search_fields['any_text_field'] . '%')
200+
->orWhere('description', 'like', '%' . $search_fields['any_text_field'] . '%')
201+
->with('ticket', 'comment.ticket')->get();
202+
203+
$a_ticket_ids = [];
204+
foreach ($attachments_col as $att){
205+
if (!is_null($att->ticket)){
206+
$a_ticket_ids[] = $att->ticket->id;
207+
208+
}elseif (!is_null($att->comment) and !is_null($att->comment->ticket)){
209+
$a_ticket_ids[] = $att->comment->ticket->id;
210+
}
211+
}
212+
213+
// Coincidence in any ticket field
214+
$collection->where(function ($query) use ($search_fields, $a_ticket_ids) {
198215
$query->where('subject', 'like', '%'.$search_fields['any_text_field'].'%')
199216
->orWhere('content', 'like', '%'.$search_fields['any_text_field'].'%')
200-
->orWhere('html', 'like', '%'.$search_fields['any_text_field'].'%')
201217
->orWhere('intervention', 'like', '%'.$search_fields['any_text_field'].'%')
202-
->orWhere('intervention_html', 'like', '%'.$search_fields['any_text_field'].'%')
203-
->orWhereHas('attachments', function ($q1) use ($search_fields) {
204-
$q1->where('original_filename', 'like', '%'.$search_fields['any_text_field'].'%')
205-
->orWhere('new_filename', 'like', '%'.$search_fields['any_text_field'].'%')
206-
->orWhere('description', 'like', '%'.$search_fields['any_text_field'].'%');
207-
})
208218
->orWhereHas('comments', function ($q2) use ($search_fields) {
209-
$q2->where('content', 'like', '%'.$search_fields['any_text_field'].'%')
210-
->orWhere('html', 'like', '%'.$search_fields['any_text_field'].'%')
211-
->orWhereHas('attachments', function ($q3) use ($search_fields) {
212-
$q3->where('original_filename', 'like', '%'.$search_fields['any_text_field'].'%')
213-
->orWhere('new_filename', 'like', '%'.$search_fields['any_text_field'].'%')
214-
->orWhere('description', 'like', '%'.$search_fields['any_text_field'].'%');
215-
});
216-
});
219+
$q2->where('content', 'like', '%'.$search_fields['any_text_field'].'%');
220+
})
221+
222+
// Attachment with coincidence with "any_text_field"
223+
->orWhereIn('panichd_tickets.id', $a_ticket_ids);
217224
});
218225
}
219226
}

0 commit comments

Comments
 (0)