How to Use Java High Level REST Client

a snapshot of the repository application

As you know, ElasticSearch is an open source full-text search engine and data analysis tool. Its development process is at dizzying speed, and the latest 6.0.0 version of the ElasticSearch has been released. Upon this, I have updated my repository according to the Java API changes.

I tried to show you how to use the high level rest client in the backend. The client added in version 6.0.0-beta1 and it works on top of the Java low level rest client. Some examples:

Creating an index

IndexRequest request = new IndexRequest(props.getIndex().getName(), props.getIndex().getType());
request.source(gson.toJson(document), XContentType.JSON);
IndexResponse response = client.index(request);

Using SearchSourceBuilder and showing search results

sourceBuilder.query(builder);
SearchRequest searchRequest = getSearchRequest();

SearchResponse searchResponse = client.search(searchRequest);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
Document doc = gson.fromJson(hit.getSourceAsString(), Document.class);
doc.setId(hit.getId());
result.add(doc);
}

Using wildcard query

QueryBuilders.wildcardQuery("_all", "*" + query.toLowerCase() + "*")

Deleting a document

DeleteRequest deleteRequest = new DeleteRequest(props.getIndex().getName(), props.getIndex().getType(), id);

Other details are in the repository.