Springboot整合elasticsearch以及接口开发
搭建elasticsearch集群
搭建过程略(我这里用的是elasticsearch5.5.2版本)
写入测试数据
新建索引book(非结构化索引)
PUT http://192.168.100.102:9200/book
修改mapping(结构化索引)
POST http://192.168.100.102:9200/book/novle/_mappings
{
"novel": {
"properties": {
"word_count": {"type": "integer"},
"author": {"type": "keyword"},
"title": {"type": "text"},
"publish_date": {"type": "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
}
}
}
查看新建索引结果(我这里使用的是elasticsearch-head插件)
测试数据写入
PUT http://192.168.100.102:9200/book/novel/1
{
"word_count": 1000000,
"author": "金庸",
"title": "神雕侠侣",
"publish_date": "1997-10-1"
}
查看数据写入结果:
Springboot整合elasticsearch
新建springboot项目
目录结构
Pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!--<version>2.1.1.RELEASE</version>--> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ylht</groupId> <artifactId>es-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>es-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <es.version>5.5.2</es.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>${es.version}</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>${es.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
log4j2.properties
appender.console.type=Console
appender.console.name=console
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=[%t] %-5p %c %m%n
rootLogger.level=info
rootLogger.appenderRef.console.ref=console
MyConfig类
package com.ylht.esdemo; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.net.InetAddress; import java.net.UnknownHostException; @Configuration public class MyConfig { @Bean public TransportClient client() throws UnknownHostException { InetSocketTransportAddress es1 = new InetSocketTransportAddress( InetAddress.getByName("192.168.100.101"), 9300 ); InetSocketTransportAddress es2 = new InetSocketTransportAddress( InetAddress.getByName("192.168.100.102"), 9300 ); InetSocketTransportAddress es3 = new InetSocketTransportAddress( InetAddress.getByName("192.168.100.103"), 9300 ); Settings settings = Settings.builder() .put("cluster.name", "aubin-cluster") .build(); TransportClient client = new PreBuiltTransportClient(settings); client.addTransportAddresses(es1, es2, es3); return client; } }
EsDemoApplication类文件
package com.ylht.esdemo; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.transport.TransportClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.xml.ws.Response; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) @RestController public class EsDemoApplication { @Autowired private TransportClient client; @GetMapping("/") public String index() { return "index"; } @GetMapping(value = "/get/book/novel") public ResponseEntity get(@RequestParam(name = "id", defaultValue = "") String id) { try { if (id.isEmpty()) { return new ResponseEntity(HttpStatus.NOT_FOUND); } GetResponse response = this.client.prepareGet("book", "novel", id) .get(); if (!response.isExists()) { return new ResponseEntity(HttpStatus.NOT_FOUND); } return new ResponseEntity(response.getSource(), HttpStatus.OK); } catch (Exception e) { e.printStackTrace(); return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); } } public static void main(String[] args) { SpringApplication.run(EsDemoApplication.class, args); } }
启动项目
(启动项目之前最好先 mvn clean,mvn package,mvn install)
启动方式有多种
- 直接点击绿色三角按钮运行(它右边一个是debug运行)
- 直接运行main方法
- 点击Teminal,输入mvn spring-boo:run
运行出错可以看看我的博客https://i.cnblogs.com/posts?categoryid=1373086
项目正常启动后
接口测试
我这里使用的ponstman
GET 192.168.100.53:8080/get/book/novel?id=1
结果图