0

Am fetching records from elasticsearch from java code, am able to fetch records with the elasticsearch _id. For that am using the below java code.

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("_id", id); 
    searchSourceBuilder.query(matchQueryBuilder); 
    searchRequest.source(searchSourceBuilder);

From Kibana am using the below query to fetch records from elasticsearch,

GET /_search
{
    "query": {
        "query_string" : {
            "default_field" : "*",
            "query" : "M*"
        }
    }
}

Now, i want to build this query in java., Am not sure how i can build this query in java.

2
  • Before anything else you are suggesting to use GET with a message body. Before investing more time, read this first stackoverflow.com/questions/978061/http-get-with-request-body Commented May 25, 2018 at 7:04
  • Beyond the above, the body is a JSON structure. So you need some mechanism of constructing a JSON string (manually or serializing a matching DTO) and a library providing an HTTP client to make a request (Java standard library, httpclient from Apache, vert.x etc). Finally, the task has really nothing to do with kibana: you are querying elastic no matter if your request copies the one from Kibana Commented May 25, 2018 at 7:06

1 Answer 1

1

You can do it like this:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryStringQueryBuilder qsQueryBuilder = new QueryStringQueryBuilder("M*"); 
qsQueryBuilder.defaultField("*");
searchSourceBuilder.query(qsQueryBuilder); 
searchRequest.source(searchSourceBuilder);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot again.. :)

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.