/ElasticSearch-1

:globe_with_meridians: This application illustrates and demonstrates use of ElasticSearch Java API in the backend

Primary LanguageJava

Build Status "Docker Pulls Coverage Status Analytics

Illustration and demonstration use of ElasticSearch

This repository illustrates and demonstrates the use of ElasticSearch Java API with most up to date version of ElasticSearch which provides Java High Level REST Client. If you want to see the sample of the old version, please visit the oldVersion branch.

How to Use Java 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.

Initialization

RetHighLevelClient(RestClient.builder(new HttpHost(props.getRestClient().getHostname(),
                props.getRestClient().getPort(), props.getRestClient().getScheme())));

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

How to run?

mvn spring-boot:run

How to run with Docker?

docker run -d --name elasticsearch -p 8080:8080 hakdogan/elasticsearch:newVersion