```java
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import java.io.IOException;
@Slf4j
public class DocumentOperation {
/**
* 新增文档
*/
@Test
public void insertDocument() throws IOException {
//获取elasticsearch客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//创建请求对象
IndexRequest request = new IndexRequest();
//设置索引与此次新增的文档id
request.index("user").id("1001");
//插入的数据
User user = User.builder().name("zhangsan").age(30).sex("男").build();
//向elasticsearch插入数据,必须将数据转换位JSON格式
ObjectMapper mapper = new ObjectMapper();
String userJson = mapper.writeValueAsString(user);
request.source(userJson, XContentType.JSON);
//发送请求并获取响应
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
log.info("{}", response.toString());
//IndexResponse[index=user,type=_doc,id=1001,version=1,
// result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
//关闭连接
client.close();
}
/**
* 查询文档
*/
@Test
public void searchDocument() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//设置查询的索引与文档id
GetRequest request = new GetRequest().index("user").id("1001");
//发送请求并获取响应
GetResponse response = client.get(request, RequestOptions.DEFAULT);
log.info(response.toString());
//{"_index":"user","_type":"_doc","_id":"1001","_version":1,"_seq_no":0,"
// _primary_term":1,"found":true,"_source":{"name":"zhangsan","sex":"男","age":30}}
}
/**
* 修改文档
* 如果文档不存在则抛出异常
*/
@Test
public void updateDocument() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
UpdateRequest request = new UpdateRequest();
//要修改的索引与文档id
request.index("user").id("1001");
//要修改的字段
request.doc(XContentType.JSON, "age", 26, "sex", "女");
//发送请求并获取响应
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
log.info(response.toString());
//UpdateResponse[index=user,type=_doc,id=1001,version=2,seqNo=1,primaryTerm=1,
// result=updated,shards=ShardInfo{total=2, successful=1, failures=[]}]
client.close();
}
/**
* 删除文档
*/
@Test
public void deleteDocument() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//设置索引与文档id
DeleteRequest request = new DeleteRequest().index("user").id("1001");
//客户端发送请求,获取响应对象
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
//DeleteResponse[index=user,type=_doc,id=1001,version=8,result=deleted
// ,shards=ShardInfo{total=2, successful=1, failures=[]}]
log.info(response.toString());
client.close();
}
/**
* 批量新增
*/
@Test
public void batchInsertDocument() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//批量请求对象
BulkRequest request = new BulkRequest();
//批量添加数据
request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan", "age", 30, "sex", "男"));
request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi", "age", 30, "sex", "女"));
request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu", "age", 40, "sex", "男"));
request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "wangwu1", "age", 40, "sex", "女"));
request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "wangwu2", "age", 50, "sex", "男"));
request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name", "wangwu3", "age", 50, "sex", "男"));
request.add(new IndexRequest().index("user").id("1007").source(XContentType.JSON, "name", "wangwu44", "age", 60, "sex", "男"));
request.add(new IndexRequest().index("user").id("1008").source(XContentType.JSON, "name", "wangwu555", "age", 60, "sex", "男"));
request.add(new IndexRequest().index("user").id("1009").source(XContentType.JSON, "name", "wangwu66666", "age", 60, "sex", "男"));
//发送请求并获取响应
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
//_took(花费的时间),59ms
log.info("_took(花费的时间),{}", response.getTook());
client.close();
}
/**
* 批量删除
*/
@Test
public void batchDeletDocument() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//批量删除数据
BulkRequest request = new BulkRequest();
//批量删除
request.add(new DeleteRequest().index("user").id("1001"));
request.add(new DeleteRequest().index("user").id("1002"));
request.add(new DeleteRequest().index("user").id("1003"));
//发送请求并获取响应
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
//_took(花费的时间),119ms
log.info("_took(花费的时间),{}", response.getTook());
client.close();
}
}
```
- Elasticsearch是什么
- 全文搜索引擎
- Elasticsearch与Solr
- 数据结构
- 安装Elasticsearch
- Linux单机安装
- Windows单机安装
- 安装Kibana
- Linux安装
- Windows安装
- es基本语句
- 索引操作
- 文档操作
- 映射操作
- 高级查询
- es-JavaAPI
- maven依赖
- 索引操作
- 文档操作
- 高级查询
- es集群搭建
- Linux集群搭建
- Windows集群搭建
- 核心概念
- 索引(Index)
- 类型(Type)
- 文档(Document)
- 字段(Field)
- 映射(Mapping)
- 分片(Shards)
- 副本(Replicas)
- 分配(Allocation)
- 系统架构
- 分布式集群
- 单节点集群
- 故障转移
- 水平扩容
- 应对故障
- 路由计算
- 分片控制
- 写流程
- 读流程
- 更新流程
- 多文档操作流程
- 分片原理
- 倒排索引
- 文档搜索
- 动态更新索引
- 近实时搜索
- 持久化变更
- 段合并
- 文档分析
- 内置分析器
- 分析器使用场景
- 测试分析器
- 指定分析器
- 自定义分析器
- 文档处理
- 文档冲突
- 乐观并发控制
- 外部系统版本控制
- es优化
- 硬件选择
- 分片策略
- 合理设置分片数
- 推迟分片分配
- 路由选择
- 写入速度优化
- 批量数据提交
- 优化存储设备
- 合理使用合并
- 减少Refresh的次数
- 加大Flush设置
- 减少副本的数量
- 内存设置
- 重要配置
- es常见问题
- 为什么要使用Elasticsearch
- master选举流程
- 集群脑裂问题
- 索引文档流程
- 更新和删除文档流程
- 搜索流程
- ES部署在Linux时的优化方法
- GC方面ES需要注意的点
- ES对大数据量的聚合实现
- 并发时保证读写一致性
- 字典树
- ES的倒排索引
- Spring Data Elasticsearch
- 环境搭建
- 索引操作
- 文档操作