In this post, we will show step by step how to create a sample application that add indexes content in an Elastic search instance and retrieve this content.
We will use Kotlin, Spring Boot and Elastic search Docker installation.

1. Run elastic search instance

Launch Elastic Search in Docker as following:

docker pull docker.elastic.co/elasticsearch/elasticsearch:7.5.2

docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.5.2

2. Bootstrap a spring boot application

Go to the spring start io website: Elastic search project bootstrap

3. Add code for indexing and retrieving indexes in Elastic search

3.1 Add Spring boot Autoconfiguration class

@Configuration
class ElasticSearchConfig: AbstractElasticsearchConfiguration() {

    @Bean
    override fun elasticsearchClient(): RestHighLevelClient? {
        val clientConfiguration = ClientConfiguration.builder()
                .connectedToLocalhost()
                //.connectedTo("localhost:9200")
                .build()
        return RestClients.create(clientConfiguration).rest()
    }

    @Bean
    override fun entityMapper(): EntityMapper? {
        val entityMapper = ElasticsearchEntityMapper(
                elasticsearchMappingContext(), DefaultConversionService()
        )
        entityMapper.setConversions(elasticsearchCustomConversions())
        return entityMapper
    }
}

3.2 Add Data class to index

@Document(indexName = "demo")
data class Content(@Id val id: Long, val text: String)

3.3 Add indexing and retrieving index code

@RestController
@RequestMapping("/demo")
class ContentController(private val elasticsearchOperations: ElasticsearchOperations,
                        private val indexationService: IndexationService) {

    @PostMapping("/content")
    fun save(@RequestBody content: Content): String? {
        val indexQuery = IndexQueryBuilder()
                .withId(content.id.toString())
                .withObject(content)
                .build()
        return elasticsearchOperations.index(indexQuery)
    }

    @GetMapping("/content/{id}")
    fun findById(@PathVariable("id") id: Long): Content? {
        return elasticsearchOperations
                .queryForObject(GetQuery.getById(id.toString()), Content::class.java)
    }

    @PostMapping("/request-index")
    fun requestIndex(@RequestBody content: Content): String? {
        return indexationService.requestIndex().toString()
    }

}

4. Call the indexing and retrieving index endpoints

Use Postman or simply Curl to call the following endpoints:

    Post
http://localhost:8080/demo/content/
  {
   "id": 1,
   "text": "test"
  }
    Get
http://localhost:8080/demo/content/1

5. Conclusion

In this article, we have showed how bootstrapping an application that allows indexation with Spring Boot and Elastic search is easy.
The code of this sample application is shared in my Github: here
Thanks to this base, we are now able to add a search engine in our applications.

Leave a comment

I’m Riadh

Welcome to my technical blog !

Let’s connect