0

I have the following query which i am able to run through command line :

 curl -XPOST "http://35.160.73.241:9201/products/_search" -d 
                                  "{"query":{"match":{"campaign_id":"12239"}}}"

I need to run the above query using java. I am using the following code :

     try {
            Process process = Runtime.getRuntime().exec("curl -XPOST
                        \"http://35.160.73.241:9201/products/_search\" -d
                       \"{\"query\":{\"match\":{\"campaign_id\":\"12239\"}}}\"");
            int resultCode = process.waitFor();
            System.out.println(resultCode);
            if (resultCode == 0) {
                // all is good
            } 
        } catch (Exception ex) {
            // process cause
            ex.printStackTrace();
        }

But it is giving me the following exception :

java.io.IOException: Cannot run program "curl": CreateProcess 
                     error=2, The system cannot find the file specified

Please help me to run the query using Java.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213

3 Answers3

1

Why dont you just use a java client for elastic search instead of using curl. There are distinct advantages, such as being able to construct queries easily and being aware of cluster state

Monish Sen
  • 1,773
  • 3
  • 20
  • 32
0

You need to set the environment. Try this:

String env = new String[] { "PATH=/bin:/usr/bin/" };
String[] cmd = new String[] {"curl", "-XPOST", "\"http://35.160.73.241:9201/products/_search\"", "-d", "\"{\"query\":{\"match\":{\"campaign_id\":\"12239\"}}}\""}
Process process = Runtime.getRuntime().exec(cmd, env);

This will work if you're using a Linux system.

A better solution will be to use HttpURLConnection. Which is the usual way of creating HTTP connections and it's system independent.

Titus
  • 22,031
  • 1
  • 23
  • 33
  • i am using windows – Shivyesh Agnihotri Feb 16 '17 at 06:04
  • @ShivyeshAgnihotri In that case. Replace the `env` array with: `{ "curl=/path/to/curl.exe" }` or something like that. – Titus Feb 16 '17 at 06:14
  • @ShivyeshAgnihotri You can also try `Runtime.getRuntime().exec(new String[] {"A://path/to/curl.exe", "-XPOST", "\"http://35.160.73.241:9201/products/_search\"", "-d", "\"{\"query\":{\"match\":{\"campaign_id\":\"12239\"}}}\""})` – Titus Feb 16 '17 at 07:28
  • i am not getting the exception now but how will i get the result of the query ? – Shivyesh Agnihotri Feb 16 '17 at 09:55
  • @ShivyeshAgnihotri You can use `process.getInputStream()` to get the process' input stream and read from it. You can find more details [HERE](http://stackoverflow.com/a/5711150/1552587) – Titus Feb 16 '17 at 09:58
  • it is giving me something like this - java.io.BufferedInputStream@4517d9a3 – Shivyesh Agnihotri Feb 16 '17 at 10:01
  • @ShivyeshAgnihotri You need to read from the `BufferedInputStream` not print/write to file the object. Take a better look at the example I've provided in my previous commend especially the `while ((s = stdInput.readLine()) != null)....` part. – Titus Feb 16 '17 at 10:05
0

You can use QueryBuilders for query the elasticsearch. org.elasticsearch.index.query.QueryBuilders

Here are some examples: Java Code Examples for org.elasticsearch.index.query.QueryBuilders

Munish Chouhan
  • 318
  • 4
  • 10