3

I am using Elasticsearch maven jar file to query Elasticsearch. But now I want to query the elasticsearch using full generated query string:

query :

{
  "bool" : {
    "must" : [ {
      "term" : {
        "title" : "mercedes"
      }
    }, {
      "term" : {
        "Doors" : "2"
      }
    } ]
  }
}

How do I use the above query string to query elasticsearch in java?

2 Answers 2

4

Following code prepares a boolquery. You should create a SearchRequestBuilder to execute it.

BoolQueryBuilder boolQuery = new BoolQueryBuilder();
boolQuery.must(QueryBuilders.termQuery("title", "mercedes"));
boolQuery.must(QueryBuilders.termQuery("Doors", "2"));

If you want to use query as string without building it in code, you can use following;

String myQuery = "Your Query Here";
SearchSourceBuilder ssb= new SearchSourceBuilder();
search.query(myQuery);

SearchRequestBuilder srb; // You should define srb before next steps
srb.internalBuilder(ssb);
SearchResponse response = srb.execute().actionGet();
Sign up to request clarification or add additional context in comments.

Comments

1

I use the following code to query Elasticsearch using JSON string.

SearchResponse response = client.prepareSearch(yourIndexName)
            .setSource(yourJsonQueryString)
            .execute().actionGet();

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.