MatchQuery using java rest

anyone have an example of how to use a match query, with fuzziness, using the java rest api.
vesion 5.6.3(current).

The ES documentation is a bit confusing.

I can perform a reqular search no problem:

String encodedBytes = Base64.getEncoder().encodeToString("username:password".getBytes());
	Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
			new BasicHeader("Authorization", "Basic " + encodedBytes) };

	RestClient r = RestClient
			.builder(new HttpHost("xxxxxxxxxxxxxxxxx", 9243, "https"))
			.setDefaultHeaders(headers).build();

Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("q", "_all:sean");
		paramMap.put("pretty", "true");
		

		Response response = r.performRequest("GET", "/*/_search", paramMap);
		System.out.println(EntityUtils.toString(response.getEntity()));
		System.out.println("Host -" + response.getHost());
		System.out.println("RequestLine -" + response.getRequestLine());
		System.out.println(response.toString());

I figured out how to perform the fuzzy match using kibana:

GET index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "firstname": {
               "query": "Mik",
        "fuzziness": "AUTO"
            }
            
          }
        },
        {
          "match": {
            "lastname": {
        "query": "Smit",
        "fuzziness": "AUTO"
      }
          }
        }
      ]
    }
  }
}

How can I turn this into an HttpEntity String?
I'm having trouble formatting the string.

Problem Solved.
Putting together the string HttpEntity is a bit painful, but it works:

String firstname ="mik";
		String lastname="smit";
		HttpEntity entity = new NStringEntity(
"{"+
  "\"query\": {"+
  " \"bool\": {"+
     "\"must\": ["+
       "{"+
          "\"match\": {"+
            "\"firstname\": {"+
               "\"query\": \""+firstname+"\","+
        "\"fuzziness\": \"AUTO\""+
            "}"+
            
          "}"+
        "},"+
        "{"+
          "\"match\": {"+
            "\"lastname\": {"+
        "\"query\": \""+lastname+"\","+
        "\"fuzziness\": \"AUTO\""+
      "}"+
         "}"+
        "}"+
      "]"+
    "}"+
  "}"+
"}");
		
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("pretty", "true");
		

		Response response = r.performRequest("GET", "/*/_search", paramMap,entity);
		System.out.println(EntityUtils.toString(response.getEntity()));
		System.out.println("Host -" + response.getHost());
		System.out.println("RequestLine -" + response.getRequestLine());
		System.out.println(response.toString());
		
		//////////
		try {
			r.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

I was able to figure out how to use objects instead of the HttpEntity String:

String encodedBytes = Base64.getEncoder().encodeToString("username:password".getBytes());
    		Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
    				new BasicHeader("Authorization", "Basic " + encodedBytes) };
RestClient restLowLevelClient = RestClient.builder(
    		        new HttpHost("url_elasticsearch_endpoint", 9243, "https")).build();
    		
    		
RestHighLevelClient client = new RestHighLevelClient(restLowLevelClient);
/////////////////////////////////////////////////////////////////////////////////////////////////////////
    		
    		
    		
    		/*
    		 * Build the query
    		 */
MatchQueryBuilder matchFirstname = new MatchQueryBuilder("firstname",firstname).fuzziness(Fuzziness.AUTO);
MatchQueryBuilder matcLastname = new MatchQueryBuilder("lastname",lastname).fuzziness(Fuzziness.AUTO);
    		BoolQueryBuilder qb = boolQuery()
    				.must(matchFirstname)
    				.must(matcLastname);
    			
  	
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(qb);
    		
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(sourceBuilder);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
    		
    		
SearchResponse response = null;
    	try {
    		response = client.search(searchRequest, headers);
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    		
System.out.println(response);
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.