mc1392
November 10, 2017, 9:38pm
1
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());
mc1392
November 11, 2017, 11:48pm
2
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.
mc1392
November 12, 2017, 2:57am
3
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();
}
}
mc1392
November 13, 2017, 3:43pm
4
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
system
(system)
Closed
December 11, 2017, 3:43pm
5
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.