Removing occurrences of repeated text Characters from title in Elastic cloud

I would like to remove any "| Brand" occurrences in the title field in such results.

What script should I use?

I tried painless scripts

String title = doc['title'].value;
if (title != null) {
// Use replaceAll to remove all occurrences of "| abc def"
return title.replaceAll('\|\sabc\sdef', '');
}
return ''; // Return an empty string if title is null

But I think the logic is incorrect.

Let me know if I should use use Kibana, ingestion API or anything else, I'm not a dev, but I do know how to get around the platform, so do need some step instructions.

I'd run that script at index time using a Script Processor in an ingest pipeline.
Or a gsub processor.

Is my script correct?

To remove occurrences of "|Brand" from the title field in Elasticsearch, you can use a Painless script.

Yes, but where will I need to place the script?

I tried painless String title = doc['title'].value;
if (title != null) {
// Use replaceAll to remove all occurrences of "| Brand AB"
return title.replaceAll('|\sBrand\sAB', '');
}
return ''; // Return an empty string if title is null

I'm not a dev, so need some guidence