1,下载 Elasticsearch
官网下载地址 https://www.elastic.co/cn/downloads/elasticsearch 作者这里下载的是7.2.0 ,如果想下载之前版本 可以点击如下图下载:
2,启动Elasticsearch
1>进入bin目录点击 elasticsearch.bat 启动 Elasticsearch
访问 http://127.0.0.1:9200/ 可以看到如下图信息:
3,在springboot pom.xml 中引入依赖包
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.2.0</version>
</dependency>
4,在application.yml 配置属性文件
spring:
es:
host: 127.0.0.1
port: 9300
5,注入 加载配置文件 并且注入 TransportClient
import lombok.Data;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Data
@Component
@ConfigurationProperties(prefix = "spring.es")
public class ElasticsearchConfig {
private String host;
private Integer port;
@Bean
public TransportClient client() throws UnknownHostException {
// 设置es节点的配置信息
Settings settings = Settings.builder()
.put("cluster.name", "myClusterName")
.build();
// 实例化es的客户端对象
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
return client;
}
}
6,新增(index)查看接口实现(get)
import io.swagger.annotations.Api;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Date;
@RestController
@RequestMapping("/es")
public class ElasticsearchController {
@Autowired
private TransportClient client;
/**
* 添加书籍数据
* @param title 书籍标题
* @param author 书籍作者
* @param wordCount 书籍字数
* @param publishDate 发行时间
* @return
*/
@PostMapping("/add/book/novel")
public ResponseEntity add(@RequestParam("title") String title,
@RequestParam("author") String author,
@RequestParam("word_count") int wordCount,
@RequestParam("publish_date")
@DateTimeFormat(pattern = "yyy-MM-dd HH:mm:ss")
Date publishDate) {
try {
// 将参数build成一个json对象
XContentBuilder content = XContentFactory.jsonBuilder()
.startObject()
.field("title", title)
.field("author", author)
.field("word_count", wordCount)
.field("publish_date", publishDate.getTime())
.endObject();
IndexResponse response = client.prepareIndex("books", "book","1")
.setSource(content)
.get();
return new ResponseEntity(response.getId(), HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}finally {
client.close();
}
}
/**
* 按id查询
* @param id
* @return
*/
@GetMapping("/get/book/novel")
public ResponseEntity searchById(@RequestParam("id") String id) {
if (id.isEmpty()) {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
// 通过索引、类型、id向es进行查询数据
GetResponse response = client.prepareGet("books", "book", id).get();
if (!response.isExists()) {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
// 返回查询到的数据
return new ResponseEntity(response.getSource(), HttpStatus.OK);
}
}
作者这里集成了swagger 所以结果如下:
修改删除等:可以参考官方API: 官方Api地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.3/index.html
7,head插件
参考:https://www.cnblogs.com/hts-technology/p/8477258.html
最后作者成功集成附图: