ElasticSearch 是一个分布式搜索服务,提供 Restful API,底层基于 Lucene,采用多 shard(分片)的方式保证数据安全,并提供自动 resharding 的功能
ElasticSearch对比MYSQL
索引》数据库 类型》表文档》行属性》列
DOCKER创建
// docker 创建 ES 容器
// -e ES_JAVA_OPTS="-Xms256m -Xmx256m" 限制 ES 内存使用大小
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES 镜像 id
依赖
// spring boot默认使用springData ES模块进行操作
org.springframework.boot
spring-boot-starter-data-elasticsearch
配置
spring:
elasticsearch:
rest:
uris: http://localhost:9200
data:
elasticsearch:
repositories:
enabled: true
client:
reactive:
endpoints: http://localhost:9200
实现
@Autowired
RestHighLevelClient restHighLevelClient ;
public void saveOne()throws IOException{
IndexRequest indexRequest = new IndexRequest("city");
City city = new City(8,"武汉",0,1);
indexRequest.id(city.getCid()+"");// 指定ID
indexRequest.source(JSON.toJSonString(city), XContentType.JSON); // 转换为JSON格式
// 执行
restHighLevelClient.index(indexRequest , RequestOptions.DEFAULT);
}
public void saveAll() throws IOException{
ArrayList
Elasticsearch官网https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html