Search code examples
solrsolrj

solrj: Search for words with whitepaces


I want to search for eg:"project launch". I am replacing whitepace with "/ ". This works properly when I query directly from solr. But it doesnt work when called from solrj. Please help!


Solution

  • If a user entered a search query with the phrase "project launch" (with quotes), then you can use ClientUtils.escapeQueryChars to escape the quotes with Solrj:

    String q = "\"project launch\"";
    q = ClientUtils.escapeQueryChars(q);
    

    Note that this will escape all the Lucene special characters used in query syntax. If you want more control over what special (query syntax) characters are allowed in your queries, then create your own copy of this function and modify accordingly.