title: How to properly encode / submit Elasticsearch query from Java?
body: <p>In my Spring Boot app's REST controller, I receive a GET call from a Node.js app, parse the incoming query string, and construct a query for Elasticsearch (v 5.4)</p>
  
  <p>The elasticsearch query string, which I have constructed looks like this:</p>
  
  <pre><code>http://34.230.72.180:9200/metadata/_search/?q="*desc*2*"
  </code></pre>
  
  <p>When I take this string, and submit it from a browser or Postman, I get the expected search results back.</p>
  
  <p>However, when I submit the exactly same query from my REST controller:</p>
  
  <pre><code>HttpGet getRequest = new HttpGet(
                      finalReq);  //finalReq: http://34.230.72.180:9200/metadata/_search/?q="*desc*2*"
              getRequest.addHeader("accept", "application/json");
  
              HttpResponse response = httpClient.execute(getRequest);
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>INFO: Initializing Spring FrameworkServlet 'dispatcherServlet'
  http://34.230.72.180:9200/metadata/_search/?q="*desc*2*"
  Jan 06, 2018 12:33:42 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Illegal character in query at index 46: http://34.230.72.180:9200/metadata/_search/?q="*desc*2*"] with root cause
  java.net.URISyntaxException: Illegal character in query at index 46: http://34.230.72.180:9200/metadata/_search/?q="*desc*2*"
      at java.net.URI$Parser.fail(URI.java:2848)
      at java.net.URI$Parser.checkChars(URI.java:3021)
      at java.net.URI$Parser.parseHierarchical(URI.java:3111)
      at java.net.URI$Parser.parse(URI.java:3053)
      at java.net.URI.&lt;init&gt;(URI.java:588)
      at java.net.URI.create(URI.java:850)
  </code></pre>
  
  <p>What am I doing wrong here?</p>