user3746601 user3746601 - 4 months ago 48
Java Question

Elasticsearch URI based search query without source fields

I am trying to execute a URI based query like below. But i don't see any effect of the search parameters i am passing. And also i want to disable _source=false. Can you help me with the query. I am using java to perform the same using Java.net packages. This query return everything no matter what search parameter i pass.

URI: "http://localhost:9200/twitter/twitter/_search?updatedBy=XX&node=YY"

Java Code:

String charset = "UTF-8";
String updatedBy = "XX";
String node = "YY";

String query = String.format("updatedBy:%s&nodeId:%s",
URLEncoder.encode(updatedBy, charset),
URLEncoder.encode(nodeId, charset));
System.out.println(searchEsUrl + "?q=" + query);

URLConnection connection = new URL(searchEsUrl + "?q=" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);

Val Val
Answer

Try this instead

String query = String.format("updatedBy:%s+AND+nodeId:%s",
        URLEncoder.encode(updatedBy, charset),
        URLEncoder.encode(nodeId, charset));
URLConnection connection = new URL(searchEsUrl + "?_source=false&q=" + query).openConnection();

The issue was that whatever is sent in the q= parameter needs to follow the query string query syntax rules and two constraints are ANDed using the AND operator not & like this: updatedBy:XX AND nodeId:YY

The URI you want to construct is thus:

"http://localhost:9200/twitter/twitter/_search?q=updatedBy:XX AND nodeId:YY"