Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
elasticsearch-demo
提交
70614a1a
E
elasticsearch-demo
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
elasticsearch-demo
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
elasticsearch-demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
70614a1a
编写于
5月 10, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:es 练习
上级
d5975d5d
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
647 addition
and
275 deletion
+647
-275
src/test/java/TestDemo.java
src/test/java/TestDemo.java
+5
-3
src/test/java/TestDocument.java
src/test/java/TestDocument.java
+0
-272
src/test/java/es_01/TestDocument_01_get.java
src/test/java/es_01/TestDocument_01_get.java
+57
-0
src/test/java/es_01/TestDocument_02_get.java
src/test/java/es_01/TestDocument_02_get.java
+66
-0
src/test/java/es_01/TestDocument_03_add.java
src/test/java/es_01/TestDocument_03_add.java
+68
-0
src/test/java/es_01/TestDocument_04_add.java
src/test/java/es_01/TestDocument_04_add.java
+64
-0
src/test/java/es_01/TestDocument_05_add.java
src/test/java/es_01/TestDocument_05_add.java
+68
-0
src/test/java/es_01/TestDocument_06_add.java
src/test/java/es_01/TestDocument_06_add.java
+60
-0
src/test/java/es_01/TestDocument_07_add.java
src/test/java/es_01/TestDocument_07_add.java
+80
-0
src/test/java/es_01/TestDocument_08_update.java
src/test/java/es_01/TestDocument_08_update.java
+66
-0
src/test/java/es_01/TestDocument_09_delete.java
src/test/java/es_01/TestDocument_09_delete.java
+46
-0
src/test/java/es_01/TestDocument_10_bulk.java
src/test/java/es_01/TestDocument_10_bulk.java
+67
-0
未找到文件。
src/test/java/TestDemo.java
浏览文件 @
70614a1a
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.http.HttpHost
;
import
org.apache.http.HttpHost
;
import
org.elasticsearch.action.get.GetRequest
;
import
org.elasticsearch.action.get.GetRequest
;
import
org.elasticsearch.action.get.GetResponse
;
import
org.elasticsearch.action.get.GetResponse
;
...
@@ -14,6 +15,7 @@ import java.io.IOException;
...
@@ -14,6 +15,7 @@ import java.io.IOException;
* @version : 2.2.0
* @version : 2.2.0
* @date : 2023/5/10 11:50
* @date : 2023/5/10 11:50
*/
*/
@Slf4j
public
class
TestDemo
{
public
class
TestDemo
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
//1获取连接客户端
//1获取连接客户端
...
@@ -25,8 +27,8 @@ public class TestDemo {
...
@@ -25,8 +27,8 @@ public class TestDemo {
//3执行
//3执行
GetResponse
getResponse
=
client
.
get
(
getRequest
,
RequestOptions
.
DEFAULT
);
GetResponse
getResponse
=
client
.
get
(
getRequest
,
RequestOptions
.
DEFAULT
);
//4获取结果
//4获取结果
System
.
out
.
println
(
getResponse
.
getId
());
log
.
info
(
getResponse
.
getId
());
System
.
out
.
println
(
getResponse
.
getVersion
(
));
log
.
info
(
String
.
valueOf
(
getResponse
.
getVersion
()
));
System
.
out
.
println
(
getResponse
.
getSourceAsString
());
log
.
info
(
getResponse
.
getSourceAsString
());
}
}
}
}
\ No newline at end of file
src/test/java/TestDocument.java
已删除
100644 → 0
浏览文件 @
d5975d5d
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.DocWriteResponse
;
import
org.elasticsearch.action.bulk.BulkItemResponse
;
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.support.replication.ReplicationResponse
;
import
org.elasticsearch.action.update.UpdateRequest
;
import
org.elasticsearch.action.update.UpdateResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.common.xcontent.XContentType
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testGet
()
throws
IOException
{
// 1构建请求
GetRequest
getRequest
=
new
GetRequest
(
"test_post"
,
"1"
);
// ===========可选参数==========
// String[] includes=new String[]{"user","message"};
// String[] excludes= Strings.EMPTY_ARRAY;
// FetchSourceContext fetchSourceContext=new FetchSourceContext(true, includes, excludes);
// getRequest.fetchSourceContext(fetchSourceContext);
// String[] includes=Strings.EMPTY_ARRAY;
// String[] excludes= new String[]{"user","message"};
// FetchSourceContext fetchSourceContext=new FetchSourceContext(true, includes, excludes);
// getRequest.fetchSourceContext(fetchSourceContext);
// getRequest.routing("routing");
// 2执行
// 同步查询
GetResponse
getResponse
=
client
.
get
(
getRequest
,
RequestOptions
.
DEFAULT
);
//异步查询
// ActionListener<GetResponse> listener=new ActionListener<GetResponse>() {
// //成功时
// public void onResponse(GetResponse getResponse) {
// System.out.println(getResponse.getId());
//// System.out.println(getResponse.getVersion());
//// System.out.println(getResponse.getSourceAsString());
// }
// //失败时
// public void onFailure(Exception e) {
// e.printStackTrace();
// }
// };
//
// client.getAsync(getRequest, RequestOptions.DEFAULT, listener);
//
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// 3获取结果
if
(
getResponse
.
isExists
())
{
System
.
out
.
println
(
getResponse
.
getId
());
System
.
out
.
println
(
getResponse
.
getVersion
());
System
.
out
.
println
(
getResponse
.
getSourceAsString
());
//以string获取数据
System
.
out
.
println
(
getResponse
.
getSourceAsBytes
());
////以Bytes获取数据
System
.
out
.
println
(
getResponse
.
getSourceAsMap
());
//以Map获取数据
}
else
{
log
.
info
(
"数据不存在"
);
}
}
@Test
public
void
testAdd
()
throws
IOException
{
// PUT test_post/_doc/2
// {
// "user":"tomas",
// "postDate":"2019-07-18",
// "message":"trying out es1"
// }
// 1构建请求
IndexRequest
request
=
new
IndexRequest
(
"test_post"
);
request
.
id
(
"5"
);
// ======================构建文档数据=======================
//方法1
String
jsonString
=
"{\n"
+
" \"user\":\"tomas\",\n"
+
" \"postDate\":\"2019-07-18\",\n"
+
" \"message\":\"trying out es1\"\n"
+
"}"
;
request
.
source
(
jsonString
,
XContentType
.
JSON
);
//方法2
// Map<String, Object> jsonMap = new HashMap<String, Object>();
// jsonMap.put("user", "tomas");
// jsonMap.put("postDate", "2019-07-18");
// jsonMap.put("message", "trying out es1");
// request.source(jsonMap);
//方法3
// XContentBuilder builder = XContentFactory.jsonBuilder();
// builder.startObject();
// {
// builder.field("user", "tomas");
// builder.field("message", "trying out es1");
// builder.timeField("postDate", "2019-07-18");
// }
// builder.endObject();
// request.source(builder);
//方法4
// request.source("user", "tomas",
// "message", "trying out es1",
// "postDate", "2019-07-18");
// =====可选参数==
//设置超时时间
// request.timeout("1s");
// request.timeout(TimeValue.timeValueSeconds(1));
//手动维护版本号
// request.version(4);
// request.versionType(VersionType.EXTERNAL);
// 2执行
//同步
IndexResponse
indexResponse
=
client
.
index
(
request
,
RequestOptions
.
DEFAULT
);
// 异步
// ActionListener<IndexResponse> listener=new ActionListener<IndexResponse>() {
// //成功时
// public void onResponse(IndexResponse indexResponse) {
// System.out.println(indexResponse.getIndex());
// System.out.println(indexResponse.getId());
// System.out.println(indexResponse.getResult());
// }
// //失败时
// public void onFailure(Exception e) {
//
// }
// };
//
// client.indexAsync(request, RequestOptions.DEFAULT, listener);
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// 3获取结果
System
.
out
.
println
(
indexResponse
.
getIndex
());
System
.
out
.
println
(
indexResponse
.
getId
());
System
.
out
.
println
(
indexResponse
.
getResult
());
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
CREATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
System
.
out
.
println
(
"CREATE"
+
result
);
}
else
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
UPDATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
System
.
out
.
println
(
"UPDATED"
+
result
);
}
else
{
}
// "_shards" : {
// "total" : 2,
// "successful" : 1,
// "failed" : 0
// }
ReplicationResponse
.
ShardInfo
shardInfo
=
indexResponse
.
getShardInfo
();
if
(
shardInfo
.
getTotal
()
!=
shardInfo
.
getSuccessful
())
{
System
.
out
.
println
(
"处理成功的分片数少于总分片!"
);
}
if
(
shardInfo
.
getFailed
()
>
0
)
{
for
(
ReplicationResponse
.
ShardInfo
.
Failure
failure
:
shardInfo
.
getFailures
())
{
String
reason
=
failure
.
reason
();
//每一个错误的原因
System
.
out
.
println
(
reason
);
}
}
}
@Test
public
void
testUpdate
()
throws
IOException
{
// POST test_post/_doc/3/_update
// {
// "doc":{
// "user":"tomas Lee"
// }
// }
// 1创建请求
UpdateRequest
request
=
new
UpdateRequest
(
"test_post"
,
"3"
);
Map
<
String
,
Object
>
jsonMap
=
new
HashMap
<>();
jsonMap
.
put
(
"user"
,
"tomas Lee"
);
request
.
doc
(
jsonMap
);
// =========可选参数========
request
.
timeout
(
"1s"
);
request
.
retryOnConflict
(
3
);
//重试次数
// 2执行
//同步
UpdateResponse
updateResponse
=
client
.
update
(
request
,
RequestOptions
.
DEFAULT
);
//异步
// 3获取结果
updateResponse
.
getId
();
updateResponse
.
getIndex
();
//判断结果
if
(
updateResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
CREATED
)
{
DocWriteResponse
.
Result
result
=
updateResponse
.
getResult
();
System
.
out
.
println
(
"CREATED:"
+
result
);
}
else
if
(
updateResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
UPDATED
)
{
DocWriteResponse
.
Result
result
=
updateResponse
.
getResult
();
System
.
out
.
println
(
"UPDATED:"
+
result
);
}
else
if
(
updateResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
DELETED
)
{
DocWriteResponse
.
Result
result
=
updateResponse
.
getResult
();
System
.
out
.
println
(
"DELETED:"
+
result
);
}
else
if
(
updateResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
NOOP
)
{
//没有操作
DocWriteResponse
.
Result
result
=
updateResponse
.
getResult
();
System
.
out
.
println
(
"NOOP:"
+
result
);
}
}
@Test
public
void
testDelete
()
throws
IOException
{
// 1创建请求
DeleteRequest
request
=
new
DeleteRequest
(
"test_post"
,
"3"
);
//可选参数
// 2执行
DeleteResponse
deleteResponse
=
client
.
delete
(
request
,
RequestOptions
.
DEFAULT
);
// 3获取结果
deleteResponse
.
getId
();
deleteResponse
.
getIndex
();
DocWriteResponse
.
Result
result
=
deleteResponse
.
getResult
();
System
.
out
.
println
(
result
);
}
@Test
public
void
testBulk
()
throws
IOException
{
// 1创建请求
BulkRequest
request
=
new
BulkRequest
();
// request.add(new IndexRequest("post").id("1").source(XContentType.JSON,"field","1" ));
// request.add(new IndexRequest("post").id("2").source(XContentType.JSON,"field","2" ));
request
.
add
(
new
UpdateRequest
(
"post"
,
"1"
).
doc
(
XContentType
.
JSON
,
"field"
,
"3"
));
request
.
add
(
new
DeleteRequest
(
"post"
).
id
(
"2"
));
// 2执行
BulkResponse
bulkResponse
=
client
.
bulk
(
request
,
RequestOptions
.
DEFAULT
);
// 3获取结果
for
(
BulkItemResponse
itemResponse
:
bulkResponse
)
{
DocWriteResponse
response
=
itemResponse
.
getResponse
();
switch
(
itemResponse
.
getOpType
())
{
case
INDEX:
IndexResponse
indexResponse
=
(
IndexResponse
)
response
;
System
.
out
.
println
(
"INDEX:"
+
indexResponse
.
getResult
());
break
;
case
CREATE:
IndexResponse
createResponse
=
(
IndexResponse
)
response
;
System
.
out
.
println
(
"CREATE:"
+
createResponse
.
getResult
());
break
;
case
UPDATE:
UpdateResponse
updateResponse
=
(
UpdateResponse
)
response
;
System
.
out
.
println
(
"UPDATE:"
+
updateResponse
.
getResult
());
break
;
case
DELETE:
DeleteResponse
deleteResponse
=
(
DeleteResponse
)
response
;
System
.
out
.
println
(
"DELETE:"
+
deleteResponse
.
getResult
());
break
;
}
}
}
}
\ No newline at end of file
src/test/java/es_01/TestDocument_01_get.java
0 → 100644
浏览文件 @
70614a1a
package
es_01
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.get.GetRequest
;
import
org.elasticsearch.action.get.GetResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.common.Strings
;
import
org.elasticsearch.search.fetch.subphase.FetchSourceContext
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
/**
* 同步获取结果
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/10 14:00
*/
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument_01_get
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testGet
()
throws
IOException
{
//构建请求
GetRequest
getRequest
=
new
GetRequest
(
"test_post"
,
"1"
);
//添加可选参数
String
[]
includes
=
new
String
[]{
"user"
,
"message"
};
String
[]
excludes
=
Strings
.
EMPTY_ARRAY
;
FetchSourceContext
fetchSourceContext
=
new
FetchSourceContext
(
true
,
includes
,
excludes
);
getRequest
.
fetchSourceContext
(
fetchSourceContext
);
//同步查询
GetResponse
getResponse
=
client
.
get
(
getRequest
,
RequestOptions
.
DEFAULT
);
//获取结果
if
(
getResponse
.
isExists
())
{
log
.
info
(
getResponse
.
getId
());
log
.
info
(
String
.
valueOf
(
getResponse
.
getVersion
()));
log
.
info
(
getResponse
.
getSourceAsString
());
//以string获取数据
log
.
info
(
String
.
valueOf
(
getResponse
.
getSourceAsBytes
()));
////以Bytes获取数据
log
.
info
(
String
.
valueOf
(
getResponse
.
getSourceAsMap
()));
//以Map获取数据
}
else
{
log
.
info
(
"数据不存在"
);
}
}
}
\ No newline at end of file
src/test/java/es_01/TestDocument_02_get.java
0 → 100644
浏览文件 @
70614a1a
package
es_01
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.ActionListener
;
import
org.elasticsearch.action.get.GetRequest
;
import
org.elasticsearch.action.get.GetResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.common.Strings
;
import
org.elasticsearch.search.fetch.subphase.FetchSourceContext
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
/**
* 异步获取结果
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/10 14:00
*/
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument_02_get
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testGet
()
{
//构建请求
GetRequest
getRequest
=
new
GetRequest
(
"test_post"
,
"1"
);
//添加可选参数
String
[]
includes
=
new
String
[]{
"user"
,
"message"
};
String
[]
excludes
=
Strings
.
EMPTY_ARRAY
;
FetchSourceContext
fetchSourceContext
=
new
FetchSourceContext
(
true
,
includes
,
excludes
);
getRequest
.
fetchSourceContext
(
fetchSourceContext
);
//设置监听器
ActionListener
<
GetResponse
>
listener
=
new
ActionListener
<
GetResponse
>()
{
//成功时
public
void
onResponse
(
GetResponse
getResponse
)
{
log
.
info
(
getResponse
.
getId
());
log
.
info
(
String
.
valueOf
(
getResponse
.
getVersion
()));
log
.
info
(
getResponse
.
getSourceAsString
());
}
//失败时
public
void
onFailure
(
Exception
e
)
{
e
.
printStackTrace
();
log
.
info
(
"数据获取异常"
);
}
};
//异步查询
client
.
getAsync
(
getRequest
,
RequestOptions
.
DEFAULT
,
listener
);
try
{
Thread
.
sleep
(
5000
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
src/test/java/es_01/TestDocument_03_add.java
0 → 100644
浏览文件 @
70614a1a
package
es_01
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.DocWriteResponse
;
import
org.elasticsearch.action.index.IndexRequest
;
import
org.elasticsearch.action.index.IndexResponse
;
import
org.elasticsearch.action.support.replication.ReplicationResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.common.xcontent.XContentType
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument_03_add
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testAdd
()
throws
IOException
{
//构建请求
IndexRequest
request
=
new
IndexRequest
(
"test_post"
);
request
.
id
(
"5"
);
//构建文档数据
String
jsonString
=
"{\n"
+
" \"user\":\"tomas\",\n"
+
" \"postDate\":\"2019-07-18\",\n"
+
" \"message\":\"trying out es1\"\n"
+
"}"
;
request
.
source
(
jsonString
,
XContentType
.
JSON
);
//同步
IndexResponse
indexResponse
=
client
.
index
(
request
,
RequestOptions
.
DEFAULT
);
//获取结果
log
.
info
(
indexResponse
.
getIndex
());
log
.
info
(
indexResponse
.
getId
());
log
.
info
(
String
.
valueOf
(
indexResponse
.
getResult
()));
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
CREATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
log
.
info
(
"CREATE"
+
result
);
}
else
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
UPDATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
log
.
info
(
"UPDATED"
+
result
);
}
else
{
log
.
info
(
"其他操作"
);
}
//获取分片信息
ReplicationResponse
.
ShardInfo
shardInfo
=
indexResponse
.
getShardInfo
();
if
(
shardInfo
.
getTotal
()
!=
shardInfo
.
getSuccessful
())
{
log
.
info
(
"处理成功的分片数少于总分片!"
);
}
if
(
shardInfo
.
getFailed
()
>
0
)
{
for
(
ReplicationResponse
.
ShardInfo
.
Failure
failure
:
shardInfo
.
getFailures
())
{
String
reason
=
failure
.
reason
();
//每一个错误的原因
log
.
info
(
reason
);
}
}
}
}
\ No newline at end of file
src/test/java/es_01/TestDocument_04_add.java
0 → 100644
浏览文件 @
70614a1a
package
es_01
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.DocWriteResponse
;
import
org.elasticsearch.action.index.IndexRequest
;
import
org.elasticsearch.action.index.IndexResponse
;
import
org.elasticsearch.action.support.replication.ReplicationResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument_04_add
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testAdd
()
throws
IOException
{
//构建请求
IndexRequest
request
=
new
IndexRequest
(
"test_post"
);
request
.
id
(
"5"
);
//构建文档数据
Map
<
String
,
Object
>
jsonMap
=
new
HashMap
<
String
,
Object
>();
jsonMap
.
put
(
"user"
,
"tomas"
);
jsonMap
.
put
(
"postDate"
,
"2019-07-18"
);
jsonMap
.
put
(
"message"
,
"trying out es1"
);
request
.
source
(
jsonMap
);
//同步执行
IndexResponse
indexResponse
=
client
.
index
(
request
,
RequestOptions
.
DEFAULT
);
//获取结果
log
.
info
(
indexResponse
.
getIndex
());
log
.
info
(
indexResponse
.
getId
());
log
.
info
(
String
.
valueOf
(
indexResponse
.
getResult
()));
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
CREATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
log
.
info
(
"CREATE"
+
result
);
}
else
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
UPDATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
log
.
info
(
"UPDATED"
+
result
);
}
ReplicationResponse
.
ShardInfo
shardInfo
=
indexResponse
.
getShardInfo
();
if
(
shardInfo
.
getTotal
()
!=
shardInfo
.
getSuccessful
())
{
log
.
info
(
"处理成功的分片数少于总分片!"
);
}
if
(
shardInfo
.
getFailed
()
>
0
)
{
for
(
ReplicationResponse
.
ShardInfo
.
Failure
failure
:
shardInfo
.
getFailures
())
{
String
reason
=
failure
.
reason
();
//每一个错误的原因
log
.
info
(
reason
);
}
}
}
}
\ No newline at end of file
src/test/java/es_01/TestDocument_05_add.java
0 → 100644
浏览文件 @
70614a1a
package
es_01
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.DocWriteResponse
;
import
org.elasticsearch.action.index.IndexRequest
;
import
org.elasticsearch.action.index.IndexResponse
;
import
org.elasticsearch.action.support.replication.ReplicationResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.common.xcontent.XContentBuilder
;
import
org.elasticsearch.common.xcontent.XContentFactory
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument_05_add
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testAdd
()
throws
IOException
{
//构建请求
IndexRequest
request
=
new
IndexRequest
(
"test_post"
);
request
.
id
(
"5"
);
//构建文档数据
XContentBuilder
builder
=
XContentFactory
.
jsonBuilder
();
builder
.
startObject
();
{
builder
.
field
(
"user"
,
"tomas"
);
builder
.
field
(
"message"
,
"trying out es1"
);
builder
.
timeField
(
"postDate"
,
"2019-07-18"
);
}
builder
.
endObject
();
request
.
source
(
builder
);
//同步执行
IndexResponse
indexResponse
=
client
.
index
(
request
,
RequestOptions
.
DEFAULT
);
//获取结果
log
.
info
(
indexResponse
.
getIndex
());
log
.
info
(
indexResponse
.
getId
());
log
.
info
(
String
.
valueOf
(
indexResponse
.
getResult
()));
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
CREATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
log
.
info
(
"CREATE"
+
result
);
}
else
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
UPDATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
log
.
info
(
"UPDATED"
+
result
);
}
ReplicationResponse
.
ShardInfo
shardInfo
=
indexResponse
.
getShardInfo
();
if
(
shardInfo
.
getTotal
()
!=
shardInfo
.
getSuccessful
())
{
log
.
info
(
"处理成功的分片数少于总分片!"
);
}
if
(
shardInfo
.
getFailed
()
>
0
)
{
for
(
ReplicationResponse
.
ShardInfo
.
Failure
failure
:
shardInfo
.
getFailures
())
{
String
reason
=
failure
.
reason
();
//每一个错误的原因
log
.
info
(
reason
);
}
}
}
}
\ No newline at end of file
src/test/java/es_01/TestDocument_06_add.java
0 → 100644
浏览文件 @
70614a1a
package
es_01
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.DocWriteResponse
;
import
org.elasticsearch.action.index.IndexRequest
;
import
org.elasticsearch.action.index.IndexResponse
;
import
org.elasticsearch.action.support.replication.ReplicationResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument_06_add
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testAdd
()
throws
IOException
{
//构建请求
IndexRequest
request
=
new
IndexRequest
(
"test_post"
);
request
.
id
(
"5"
);
//构建文档数据
request
.
source
(
"user"
,
"tomas"
,
"message"
,
"trying out es1"
,
"postDate"
,
"2019-07-18"
);
//同步执行
IndexResponse
indexResponse
=
client
.
index
(
request
,
RequestOptions
.
DEFAULT
);
//获取结果
log
.
info
(
indexResponse
.
getIndex
());
log
.
info
(
indexResponse
.
getId
());
log
.
info
(
String
.
valueOf
(
indexResponse
.
getResult
()));
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
CREATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
log
.
info
(
"CREATE"
+
result
);
}
else
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
UPDATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
log
.
info
(
"UPDATED"
+
result
);
}
ReplicationResponse
.
ShardInfo
shardInfo
=
indexResponse
.
getShardInfo
();
if
(
shardInfo
.
getTotal
()
!=
shardInfo
.
getSuccessful
())
{
log
.
info
(
"处理成功的分片数少于总分片!"
);
}
if
(
shardInfo
.
getFailed
()
>
0
)
{
for
(
ReplicationResponse
.
ShardInfo
.
Failure
failure
:
shardInfo
.
getFailures
())
{
String
reason
=
failure
.
reason
();
//每一个错误的原因
log
.
info
(
reason
);
}
}
}
}
\ No newline at end of file
src/test/java/es_01/TestDocument_07_add.java
0 → 100644
浏览文件 @
70614a1a
package
es_01
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.DocWriteResponse
;
import
org.elasticsearch.action.index.IndexRequest
;
import
org.elasticsearch.action.index.IndexResponse
;
import
org.elasticsearch.action.support.replication.ReplicationResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.common.unit.TimeValue
;
import
org.elasticsearch.common.xcontent.XContentType
;
import
org.elasticsearch.index.VersionType
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
/**
* 设置额外的参数
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/10 14:00
*/
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument_07_add
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testAdd
()
throws
IOException
{
//构建请求
IndexRequest
request
=
new
IndexRequest
(
"test_post"
);
request
.
id
(
"5"
);
//构建文档数据
String
jsonString
=
"{\n"
+
" \"user\":\"tomas\",\n"
+
" \"postDate\":\"2019-07-18\",\n"
+
" \"message\":\"trying out es1\"\n"
+
"}"
;
request
.
source
(
jsonString
,
XContentType
.
JSON
);
//设置超时时间
request
.
timeout
(
"1s"
);
request
.
timeout
(
TimeValue
.
timeValueSeconds
(
1
));
//手动维护版本号
request
.
version
(
4
);
request
.
versionType
(
VersionType
.
EXTERNAL
);
//同步执行
IndexResponse
indexResponse
=
client
.
index
(
request
,
RequestOptions
.
DEFAULT
);
//获取结果
log
.
info
(
indexResponse
.
getIndex
());
log
.
info
(
indexResponse
.
getId
());
log
.
info
(
String
.
valueOf
(
indexResponse
.
getResult
()));
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
CREATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
log
.
info
(
"CREATE"
+
result
);
}
else
if
(
indexResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
UPDATED
)
{
DocWriteResponse
.
Result
result
=
indexResponse
.
getResult
();
log
.
info
(
"UPDATED"
+
result
);
}
ReplicationResponse
.
ShardInfo
shardInfo
=
indexResponse
.
getShardInfo
();
if
(
shardInfo
.
getTotal
()
!=
shardInfo
.
getSuccessful
())
{
log
.
info
(
"处理成功的分片数少于总分片!"
);
}
if
(
shardInfo
.
getFailed
()
>
0
)
{
for
(
ReplicationResponse
.
ShardInfo
.
Failure
failure
:
shardInfo
.
getFailures
())
{
String
reason
=
failure
.
reason
();
//每一个错误的原因
log
.
info
(
reason
);
}
}
}
}
\ No newline at end of file
src/test/java/es_01/TestDocument_08_update.java
0 → 100644
浏览文件 @
70614a1a
package
es_01
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.DocWriteResponse
;
import
org.elasticsearch.action.update.UpdateRequest
;
import
org.elasticsearch.action.update.UpdateResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 更新文档
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/10 14:05
*/
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument_08_update
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testUpdate
()
throws
IOException
{
//创建请求
UpdateRequest
request
=
new
UpdateRequest
(
"test_post"
,
"3"
);
Map
<
String
,
Object
>
jsonMap
=
new
HashMap
<>();
jsonMap
.
put
(
"user"
,
"tomas Lee"
);
request
.
doc
(
jsonMap
);
request
.
timeout
(
"1s"
);
request
.
retryOnConflict
(
3
);
//重试次数
//同步执行
UpdateResponse
updateResponse
=
client
.
update
(
request
,
RequestOptions
.
DEFAULT
);
//获取结果
updateResponse
.
getId
();
updateResponse
.
getIndex
();
//判断结果
if
(
updateResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
CREATED
)
{
DocWriteResponse
.
Result
result
=
updateResponse
.
getResult
();
log
.
info
(
"CREATED:"
+
result
);
}
else
if
(
updateResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
UPDATED
)
{
DocWriteResponse
.
Result
result
=
updateResponse
.
getResult
();
log
.
info
(
"UPDATED:"
+
result
);
}
else
if
(
updateResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
DELETED
)
{
DocWriteResponse
.
Result
result
=
updateResponse
.
getResult
();
log
.
info
(
"DELETED:"
+
result
);
}
else
if
(
updateResponse
.
getResult
()
==
DocWriteResponse
.
Result
.
NOOP
)
{
//没有操作
DocWriteResponse
.
Result
result
=
updateResponse
.
getResult
();
log
.
info
(
"NOOP:"
+
result
);
}
}
}
\ No newline at end of file
src/test/java/es_01/TestDocument_09_delete.java
0 → 100644
浏览文件 @
70614a1a
package
es_01
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.DocWriteResponse
;
import
org.elasticsearch.action.delete.DeleteRequest
;
import
org.elasticsearch.action.delete.DeleteResponse
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
/**
* 删除文档
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/10 14:04
*/
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument_09_delete
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testDelete
()
throws
IOException
{
//创建请求
DeleteRequest
request
=
new
DeleteRequest
(
"test_post"
,
"3"
);
//执行
DeleteResponse
deleteResponse
=
client
.
delete
(
request
,
RequestOptions
.
DEFAULT
);
//获取结果
deleteResponse
.
getId
();
deleteResponse
.
getIndex
();
DocWriteResponse
.
Result
result
=
deleteResponse
.
getResult
();
log
.
info
(
result
.
toString
());
}
}
\ No newline at end of file
src/test/java/es_01/TestDocument_10_bulk.java
0 → 100644
浏览文件 @
70614a1a
package
es_01
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
import
org.elasticsearch.action.DocWriteResponse
;
import
org.elasticsearch.action.bulk.BulkItemResponse
;
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.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.RestHighLevelClient
;
import
org.elasticsearch.common.xcontent.XContentType
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.io.IOException
;
@Slf4j
@SpringBootTest
(
classes
=
SearchApplication
.
class
)
@RunWith
(
SpringRunner
.
class
)
public
class
TestDocument_10_bulk
{
@Autowired
RestHighLevelClient
client
;
@Test
public
void
testBulk
()
throws
IOException
{
//创建请求
BulkRequest
request
=
new
BulkRequest
();
request
.
add
(
new
IndexRequest
(
"post"
).
id
(
"1"
).
source
(
XContentType
.
JSON
,
"field"
,
"1"
));
request
.
add
(
new
IndexRequest
(
"post"
).
id
(
"2"
).
source
(
XContentType
.
JSON
,
"field"
,
"2"
));
request
.
add
(
new
UpdateRequest
(
"post"
,
"1"
).
doc
(
XContentType
.
JSON
,
"field"
,
"3"
));
request
.
add
(
new
DeleteRequest
(
"post"
).
id
(
"2"
));
//执行
BulkResponse
bulkResponse
=
client
.
bulk
(
request
,
RequestOptions
.
DEFAULT
);
//获取结果
for
(
BulkItemResponse
itemResponse
:
bulkResponse
)
{
DocWriteResponse
response
=
itemResponse
.
getResponse
();
switch
(
itemResponse
.
getOpType
())
{
case
INDEX:
IndexResponse
indexResponse
=
(
IndexResponse
)
response
;
log
.
info
(
"INDEX:"
+
indexResponse
.
getResult
());
break
;
case
CREATE:
IndexResponse
createResponse
=
(
IndexResponse
)
response
;
log
.
info
(
"CREATE:"
+
createResponse
.
getResult
());
break
;
case
UPDATE:
UpdateResponse
updateResponse
=
(
UpdateResponse
)
response
;
log
.
info
(
"UPDATE:"
+
updateResponse
.
getResult
());
break
;
case
DELETE:
DeleteResponse
deleteResponse
=
(
DeleteResponse
)
response
;
log
.
info
(
"DELETE:"
+
deleteResponse
.
getResult
());
break
;
}
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录