<p dir="auto">Related to: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="59153240" data-permission-text="Title is private" data-url="https://github.com/elastic/elasticsearch/issues/9910" data-hovercard-type="issue" data-hovercard-url="/elastic/elasticsearch/issues/9910/hovercard" href="https://github.com/elastic/elasticsearch/issues/9910">#9910</a></p>
<p dir="auto">If you use JSON string as the query and set it by calling <code class="notranslate">setSource()</code> method, before executing the query if you call <code class="notranslate">requestBuilder.toString()</code> and then execute the query, you will get incorrect response, because <code class="notranslate">toString()</code> modified the internal state of <code class="notranslate">SearchRequestBuilder</code> object.</p>
<p dir="auto">Try this code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@GrabResolver(name='maven', root='http://repo1.maven.org/maven2/')
@Grab(group='org.elasticsearch', module='elasticsearch', version='1.4.4')
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.common.settings.ImmutableSettings
import org.elasticsearch.common.transport.InetSocketTransportAddress
import org.elasticsearch.client.Client
import org.elasticsearch.client.transport.TransportClient
import org.elasticsearch.action.index.IndexRequestBuilder
import org.elasticsearch.action.index.IndexResponse
import org.elasticsearch.action.search.SearchRequestBuilder
import org.elasticsearch.action.search.SearchResponse

Settings settings = ImmutableSettings.settingsBuilder().put(&quot;cluster.name&quot;, &quot;elasticsearch&quot;).build();
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(&quot;localhost&quot;, 9300));

[1,2,3,4].each() {
    String body = &quot;&quot;&quot;{&quot;name&quot;:&quot;Hello world ${it}&quot;}&quot;&quot;&quot;
    IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(client, &quot;twitter&quot;)
            .setType(&quot;data&quot;)
            .setId(it.toString())
            .setSource(body)
            .setRefresh(true)
    IndexResponse indexResponse = indexRequestBuilder.execute().actionGet()
}

String jsonQuery = &quot;&quot;&quot;{&quot;query&quot;:{&quot;filtered&quot;:{&quot;filter&quot;:{&quot;term&quot;:{&quot;_id&quot;:&quot;1&quot;}}}}}&quot;&quot;&quot;
SearchRequestBuilder requestBuilder = client.prepareSearch(&quot;twitter&quot;)
            .setTypes(&quot;data&quot;)
            .setSource(jsonQuery)

// This is the bug
println requestBuilder.toString()

SearchResponse response = requestBuilder.execute().actionGet()
println response.toString()"><pre class="notranslate"><code class="notranslate">@GrabResolver(name='maven', root='http://repo1.maven.org/maven2/')
@Grab(group='org.elasticsearch', module='elasticsearch', version='1.4.4')
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.common.settings.ImmutableSettings
import org.elasticsearch.common.transport.InetSocketTransportAddress
import org.elasticsearch.client.Client
import org.elasticsearch.client.transport.TransportClient
import org.elasticsearch.action.index.IndexRequestBuilder
import org.elasticsearch.action.index.IndexResponse
import org.elasticsearch.action.search.SearchRequestBuilder
import org.elasticsearch.action.search.SearchResponse

Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build();
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));

[1,2,3,4].each() {
    String body = """{"name":"Hello world ${it}"}"""
    IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(client, "twitter")
            .setType("data")
            .setId(it.toString())
            .setSource(body)
            .setRefresh(true)
    IndexResponse indexResponse = indexRequestBuilder.execute().actionGet()
}

String jsonQuery = """{"query":{"filtered":{"filter":{"term":{"_id":"1"}}}}}"""
SearchRequestBuilder requestBuilder = client.prepareSearch("twitter")
            .setTypes("data")
            .setSource(jsonQuery)

// This is the bug
println requestBuilder.toString()

SearchResponse response = requestBuilder.execute().actionGet()
println response.toString()
</code></pre></div>
<p dir="auto">The expected response is one document with <code class="notranslate">id=1</code>, however, the actual response is all documents inside the index.</p>
<p dir="auto">But if you remove the bug line, the response will be correct.</p>
<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>