Spring Framework - Spring Boot - Spring Boot + Spring Data Elasticsearch
Start
Spring boot and spring data elasticsearch integration
In this example we are going to see a maven based spring boot application which integrates spring-data-elasticsearch. Here, we will do the followings and see the respective code segments.
Insert a Greeting(id, username, message) item on elasticsearch.
We will use the default cluster name, host and port. By default, 9300 port is used as transport port and 9200 port is known as http port. To see the default cluster name hit http://localhost:9200/(opens new window).
@SpringBootApplication is a combination of @Configuration, @EnableAutoConfiguration, @EnableWebMvc and @ComponentScan annotations. The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. There we don't need any xml configuration, this application is pure java spring application.
ElasticsearchConfig class configures elasticsearch to this project and make a connection with elasticsearch. Here, @PropertySource is used to read the application.properties file where we store the cluster name, elasticsearch host and port. @EnableElasticsearchRepositories is used to enable Elasticsearch repositories that Will scan the packages of the annotated configuration class for Spring Data repositories by default. @Value is used here for reading the properties from the application.properties file.
The Client() method creates a transport connection with elasticsearch.
The configuration above sets up an Embedded Elasticsearch Server which is used by the ElasticsearchTemplate. The ElasticsearchTemplate bean uses the Elasticsearch Client and provides a custom layer for manipulating data in Elasticsearch.
Here we have annotated our Greeting data objects with a @Document annotation that we can also use to determine index settings like name, numbers of shards or number of replicas. One of the attributes of the class needs to be an id, either by annotating it with @Id or using one of the automatically found names id or documentId. Here, id field value will be auto-generated, if we don't set any value of id field.
Here, We have extended ElasticsearchRepository which provide us many of apis that we don't need to define externally. This is the base repository class for elasticsearch based domain classes. Since it extends Spring based repository classes, we get the benefit of avoiding boilerplate code required to implement data access layers for various persistence stores.
Here we have declared a method findByUsername(String username) which will convert to a match query that matches with username with the username field of Greeting objects and returns the list of results.
In above class, we have @Autowired the GreetingRepository. We can simply call the CRUDRepository methods and the method we have declared in repository class with the GreetingRepository object.
In getAll() method, you may find a line Lists.newArrayList(repository.findAll()). We have done this to convert repository.findAll() to List<> item as it returns a Iterable List.
Above command first remove all the files in the target folder and build the project. After building the project we will get the executable .jar file which is named springdataes-1.0-SNAPSHOT.jar. We can run the main class(Application.java) to start the process or simply executing the above jar by typing:
java -jar springdataes-1.0-SNAPSHOT.jar
Checking the APIs
For inserting a Greeting item in elasticsearch, execute the below command
curl -H"Content-Type: application/json"-X POST -d '{"username":"sunkuet02","message": "this is a message"}' http://localhost:8080/api/greetings
You should get the below result like
{"id":"AV2ddRxBcuirs1TrVgHH","username":"sunkuet02","message":"this is a message"}
You can also check the get api by executing:
curl -H"Content-Type: application/json"-X GET http://localhost:8080/api/greetings
You should get
[{"id":"AV2ddRxBcuirs1TrVgHH","username":"sunkuet02","message":"this is a message"}]
You can check other apis by following the above processes.
Spring Boot and Spring Data Elasticsearch integration
In this example we are going to implement spring-data-elasticsearch project to store POJO in elasticsearch. We will see a sample maven project which does the followings:
Insert a Greeting(id, username, message) item on elasticsearch.