<p dir="auto">If you use setSource on a SearchRequestBuilder and then call to string the SearchRequest will appear to be cleared.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="SearchRequestBuilder srb = client().prepareSearch().setSource(&quot;{\&quot;query\&quot;:\&quot;match\&quot;:{\&quot;foo\&quot;:\&quot;bar\&quot;}}&quot;);
srb.execute().get();"><pre class="notranslate"><code class="notranslate">SearchRequestBuilder srb = client().prepareSearch().setSource("{\"query\":\"match\":{\"foo\":\"bar\"}}");
srb.execute().get();
</code></pre></div>
<p dir="auto">Will execute the correct search, however</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="SearchRequestBuilder srb = client().prepareSearch().setSource(&quot;{\&quot;query\&quot;:\&quot;match\&quot;:{\&quot;foo\&quot;:\&quot;bar\&quot;}}&quot;);
logger.debug(&quot;About to execute [{}]&quot;,srb.toString());
srb.execute().get();"><pre class="notranslate"><code class="notranslate">SearchRequestBuilder srb = client().prepareSearch().setSource("{\"query\":\"match\":{\"foo\":\"bar\"}}");
logger.debug("About to execute [{}]",srb.toString());
srb.execute().get();
</code></pre></div>
<p dir="auto">Will execute an empty search.</p>
<p dir="auto">The problem is that <code class="notranslate">toString()</code> calls <code class="notranslate">internalBuilder()</code> which calls <code class="notranslate">sourceBuilder()</code></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    private SearchSourceBuilder sourceBuilder() {
        if (sourceBuilder == null) {
            sourceBuilder = new SearchSourceBuilder();
        }
        return sourceBuilder;
    }"><pre class="notranslate"><code class="notranslate">    private SearchSourceBuilder sourceBuilder() {
        if (sourceBuilder == null) {
            sourceBuilder = new SearchSourceBuilder();
        }
        return sourceBuilder;
    }
</code></pre></div>
<p dir="auto">Then when the <code class="notranslate">SearchRequestBuilder.execute</code> is called the request that was constructed via <code class="notranslate">setSource</code> is replaced by an empty sourceBuilder.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    @Override
    protected void doExecute(ActionListener&lt;SearchResponse&gt; listener) {
        if (sourceBuilder != null) {
            request.source(sourceBuilder());
        }
        client.search(request, listener);
    }"><pre class="notranslate"><code class="notranslate">    @Override
    protected void doExecute(ActionListener&lt;SearchResponse&gt; listener) {
        if (sourceBuilder != null) {
            request.source(sourceBuilder());
        }
        client.search(request, listener);
    }
</code></pre></div>
<p dir="auto">Using the Java API, If one sets the content of a search request through <code class="notranslate">SearchRequestBuilder#setSource</code> methods and then calls <code class="notranslate">toString</code> to see the result, not only the content of the request is not returned as it wasn't set through <code class="notranslate">sourceBuilder()</code>,  the content of the request gets also reset due to the <code class="notranslate">internalBuilder()</code> call in <code class="notranslate">toString</code>.</p>
<p dir="auto">Here is a small failing test that demontrates it:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client()).setSource(&quot;{\n&quot; +
                &quot;            \&quot;query\&quot; : {\n&quot; +
                &quot;            \&quot;match\&quot; : {\n&quot; +
                &quot;                \&quot;field\&quot; : {\n&quot; +
                &quot;                    \&quot;query\&quot; : \&quot;value\&quot;&quot; +
                &quot;                }\n&quot; +
                &quot;            }\n&quot; +
                &quot;        }\n&quot; +
                &quot;        }&quot;);
String preToString = searchRequestBuilder.request().source().toUtf8();
searchRequestBuilder.toString();
String postToString = searchRequestBuilder.request().source().toUtf8();
assertThat(preToString, equalTo(postToString));"><pre class="notranslate"><code class="notranslate">SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client()).setSource("{\n" +
                "            \"query\" : {\n" +
                "            \"match\" : {\n" +
                "                \"field\" : {\n" +
                "                    \"query\" : \"value\"" +
                "                }\n" +
                "            }\n" +
                "        }\n" +
                "        }");
String preToString = searchRequestBuilder.request().source().toUtf8();
searchRequestBuilder.toString();
String postToString = searchRequestBuilder.request().source().toUtf8();
assertThat(preToString, equalTo(postToString));
</code></pre></div>