0

I have the following curl command:

curl -XPOST 'localhost:9200/indexname/status/_search?pretty=1&size=1000000&q=date:"2012/10/10"' -d '{"_source": {"include": [ "ID", "Name" ]}}'

My java code is like this:

public static void main(String[] args) throws IOException{

ProcessBuilder pb = new ProcessBuilder("curl","-g","-H", "Accept:application/json", "-XPOST","localhost:9200/indexname/status/_search?pretty=1&size=10000","-d '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}'");

  pb.directory(new File("localpath"));
  pb.redirectErrorStream(true);
  Process p = pb.start();
  InputStream is = p.getInputStream();

  FileOutputStream outputStream = new FileOutputStream(
          "localpath/data.json");

  String line;
  BufferedInputStream bis = new BufferedInputStream(is);
  byte[] bytes = new byte[100];
  int numberByteReaded;
  while ((numberByteReaded = bis.read(bytes, 0, 100)) != -1) {

      outputStream.write(bytes, 0, numberByteReaded);
      Arrays.fill(bytes, (byte) 0);
  }
  outputStream.flush();
  outputStream.close();
 }

On executing the code, in data.json file stored in my local drive, I am seeing this error:

{
   "error" : "SearchPhaseExecutionException[Failed to execute phase [query],  all shards failed; shardFailures {[EsJClybMTnONMBSDBIfS9g][indexname][0]: SearchParseException[[indexname][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [ '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}']]]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1f7c7994; line: 1, column: 3]]; }{[EsJClybMTnONMBSDBIfS9g][indexname][1]: SearchParseException[[indexname][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [ '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}']]]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1f7c7994; line: 1, column: 3]]; }{[EsJClybMTnONMBSDBIfS9g][indexname][2]: SearchParseException[[indexname][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [ '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}']]]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1f7c7994; line: 1, column: 3]]; }{[EsJClybMTnONMBSDBIfS9g][indexname][3]: SearchParseException[[indexname][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [ '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}']]]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1f7c7994; line: 1, column: 3]]; }{[EsJClybMTnONMBSDBIfS9g][indexname][4]: SearchParseException[[indexname][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [ '{\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}']]]; nested: JsonParseException[Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@1f7c7994; line: 1, column: 3]]; }]",
 "status" : 400
}

Any idea why this is happening? I want to store the ID and Name Key value pairs from the date mentioned in the query. Any advice? Thanks.

Sweet
  • 187
  • 3
  • 15

1 Answers1

0

You can do it using the ES java client

client = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(HOST, PORT));

String queryString = "{\"size\":10,  \"query\": { \"term\": {\"date\": \"VALUE\"}},\"_source\": {\"include\": [ \"ID\", \"Name\" ]}}";
SearchResponse response = client.prepareSearch("indexname").setTypes("status").setSource(queryString)
                .execute().actionGet();

Please read java client for elasticsearch. It could be helpful for you to play with ES java api.

Rahul
  • 15,979
  • 4
  • 42
  • 63