# MatchQuery using java rest

**URL:** <https://discuss.elastic.co/t/matchquery-using-java-rest/107250>\
**Category:** Elasticsearch\
**Created:** [November 10, 2017, 9:38pm UTC](https://discuss.elastic.co/t/matchquery-using-java-rest/107250 "2017-11-10T21:38:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 10, 2017, 9:38pm UTC](https://discuss.elastic.co/t/matchquery-using-java-rest/107250/1 "2017-11-10T21:38:38Z")

</div>

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());
```

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 11, 2017, 11:48pm UTC](https://discuss.elastic.co/t/matchquery-using-java-rest/107250/2 "2017-11-11T23:48:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 12, 2017, 2:57am UTC](https://discuss.elastic.co/t/matchquery-using-java-rest/107250/3 "2017-11-12T02:57:57Z")

</div>

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();
		}
	}
```

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 13, 2017, 3:43pm UTC](https://discuss.elastic.co/t/matchquery-using-java-rest/107250/4 "2017-11-13T15:43:33Z")

</div>

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);
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 11, 2017, 3:43pm UTC](https://discuss.elastic.co/t/matchquery-using-java-rest/107250/5 "2017-12-11T15:43:39Z")

</div>

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