LPS-48640 (Got stuck, need some help or advice) Searching for users with invalid characters returns all results

https://issues.liferay.com/browse/LPS-48640

Hey Mike,

Thank you for offering your help in Zulip on October 9th. :-) (Elasticsearch topic)

I hope you will have some time slot to take a closer look at this issue. I'm trying to sum-up here everything you need to know, but please, also get through my comments at https://issues.liferay.com/browse/LPS-48640 in case there were unclear parts. So.

1- The LPS looked easy to fix at first sight: if you search for some special lucene charaters like "!", you will get all results back. OK, restore the "escape" logic in LuceneHelperImpl and everything will be fine, they said...

2- Technically, lipusz@a7ea177 solves this. Sadly, another problem raised up: https://issues.liferay.com/browse/LPS-48640?focusedCommentId=513153&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-513153

--> lipusz@d70cb1c came and saved my ass.

3- OK, almost sent the pull Ray, when suddenly... ES started throw "SearchPhaseExecutionException" for searches like "!" or any spec. lucene character. --> https://issues.liferay.com/browse/LPS-48640?focusedCommentId=513803&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-513803

User enters a search keyword (e.g. "!") --> LHI#addTerm parses it (and escapes if necessary) --> Lucene Query objects gets created, e.g. a TermQuery where the Term's value is "!" --> ElasticsearchIndexSearch#doSearch invokes

````java
		QueryBuilder queryBuilder =
			QueryBuilders.queryString(query.toString());
````

--> calling a "toString()" on a lucene query doesn't preserve the escape sequence (https://issues.apache.org/jira/browse/LUCENE-271),  --> ES receives a "query_string" where the spec. characters are not escaped --> ES tries to parse the string --> fails

4- I didn't find any way to tell ES to escape the spec. characters when they are not operators automatically.
5- I created the helper in portal's side first: https://github.com/lipusz/liferay-portal/commits/LPS-48640-master

It works but goes against the concept of decoupling search from the portal and ship as an OSGi module.

6- Created lipusz@48c7c20, but it fails due to the different classloaders used by the Portal (in LHI to construct the lucene Query) and ES, so casting and "instanceof" fail for the obvious reasons.

How should we overcome this? Is my concept to avoid SearchPhaseExecutionException in ES totally wrong?

I appreciate any help.

Thanks,
Tibor