How to create a query with query_string using Elasticsearch Java Api -


currently query request body looks

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

my java code looks

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 prefer more api based approach "querystring" part.i not able find api handles "query_string" part of request. there apis match_all,match, term on , forth not query_string

any appreciated

querybuilders factory creating query including query_string. documentation:

import static org.elasticsearch.index.query.querybuilders.*; querybuilder qb = querystringquery("+kimchy -elasticsearch"); 

your query built follows:

querybuilder qb = querystringquery("email or @gmail.com @yahoo.com").defaultfield("file"); 

and full example be:

searchrequestbuilder searchrequestbuilder = client.preparesearch()             .setindices("resume")  .settypes("docs").setquery(qb).addhighlightedfield("file");  searchresponse response = searchrequestbuilder.execute().actionget(); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -