依赖的包
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.6.2
org.elasticsearch.client
elasticsearch-rest-client
7.6.2
org.elasticsearch
elasticsearch
7.6.2
创建client 和 新增索引信息示例:
public static void main(String[] args){
// 指定ip 端口
HttpHost httpHost = new HttpHost("localhost",9200,"http");
// 创建client
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHost));
// 索引数据
Map json = new HashMap<>();
json.put("user","zhanghaha");
json.put("message","hello啊");
// 请求对象 指定索引名称,和source 数据
IndexRequest indexRequest = new IndexRequest("testsuoyin1").source(json);
try {
// 执行得到 response
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("出参:"+indexResponse.toString());
// 关闭
restHighLevelClient.close();
}catch (Exception e){
}
}
执行上面代码后会 生成‘testsuoyin1 的索引文件,和数据。如图:
// 根据文档 id 查询
public static void main(String[] args){
// 指定ip 端口
HttpHost httpHost = new HttpHost("localhost",9200,"http");
// 创建client
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHost));
// 根据文档id 查询
GetRequest getRequest = new GetRequest("testsuoyin1","rpsjR3QBb3uB6hDzEBFC");
try {
// 查询结果
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
String id = documentFields.getId();// id
Map fields = documentFields.getFields();
String index = documentFields.getIndex();// index
Map source = documentFields.getSource();// 索引的字段信息
long primaryTerm = documentFields.getPrimaryTerm();
long version = documentFields.getVersion();
System.out.println("id:"+id+"index:"+index+"version:"+version+"count:"+fields.size());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
restHighLevelClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
根据文档id 删除 获取删除结果 和删除条数
public static void main(String[] args){
// 指定ip 端口
HttpHost httpHost = new HttpHost("localhost",9200,"http");
// 创建client
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHost));
// 根据文档id 删除
DeleteRequest deleteRequest = new DeleteRequest("testsuoyin1","rpsjR3QBb3uB6hDzEBFC");
try {
// 执行删除
DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
DocWriteResponse.Result result = delete.getResult();
long seqNo = delete.getSeqNo();
int successful = delete.getShardInfo().getSuccessful();// 获取删除条数
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
restHighLevelClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 根据文档id 更新文档 需要注意的是:测试类运行后 好像只能更新 已存在的字段,不存在的字段不会增加
public static void main(String[] args){
// 指定ip 端口
HttpHost httpHost = new HttpHost("localhost",9200,"http");
// 创建client
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHost));
// 根据文档id 更新
HashMap map = new HashMap<>();
map.put("123","awaerqwer");
map.put("234","asfd");
map.put("message","helloaaaaa");
UpdateRequest updateRequest = new UpdateRequest("testsuoyin1","r5sjR3QBb3uB6hDz2RG7").doc(map);
try {
// 执行更新
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
GetResult getResult = update.getGetResult();
int total = update.getShardInfo().getTotal();
System.out.println(total);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
restHighLevelClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用head 插入一条数据:
post:
{
"id": 123,
"title": "第二个目标是通过该接口获取特定歌曲的hash和album_id的值用于下面歌曲的播放,文字图片以及歌词(下一篇讲)的显示,",
"content": "第二个目标是通过该接口获取特定歌曲的hash和album_id的值用于下面歌曲的播放,文字图片以及歌词(下一篇讲)的显示"
}
使用head match 查询单个字段:
post:
{
"query": {
"match": {
"title": "集群健康状态"
}
}
}
指定字段排序:
{
"query": {
"match": {
"title": "是"
}
},
"sort": {
"_id": {
"order": "desc"
}
}
}
match_phrase 查询:
{
"query": {
"match_phrase": {
"title": {
"query": "是 and 的",
"slop": 3
}
}
},
"sort": {
"_id": {
"order": "desc"
}
}
}
multi_match 查询。匹配多个字段:
title content 字段 其中一个字段内容包含 ‘第二个’ 或者 and 都可以
{
"query": {
"multi_match": {
"query": "第二个 and",
"fields": [
"title",
"content"
]
}
}
}