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.
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: trueor 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
