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.

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

7 Comments

i am using windows
@ShivyeshAgnihotri In that case. Replace the env array with: { "curl=/path/to/curl.exe" } or something like that.
@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\"}}}\""})
i am not getting the exception now but how will i get the result of the query ?
@ShivyeshAgnihotri You can use process.getInputStream() to get the process' input stream and read from it. You can find more details HERE
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.