Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an Elasticsearch query with a JSON in Java?

I am trying to make an Elasticsearch search with Java and Elasticsearch. Elastic search provides API for Java and this is cool.

The thing is, I wish to create a method in Java who receives a string (properly, a JSON containing information for searching) who reflects this HTTP call to Elasticsearch

curl -X GET 'http://localhost:9200/geocon/_search?q=first:"Sonya"'

But I want this method as much general as possible.

So the question is: is it possible to parse and translate 1:1 HTTP request to Elasticsearch with Java API? Maybe giving to the Elasticsearch object just the JSON string as parameter?

like image 430
BlacK Avatar asked Mar 06 '15 20:03

BlacK


2 Answers

Check this out. Maybe this post can help you:

How to construct QueryBuilder from JSON DSL when using Java API in ElasticSearch?

like image 166
fct Avatar answered Oct 03 '22 06:10

fct


Basically you need to do something like this:

String query = whatever_query...
SearchResponse response = esClient.prepareSearch(Configuration.PRIMARY_INDEX_NAME)
                    .setQuery(query)
                    .execute()
                    .actionGet();
like image 41
BlacK Avatar answered Oct 03 '22 06:10

BlacK