class FacetedSearchService {

    public String escapeTermsInQuery(String query)
    {
        if (StringUtils.isEmpty(query))
        {
            return "";
        }
        query = query.trim();
        StringBuilder sb = new StringBuilder();
        for (String term : getSearchTerms(query))
        {
            if (term.endsWith("\"") && term.startsWith("\""))
            {
                // already escaped with quotes, don't escape it
                sb.append(" ").append(term);
            }
            else
            {
                // it must be escaped because term can contain special character which can interfere constructing the
                // query
                sb.append(" \"").append(term).append("\"");
            }
        }
        return sb.toString().trim();
    }

}