title: Spring-data-elasticsearch run native JSON query
body: <p>I have a native elasticsearch query (in form of a JSON). It's quite complicated, so I don't want to rewrite it into Java code.</p>
  <pre class="lang-json prettyprint-override"><code>{
     &quot;query&quot;: {
         &quot;bool&quot;: { ... }
     },
     &quot;sort&quot;: {
         &quot;_script&quot;: { ... }
     }
  }
  </code></pre>
  <p>How to execute such native query using <code>spring-data-elasticsearch</code> client (without @Repository and @Query annotations)? <code>Query query = StringQuery.builder(&quot;json here&quot;).withSort(...).build()</code> doesn't work, because it doesn't support custom sorting mechanisms (by script).</p>
  <p>I've tried using NativeSearchQuery, but I find it impossible to pass perfectly valid native query.</p>
  <pre class="lang-java prettyprint-override"><code>NativeSearchQueryBuilder()
      .withQuery(QueryBuilders.queryStringQuery(&quot;query from above&quot;))
      .withSorts(/* sorted by script */)
      .build()
  // results in &quot;Failed to parse query&quot; error.
  </code></pre>