How to send a query as String with the Java API Rest

Hello, I'm trying to send a Update request using a Query as String, but to write an query in JSON format in java, I have to use ", but the parser can't understand this characters, what could I do?

For example, having this query:

String queryString = "\"query\": {\"match_all\": {}}";

When I use this:

searchSourceBuilder.query(QueryBuilders.queryStringQuery(queryString));

I get that error:

"reason":"Cannot parse '"query": {"match_all": {}}': Encountered " ":" ": "" at line 1, column 7.\r\nWas expecting one of:\r\n .....

The query string query is not meant for that.
May be you can do something like:

searchSourceBuilder.source(json);

?

i'll try that, thanks

There's no such function at searchSourceBuilder

at least, not in 7.2 java api rest

Any similar one? Can't really check on my phone ATM.

i'll check something like that

You can do something like:

client.search(new SearchRequest("test").source(
        new SearchSourceBuilder().query(
                QueryBuilders.wrapperQuery("{\"match_all\":{}}")
        )
), RequestOptions.DEFAULT);
1 Like

Thanks, it worked.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.