Hibernate Search is a library that allows keeping your local Apache Lucene
indexes or ElasticSearch
cluster in sync with your data that extracts from Hibernate ORM
based on your domain model. This repository was created for an article published in the Java Advent Calendar
and shows you how to use Hibernate Search in a concrete example.
- JDK 1.8 or later
- Maven 3.x.x
- Docker for Elasticsearch and PostgreSQL
The entities of the application are stored in PostgreSQL
and indexed in ElasticSearch
so we need instances of them.
docker run --rm --name postgresql \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=jugistanbul \
-p 5432:5432 \
postgres:${POSTGRESQL_VERSION}
docker run --rm --name elasticsearch \
-e "discovery.type=single-node" \
-p 9200:9200 \
docker.elastic.co/elasticsearch/elasticsearch-oss:${ELASTICSEARCH_VERSION}
http -v GET localhost:8080/search/event/quarkus
GET /search/event/Quarkus HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8080
User-Agent: HTTPie/2.3.0
HTTP/1.1 200 OK
Content-Length: 43
Content-Type: application/json
[
{
"id": 3,
"name": "Introduction to Quarkus"
}
]
http -v GET localhost:8080/search/host/title/consultion
GET /search/host/title/consultion HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8080
User-Agent: HTTPie/2.3.0
HTTP/1.1 200 OK
Content-Length: 187
Content-Type: application/json
[
{
"events": [
{
"id": 2,
"name": "CI & CD with Azure Devops"
},
{
"id": 3,
"name": "Introduction to Quarkus"
}
],
"id": 2,
"firstname": "Huseyin",
"lastname": "Akdogan",
"title": "Expert Software Consultant"
}
]
http -v GET localhost:8080/search/event/quar
GET /search/event/Quar HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8080
User-Agent: HTTPie/2.3.0
HTTP/1.1 404 Not Found
Content-Length: 91
Content-Type: application/json
{
"code": 404,
"error": "No event found",
"exceptionType": "javax.ws.rs.WebApplicationException"
}
mvn quarkus:dev
mvn verify
How to Keep Elasticsearch in Sync with Relational Databases?