5

Currently my query request body looks like

{
  "query": {
    "query_string": {
      "default_field": "file",
      "query": "Email OR @gmail.com @yahoo.com"
    }
  },
  "highlight": {
    "fields": {
      "file": {

      }
    }
  }
}

My java code looks like

String queryString = "{" 
                + "\"query_string\": " 
                    + "{"
                        + "\"default_field\":"
                    + " \"file\","
                            + " \"query\": \"Email OR @gmail.com @yahoo.com\""
                            + "}"
                            + "}";

with following API calls

SearchRequestBuilder searchRequestBuilder = client.prepareSearch()
                .setIndices("resume")
     .setTypes("docs").setQuery(queryString).addHighlightedField("file");

SearchResponse response = searchRequestBuilder.execute().actionGet();

I would prefer a more api based approach for "queryString" part.I am not able to find any api which handles "query_string" part of the request. There are apis for match_all,match, term so on and so forth but not for query_string

Any help would be really appreciated

1 Answer 1

9

QueryBuilders is the factory for creating any query including query_string. From documentation:

import static org.elasticsearch.index.query.QueryBuilders.*;
QueryBuilder qb = queryStringQuery("+kimchy -elasticsearch");

Your query would be built as follows:

QueryBuilder qb = queryStringQuery("Email OR @gmail.com @yahoo.com").defaultField("file");

And the full example would be:

SearchRequestBuilder searchRequestBuilder = client.prepareSearch()
            .setIndices("resume")
 .setTypes("docs").setQuery(qb).addHighlightedField("file");

SearchResponse response = searchRequestBuilder.execute().actionGet();
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this , but somehow was getting ParseException, Can you please take a look at provided JSON and update what it might look like with that particular json
Can you add that ParseException to your question so that we can know more about your problem?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.