In my backend code I have to query for documents where a specific field has a specific value. The value is provided by the user, so I have to make sure to escape characters that are special to solr, I guess.
In a training we used
org.apache.solr.client.solrj.util.ClientUtils.escapeQueryChars(fieldValue)
However, when I do that my search returns no results for a search by a UUID like 12345678-1234-abcd-1234-123456789012, even though that field value is present.
If I do the search with unescaped value, I get the desired results.
The docs here use no escaping at all: GetA12 Login
So: Do I have to escape characters? How/which ones?
Hi @stephen-warm-graph,
I just had a look at the implementation of the document query service in DataServices and noticed that they escape filter values when building the search query (see com.mgmtp.a12.dataservices.search.internal.SolrQueryParser#processStringFilter).
This means that if you escape characters before sending the filters to DataServices, they will be escaped again on the DataServices side, resulting in filter values that won’t match your expectations.
Hello @stephen-warm-graph
Did answer above help you? Can we mark it as a solution?
Thanks for the answer!
I guess, that solves it, but to really understand it, I had to dig into the code.
Found out that there is a regex that splits the filter text into a left part (field path that must not be escaped) and a right part that needs to be escaped (SolrSearchConstants.TERM_PATTERN).
So I can use FilterSpec.addFilter() for simple field based queries and escaping is handled for me.
I can do more complex queries by using FilterSpec.setFullText() to do more complex queries, but then I have to escape relevant parts myself?
Related: Please add Javadoc to FilterSpec class that explains the correct usage!
Currently it has no code documentation at all.
You can enable special character escaping in fulltext query by configuring this property mgmtp.a12.dataservices.search.analysis.fullText.escapeSpecialCharacters.enabled. For more details, please refer to the official documentation at Redirecting…