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.

Karthikeyan
  • 1,927
  • 6
  • 44
  • 109
  • Before anything else you are suggesting to use GET with a message body. Before investing more time, read this first https://stackoverflow.com/questions/978061/http-get-with-request-body – Oleg Sklyar May 25 '18 at 07: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 – Oleg Sklyar May 25 '18 at 07:06

1 Answers1

1

You can do it like this:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryStringQueryBuilder qsQueryBuilder = new QueryStringQueryBuilder("M*"); 
qsQueryBuilder.defaultField("*");
searchSourceBuilder.query(qsQueryBuilder); 
searchRequest.source(searchSourceBuilder);
Val
  • 207,596
  • 13
  • 358
  • 360