es安装配置
https://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
macbook 安装出现 【无法打开“XXXX”,因为Apple无法检查其是否包含恶意软件。怎么解决?】 https://cloud.tencent.com/developer/article/1918927
springboot配置elasticsearch客户端
elasticsearch服务 7.6.0 版本 restHighLevelClient客户端版本7.6.0
创建spring web项目引入依赖: 父项目引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
spring-boot项目引入elasticsearch:
引入elasticsearch客户端 restHighLevelClient:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.0</version>
<exclusions>
<exclusion>
<artifactId>elasticsearch-rest-client</artifactId>
<groupId>org.elasticsearch.client</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.6.0</version>
</dependency>
<!--ES依赖-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.0</version>
</dependency>
客户端配置:
@Configuration
public class config {
@PostConstruct
public void init(){
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
public String hostname="localhost";
public int port=9200;
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
new HttpHost(hostname, port)));
return restHighLevelClient;
}
}
安装ik分词器
https://release.infinilabs.com/analysis-ik/stable/
https://blog.csdn.net/weixin_43971836/article/details/122213893
创建 目录将ik解压,/Users/liulei318/Documents/goldstine_workspace/application/es/elasticsearch-7.6.0/plugins/ik
删除.DS_Store目录。
重启es,加在ik分词器。
安装kibana
安装对应版本的kibana, https://www.elastic.co/cn/downloads/past-releases#kibana
/etc/hosts 配置 127.0.0.1 localhost 启动kibana。 ./bin/kibana
访问: localhost:5601
查看创建的索引:
配置kibana: /kibana/config/kibana.yml文件 :
ES 索引
创建索引
查看当前所有索引: http://localhost:9200/_cat/indices?v
创建索引:
1、创建mapping:
{
"properties": {
"brandName": {
"type": "keyword"
},
"categoryName": {
"type": "keyword"
},
"createTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"id": {
"type": "long"
},
"price": {
"type": "double"
},
"saleNum": {
"type": "integer"
},
"status": {
"type": "integer"
},
"stock": {
"type": "integer"
}
}
}
public boolean indexCreate() throws IOException {
CreateIndexRequest indexRequest = new CreateIndexRequest("goods");
String mapping = "{\n" +
" \"properties\": {\n" +
" \"brandName\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"categoryName\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"createTime\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"yyyy-MM-dd HH:mm:ss\"\n" +
" },\n" +
" \"id\": {\n" +
" \"type\": \"long\"\n" +
" },\n" +
" \"price\": {\n" +
" \"type\": \"double\"\n" +
" },\n" +
" \"saleNum\": {\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"status\": {\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"stock\": {\n" +
" \"type\": \"integer\"\n" +
" }\n" +
" }\n" +
"}";
// 4、 设置索引的别名
// 5、 发送请求
// 5.1 同步方式发送请求
IndicesClient indicesClient = restHighLevelClient.indices();
indexRequest.mapping("_doc",mapping, XContentType.JSON);
// 请求服务器
CreateIndexResponse response = indicesClient.create(indexRequest, RequestOptions.DEFAULT);
return response.isAcknowledged();
}
获取mapping信息:
@Test
public void getMapping() {
try {
Map<String, Object> indexMap = indexTestService.getMapping("goods");
// 将bean 转化为格式化后的json字符串
String pretty1 = JSON.toJSONString(indexMap, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
log.info("索引信息:{}", pretty1);
} catch (Exception e) {
log.error("获取索引失败,错误信息:" ,e);
}
}
@Override
public Map<String, Object> getMapping(String indexName) throws IOException {
IndicesClient indicesClient = restHighLevelClient.indices();
// 创建get请求
GetIndexRequest request = new GetIndexRequest();
request.indices(indexName);
// 发送get请求
GetIndexResponse response = indicesClient.get(request, RequestOptions.DEFAULT);
// 获取表结构
ImmutableOpenMap<String, MappingMetaData> immutableOpenMap = response.getMappings().get(indexName);
Map<String, Object> sourceAsMap = immutableOpenMap.get("_doc").getSourceAsMap();
return sourceAsMap;
}
判断索引是否存在
@Override
public boolean indexExists(String indexName) throws IOException {
IndicesClient indicesClient = restHighLevelClient.indices();
// 创建get请求
GetIndexRequest request = new GetIndexRequest();
request.indices(indexName);
// 判断索引库是否存在
boolean result = indicesClient.exists(request, RequestOptions.DEFAULT);
return result;
}
删除索引
文档操作
创建文档
定义实体对象:
public class Goods {
private Long id;
/**
* 商品标题
*/
private String title;
/**
* 商品价格
*/
private BigDecimal price;
/**
* 商品库存
*/
private Integer stock;
/**
* 商品销售数量
*/
private Integer saleNum;
/**
* 商品分类
*/
private String categoryName;
/**
* 商品品牌
*/
private String brandName;
/**
* 上下架状态
*/
private Integer status;
/**
* 说明书
*/
private String spec;
/**
* 商品创建时间
*/
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
创建文档:
@Test
public void addDocument() {
// 创建商品信息
Goods goods = new Goods();
goods.setId(1L);
goods.setTitle("Apple iPhone 13 Pro (A2639) 256GB 远峰蓝色 支持移动联通电信5G 双卡双待手机");
goods.setPrice(new BigDecimal("8799.00"));
goods.setStock(1000);
goods.setSaleNum(599);
goods.setCategoryName("手机");
goods.setBrandName("Apple");
goods.setStatus(0);
goods.setCreateTime(new Date());
// 返回状态
RestStatus restStatus = null;
try {
restStatus = documentOperatorService.addDocument("goods","_doc", goods);
} catch (Exception e) {
log.error("添加文档失败,错误信息:",e);
}
System.out.println("添加文档响应状态:" + restStatus);
}
@Override
public RestStatus addDocument(String indexName, String type, Goods goods) throws IOException {
type = StringUtils.isEmpty(type) ? "_doc" : type;
// 将对象转为json
String data = JSON.toJSONString(goods);
// 创建索引请求对象
IndexRequest indexRequest = new IndexRequest(indexName,type).id(goods.getId() + "").source(data, XContentType.JSON);
// 执行增加文档
IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
RestStatus status = response.status();
log.info("创建状态:{}", status);
return status;
}
查询文档内容
@Test
public void getDocument() {
// 返回信息
Goods goods = null;
try {
goods = documentOperatorService.getDocument("goods", "_doc", "1");
} catch (Exception e) {
log.error("查询文档失败,错误信息:" ,e);
}
System.out.println("查询的文档信息:" + goods);
}
@Override
public Goods getDocument(String indexName, String type, String id) throws Exception {
// 默认类型为_doc
type = StringUtils.isEmpty(type) ? "_doc" : type;
// 创建获取请求对象
GetRequest getRequest = new GetRequest(indexName, type, id);
GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
Map<String, Object> sourceAsMap = response.getSourceAsMap();
// Goods goods = BeanMapUtils.mapToBean(sourceAsMap,Goods.class);
System.out.println(JSON.toJSONString(sourceAsMap));
log.info("document content:{}",JSON.toJSONString(sourceAsMap));
return null;
}
查询指定索引的所有文档DSL:
更新文档内容
@Test
public void updateDocument() {
// 创建商品信息
Goods goods = new Goods();
goods.setTitle("Apple iPhone 13 Pro Max (A2644) 256GB 远峰蓝色 支持移动联通电信5G 双卡双待手机");
goods.setPrice(new BigDecimal("9999"));
goods.setId(1L);
// 返回状态
RestStatus restStatus = null;
try {
restStatus = documentOperatorService.updateDocument("goods", "_doc", goods);
} catch (Exception e) {
log.error("更新文档失败,错误信息:" ,e);
}
System.out.println("更新文档响应状态:" + restStatus);
}
@Override
public RestStatus updateDocument(String indexName, String type, Goods goods) throws IOException {
type = StringUtils.isEmpty(type) ? "_doc" : type;
// 将对象转为json
String data = JSON.toJSONString(goods);
// 创建索引请求对象
UpdateRequest updateRequest = new UpdateRequest(indexName, type, String.valueOf(goods.getId()));
// 设置更新文档内容
updateRequest.doc(data, XContentType.JSON);
// 执行更新文档
UpdateResponse response = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
log.info("创建状态:{}", response.status());
RestStatus status = response.status();
log.info("更新文档信息响应状态:{}", status);
return status;
}
删除文档
@Test
public void deleteDocument() {
// 返回状态
RestStatus restStatus = null;
try {
restStatus = documentOperatorService.deleteDocument("goods", "_doc", "1");
} catch (Exception e) {
log.error("删除文档失败,错误信息:",e);
}
System.out.println("删除文档响应状态:" + restStatus);
}
@Override
public RestStatus deleteDocument(String indexName, String type, String id) throws IOException {
type = StringUtils.isEmpty(type) ? "_doc" : type;
// 创建删除请求对象
DeleteRequest deleteRequest = new DeleteRequest(indexName, type, id);
// 执行删除文档
DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
RestStatus status = response.status();
log.info("删除文档响应状态:{}", status);
return status;
}