Spring Boot 2.0.5 Integration with Elasticsearch: Installation, Configuration, Entity Mapping, CRUD, and Full‑Text Search

This guide demonstrates how to integrate Spring Boot 2.0.5 with Elasticsearch 5.6.11 by installing Elasticsearch and Kibana, adding the required Maven dependencies, configuring the client via application.yml or Java code, defining @Document‑annotated entities, implementing repository, service and controller layers for CRUD and full‑text search, handling Netty conflicts at startup, and verifying the setup with health, list and search endpoints.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Spring Boot 2.0.5 Integration with Elasticsearch: Installation, Configuration, Entity Mapping, CRUD, and Full‑Text Search

This article provides a step‑by‑step tutorial for integrating Spring Boot 2.0.5 (springBootVersion = '2.0.5.RELEASE') with Elasticsearch 5.6.11.

Installation : Download Elasticsearch and Kibana from the official Elastic website.

Dependencies (add to build.gradle or pom.xml):

springBootVersion = '2.0.5.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-elasticsearch'
// Ensure the transport client version matches the Elasticsearch version
compile 'org.elasticsearch.client:transport:5.6.11'

Configuration can be done either in application.yml:

spring:
  data:
    elasticsearch:
      cluster-name: springboot-elasticsearch
      cluster-nodes: 127.0.0.1:9300
      repositories:
        enabled: true

or programmatically with a Kotlin configuration class:

@Configuration
@EnableElasticsearchRepositories(basePackages = ["com.futao.springmvcdemo.dao"])
open class ElasticSearchConfiguration {
    @Bean
    fun client(): TransportClient {
        val node = InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)
        val settings = Settings.builder()
            .put("cluster.name", "springboot-elasticsearch")
            .build()
        return PreBuiltTransportClient(settings).addTransportAddress(node)
    }
}

Entity Mapping uses the @Document annotation to map a Kotlin/Java class to an Elasticsearch index and type:

@Document(indexName = "futao", type = "article")
class Article : BaseEntity() {
    private String title;
    private String description;
    private String content;
    // getters and setters omitted for brevity
}

DAO Layer defines an interface extending ElasticsearchRepository:

interface ArticleSearchDao : ElasticsearchRepository<Article, String>

Service Layer saves articles and performs full‑text search using QueryBuilders:

@Service
open class ArticleServiceImpl : ArticleService {
    @Resource
    lateinit var elasticsearch: ArticleSearchDao
    @Resource
    lateinit var client: Client

    override fun list(): List<Article> {
        val list = articleDao.list()
        client.saveAll(list)
        return list
    }

    override fun search(key: String): ArrayList<Article> {
        val hits = client.prepareSearch("futao")
            .setTypes("article")
            .setQuery(QueryBuilders.boolQuery()
                .should(QueryBuilders.matchQuery(Article::getContent.getFieldName(), key))
                .should(QueryBuilders.matchQuery(Article::getTitle.getFieldName(), key))
                .should(QueryBuilders.matchQuery(Article::getDescription.getFieldName(), key)))
            .execute().actionGet().hits
        val result = arrayListOf<Article>()
        hits.forEach { result.add(JSONObject.parseObject(it.sourceAsString, Article::class.java)) }
        return result
    }
}

Controller Layer exposes REST endpoints for adding, listing, and searching articles:

@RestController
@RequestMapping(path = "article", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
class ArticleController {
    @Resource
    lateinit var articleService: ArticleService

    @PostMapping("add")
    fun add(@RequestParam("title") title: String,
            @RequestParam("desc") desc: String,
            @RequestParam("content") content: String): SingleValueResult {
        articleService.add(title, desc, content)
        return SingleValueResult("success")
    }

    @GetMapping("list")
    fun list(): List<Article> = articleService.list()

    @GetMapping("search")
    fun search(@RequestParam("key") key: String): List<Article> = articleService.search(key)
}

Startup Fix for Netty Conflict : Before running the application, set the system property to disable Netty's processor detection:

System.setProperty("es.set.netty.runtime.available.processors", "false")

Optionally enable FastJSON auto‑type support for Redis deserialization: ParserConfig.getGlobalInstance().setAutoTypeSupport(true) Testing : After starting the Spring Boot application, you can verify the health endpoint, use the /article/list endpoint to insert data into Elasticsearch, view the data in Kibana, and perform full‑text searches via /article/search?key=.... The stored data resides under Elasticsearch’s data directory, which can be cleared to delete all indices.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javabackend-developmentElasticsearchKotlinSpring BootFull‑Text Search
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.