Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
elasticsearch-demo
提交
07d36e09
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看板
提交
07d36e09
编写于
5月 13, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:开启索引
上级
0c2278b4
变更
21
隐藏空白更改
内联
并排
Showing
21 changed file
with
645 addition
and
10 deletion
+645
-10
src/main/resources/application.yml
src/main/resources/application.yml
+3
-0
src/main/resources/logback-spring.xml
src/main/resources/logback-spring.xml
+47
-0
src/test/java/es_01_document/TestDocument_01_get.java
src/test/java/es_01_document/TestDocument_01_get.java
+1
-1
src/test/java/es_01_document/TestDocument_02_get.java
src/test/java/es_01_document/TestDocument_02_get.java
+1
-1
src/test/java/es_01_document/TestDocument_03_add.java
src/test/java/es_01_document/TestDocument_03_add.java
+1
-1
src/test/java/es_01_document/TestDocument_04_add.java
src/test/java/es_01_document/TestDocument_04_add.java
+1
-1
src/test/java/es_01_document/TestDocument_05_add.java
src/test/java/es_01_document/TestDocument_05_add.java
+1
-1
src/test/java/es_01_document/TestDocument_06_add.java
src/test/java/es_01_document/TestDocument_06_add.java
+1
-1
src/test/java/es_01_document/TestDocument_07_add.java
src/test/java/es_01_document/TestDocument_07_add.java
+1
-1
src/test/java/es_01_document/TestDocument_08_update.java
src/test/java/es_01_document/TestDocument_08_update.java
+1
-1
src/test/java/es_01_document/TestDocument_09_delete.java
src/test/java/es_01_document/TestDocument_09_delete.java
+1
-1
src/test/java/es_01_document/TestDocument_10_bulk.java
src/test/java/es_01_document/TestDocument_10_bulk.java
+1
-1
src/test/java/es_02_index/TestIndex_01_Create.java
src/test/java/es_02_index/TestIndex_01_Create.java
+100
-0
src/test/java/es_02_index/TestIndex_02_Create.java
src/test/java/es_02_index/TestIndex_02_Create.java
+91
-0
src/test/java/es_02_index/TestIndex_03_Create.java
src/test/java/es_02_index/TestIndex_03_Create.java
+99
-0
src/test/java/es_02_index/TestIndex_04_Async.java
src/test/java/es_02_index/TestIndex_04_Async.java
+94
-0
src/test/java/es_02_index/TestIndex_05_delete.java
src/test/java/es_02_index/TestIndex_05_delete.java
+42
-0
src/test/java/es_02_index/TestIndex_06_delete.java
src/test/java/es_02_index/TestIndex_06_delete.java
+47
-0
src/test/java/es_02_index/TestIndex_07_exists.java
src/test/java/es_02_index/TestIndex_07_exists.java
+38
-0
src/test/java/es_02_index/TestIndex_08_close.java
src/test/java/es_02_index/TestIndex_08_close.java
+37
-0
src/test/java/es_02_index/TestIndex_09_open.java
src/test/java/es_02_index/TestIndex_09_open.java
+37
-0
未找到文件。
src/main/resources/application.yml
浏览文件 @
07d36e09
server
:
port
:
40100
spring
:
application
:
name
:
search-service
...
...
src/main/resources/logback-spring.xml
0 → 100644
浏览文件 @
07d36e09
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--定义日志文件的存储地址,使用绝对路径-->
<property
name=
"LOG_HOME"
value=
"/Users/qinyingjie/Downloads/"
/>
<!-- Console 输出设置 -->
<appender
name=
"CONSOLE"
class=
"ch.qos.logback.core.ConsoleAppender"
>
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
<charset>
utf8
</charset>
</encoder>
</appender>
<!-- 按照每天生成日志文件 -->
<appender
name=
"FILE"
class=
"ch.qos.logback.core.rolling.RollingFileAppender"
>
<rollingPolicy
class=
"ch.qos.logback.core.rolling.TimeBasedRollingPolicy"
>
<!--日志文件输出的文件名-->
<fileNamePattern>
${LOG_HOME}/%d{yyyy-MM-dd}.log
</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<!-- 异步输出 -->
<appender
name=
"ASYNC"
class=
"ch.qos.logback.classic.AsyncAppender"
>
<!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
<discardingThreshold>
0
</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>
512
</queueSize>
<!-- 添加附加的appender,最多只能添加一个 -->
<appender-ref
ref=
"FILE"
/>
</appender>
<logger
name=
"org.apache.ibatis.cache.decorators.LoggingCache"
level=
"DEBUG"
additivity=
"false"
>
<appender-ref
ref=
"CONSOLE"
/>
</logger>
<logger
name=
"org.springframework.boot"
level=
"DEBUG"
/>
<root
level=
"info"
>
<!--<appender-ref ref="ASYNC"/>-->
<appender-ref
ref=
"FILE"
/>
<appender-ref
ref=
"CONSOLE"
/>
</root>
</configuration>
\ No newline at end of file
src/test/java/es_01/TestDocument_01_get.java
→
src/test/java/es_01
_document
/TestDocument_01_get.java
浏览文件 @
07d36e09
package
es_01
;
package
es_01
_document
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
...
...
src/test/java/es_01/TestDocument_02_get.java
→
src/test/java/es_01
_document
/TestDocument_02_get.java
浏览文件 @
07d36e09
package
es_01
;
package
es_01
_document
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
...
...
src/test/java/es_01/TestDocument_03_add.java
→
src/test/java/es_01
_document
/TestDocument_03_add.java
浏览文件 @
07d36e09
package
es_01
;
package
es_01
_document
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
...
...
src/test/java/es_01/TestDocument_04_add.java
→
src/test/java/es_01
_document
/TestDocument_04_add.java
浏览文件 @
07d36e09
package
es_01
;
package
es_01
_document
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
...
...
src/test/java/es_01/TestDocument_05_add.java
→
src/test/java/es_01
_document
/TestDocument_05_add.java
浏览文件 @
07d36e09
package
es_01
;
package
es_01
_document
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
...
...
src/test/java/es_01/TestDocument_06_add.java
→
src/test/java/es_01
_document
/TestDocument_06_add.java
浏览文件 @
07d36e09
package
es_01
;
package
es_01
_document
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
...
...
src/test/java/es_01/TestDocument_07_add.java
→
src/test/java/es_01
_document
/TestDocument_07_add.java
浏览文件 @
07d36e09
package
es_01
;
package
es_01
_document
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
...
...
src/test/java/es_01/TestDocument_08_update.java
→
src/test/java/es_01
_document
/TestDocument_08_update.java
浏览文件 @
07d36e09
package
es_01
;
package
es_01
_document
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
...
...
src/test/java/es_01/TestDocument_09_delete.java
→
src/test/java/es_01
_document
/TestDocument_09_delete.java
浏览文件 @
07d36e09
package
es_01
;
package
es_01
_document
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
...
...
src/test/java/es_01/TestDocument_10_bulk.java
→
src/test/java/es_01
_document
/TestDocument_10_bulk.java
浏览文件 @
07d36e09
package
es_01
;
package
es_01
_document
;
import
com.kwan.shuyu.SearchApplication
;
import
lombok.extern.slf4j.Slf4j
;
...
...
src/test/java/es_02_index/TestIndex_01_Create.java
0 → 100644
浏览文件 @
07d36e09
package
es_02_index
;
import
org.elasticsearch.action.admin.indices.alias.Alias
;
import
org.elasticsearch.action.support.ActiveShardCount
;
import
org.elasticsearch.client.IndicesClient
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.client.indices.CreateIndexRequest
;
import
org.elasticsearch.client.indices.CreateIndexResponse
;
import
org.elasticsearch.common.settings.Settings
;
import
org.elasticsearch.common.unit.TimeValue
;
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
;
/**
* 创建索引
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 11:06
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestIndex_01_Create
{
@Autowired
private
RestHighLevelClient
client
;
//创建索引
@Test
public
void
testCreateIndex
()
throws
IOException
{
// PUT /my_index
// {
// "settings": {
// "number_of_shards": 1,
// "number_of_replicas": 1
// },
// "mappings": {
// "properties": {
// "field1":{
// "type": "text"
// },
// "field2":{
// "type": "text"
// }
// }
// },
// "aliases": {
// "default_index": {}
// }
// }
//创建索引对象
CreateIndexRequest
createIndexRequest
=
new
CreateIndexRequest
(
"itheima_book"
);
//设置参数
createIndexRequest
.
settings
(
Settings
.
builder
().
put
(
"number_of_shards"
,
"1"
).
put
(
"number_of_replicas"
,
"0"
));
//指定映射1
createIndexRequest
.
mapping
(
" {\n"
+
" \t\"properties\": {\n"
+
" \"name\":{\n"
+
" \"type\":\"keyword\"\n"
+
" },\n"
+
" \"description\": {\n"
+
" \"type\": \"text\"\n"
+
" },\n"
+
" \"price\":{\n"
+
" \"type\":\"long\"\n"
+
" },\n"
+
" \"pic\":{\n"
+
" \"type\":\"text\",\n"
+
" \"index\":false\n"
+
" }\n"
+
" \t}\n"
+
"}"
,
XContentType
.
JSON
);
//设置别名
createIndexRequest
.
alias
(
new
Alias
(
"itheima_index_new"
));
// 额外参数
//设置超时时间
createIndexRequest
.
setTimeout
(
TimeValue
.
timeValueMinutes
(
2
));
//设置主节点超时时间
createIndexRequest
.
setMasterTimeout
(
TimeValue
.
timeValueMinutes
(
1
));
//在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示
createIndexRequest
.
waitForActiveShards
(
ActiveShardCount
.
from
(
2
));
createIndexRequest
.
waitForActiveShards
(
ActiveShardCount
.
DEFAULT
);
//操作索引的客户端
IndicesClient
indices
=
client
.
indices
();
//执行创建索引库
CreateIndexResponse
createIndexResponse
=
indices
.
create
(
createIndexRequest
,
RequestOptions
.
DEFAULT
);
//得到响应
boolean
acknowledged
=
createIndexResponse
.
isAcknowledged
();
//得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本
boolean
shardsAcknowledged
=
createIndexResponse
.
isShardsAcknowledged
();
System
.
out
.
println
(
"acknowledged:"
+
acknowledged
);
System
.
out
.
println
(
"shardsAcknowledged:"
+
shardsAcknowledged
);
}
}
\ No newline at end of file
src/test/java/es_02_index/TestIndex_02_Create.java
0 → 100644
浏览文件 @
07d36e09
package
es_02_index
;
import
org.elasticsearch.action.admin.indices.alias.Alias
;
import
org.elasticsearch.action.support.ActiveShardCount
;
import
org.elasticsearch.client.IndicesClient
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.client.indices.CreateIndexRequest
;
import
org.elasticsearch.client.indices.CreateIndexResponse
;
import
org.elasticsearch.common.settings.Settings
;
import
org.elasticsearch.common.unit.TimeValue
;
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/13 11:06
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestIndex_02_Create
{
@Autowired
private
RestHighLevelClient
client
;
//创建索引
@Test
public
void
testCreateIndex
()
throws
IOException
{
// PUT /my_index
// {
// "settings": {
// "number_of_shards": 1,
// "number_of_replicas": 1
// },
// "mappings": {
// "properties": {
// "field1":{
// "type": "text"
// },
// "field2":{
// "type": "text"
// }
// }
// },
// "aliases": {
// "default_index": {}
// }
// }
//创建索引对象
CreateIndexRequest
createIndexRequest
=
new
CreateIndexRequest
(
"itheima_book"
);
//设置参数
createIndexRequest
.
settings
(
Settings
.
builder
().
put
(
"number_of_shards"
,
"1"
).
put
(
"number_of_replicas"
,
"0"
));
//指定映射
Map
<
String
,
Object
>
message
=
new
HashMap
<>();
message
.
put
(
"type"
,
"text"
);
Map
<
String
,
Object
>
properties
=
new
HashMap
<>();
properties
.
put
(
"message"
,
message
);
Map
<
String
,
Object
>
mapping
=
new
HashMap
<>();
mapping
.
put
(
"properties"
,
properties
);
createIndexRequest
.
mapping
(
mapping
);
//设置别名
createIndexRequest
.
alias
(
new
Alias
(
"itheima_index_new"
));
// 额外参数
//设置超时时间
createIndexRequest
.
setTimeout
(
TimeValue
.
timeValueMinutes
(
2
));
//设置主节点超时时间
createIndexRequest
.
setMasterTimeout
(
TimeValue
.
timeValueMinutes
(
1
));
//在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示
createIndexRequest
.
waitForActiveShards
(
ActiveShardCount
.
from
(
2
));
createIndexRequest
.
waitForActiveShards
(
ActiveShardCount
.
DEFAULT
);
//操作索引的客户端
IndicesClient
indices
=
client
.
indices
();
//执行创建索引库
CreateIndexResponse
createIndexResponse
=
indices
.
create
(
createIndexRequest
,
RequestOptions
.
DEFAULT
);
//得到响应(全部)
boolean
acknowledged
=
createIndexResponse
.
isAcknowledged
();
//得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本
boolean
shardsAcknowledged
=
createIndexResponse
.
isShardsAcknowledged
();
System
.
out
.
println
(
"acknowledged:"
+
acknowledged
);
System
.
out
.
println
(
"shardsAcknowledged:"
+
shardsAcknowledged
);
}
}
\ No newline at end of file
src/test/java/es_02_index/TestIndex_03_Create.java
0 → 100644
浏览文件 @
07d36e09
package
es_02_index
;
import
org.elasticsearch.action.admin.indices.alias.Alias
;
import
org.elasticsearch.action.support.ActiveShardCount
;
import
org.elasticsearch.client.IndicesClient
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.client.indices.CreateIndexRequest
;
import
org.elasticsearch.client.indices.CreateIndexResponse
;
import
org.elasticsearch.common.settings.Settings
;
import
org.elasticsearch.common.unit.TimeValue
;
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
;
/**
* 创建索引
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 11:06
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestIndex_03_Create
{
@Autowired
private
RestHighLevelClient
client
;
//创建索引
@Test
public
void
testCreateIndex
()
throws
IOException
{
// PUT /my_index
// {
// "settings": {
// "number_of_shards": 1,
// "number_of_replicas": 1
// },
// "mappings": {
// "properties": {
// "field1":{
// "type": "text"
// },
// "field2":{
// "type": "text"
// }
// }
// },
// "aliases": {
// "default_index": {}
// }
// }
//创建索引对象
CreateIndexRequest
createIndexRequest
=
new
CreateIndexRequest
(
"itheima_book"
);
//设置参数
createIndexRequest
.
settings
(
Settings
.
builder
().
put
(
"number_of_shards"
,
"1"
).
put
(
"number_of_replicas"
,
"0"
));
//指定映射
XContentBuilder
builder
=
XContentFactory
.
jsonBuilder
();
builder
.
startObject
();
{
builder
.
startObject
(
"properties"
);
{
builder
.
startObject
(
"message"
);
{
builder
.
field
(
"type"
,
"text"
);
}
builder
.
endObject
();
}
builder
.
endObject
();
}
builder
.
endObject
();
createIndexRequest
.
mapping
(
builder
);
//设置别名
createIndexRequest
.
alias
(
new
Alias
(
"itheima_index_new"
));
// 额外参数
//设置超时时间
createIndexRequest
.
setTimeout
(
TimeValue
.
timeValueMinutes
(
2
));
//设置主节点超时时间
createIndexRequest
.
setMasterTimeout
(
TimeValue
.
timeValueMinutes
(
1
));
//在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示
createIndexRequest
.
waitForActiveShards
(
ActiveShardCount
.
from
(
2
));
createIndexRequest
.
waitForActiveShards
(
ActiveShardCount
.
DEFAULT
);
//操作索引的客户端
IndicesClient
indices
=
client
.
indices
();
//执行创建索引库
CreateIndexResponse
createIndexResponse
=
indices
.
create
(
createIndexRequest
,
RequestOptions
.
DEFAULT
);
//得到响应(全部)
boolean
acknowledged
=
createIndexResponse
.
isAcknowledged
();
//得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本
boolean
shardsAcknowledged
=
createIndexResponse
.
isShardsAcknowledged
();
System
.
out
.
println
(
"acknowledged:"
+
acknowledged
);
System
.
out
.
println
(
"shardsAcknowledged:"
+
shardsAcknowledged
);
}
}
\ No newline at end of file
src/test/java/es_02_index/TestIndex_04_Async.java
0 → 100644
浏览文件 @
07d36e09
package
es_02_index
;
import
org.elasticsearch.action.ActionListener
;
import
org.elasticsearch.action.admin.indices.alias.Alias
;
import
org.elasticsearch.action.support.ActiveShardCount
;
import
org.elasticsearch.client.IndicesClient
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.client.indices.CreateIndexRequest
;
import
org.elasticsearch.client.indices.CreateIndexResponse
;
import
org.elasticsearch.common.settings.Settings
;
import
org.elasticsearch.common.unit.TimeValue
;
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
;
/**
* 异步创建索引
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/13 11:10
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestIndex_04_Async
{
@Autowired
private
RestHighLevelClient
client
;
//创建索引异步方式
@Test
public
void
testCreateIndexAsync
()
{
//创建索引对象
CreateIndexRequest
createIndexRequest
=
new
CreateIndexRequest
(
"itheima_book"
);
//设置参数
createIndexRequest
.
settings
(
Settings
.
builder
().
put
(
"number_of_shards"
,
"1"
).
put
(
"number_of_replicas"
,
"0"
));
//指定映射1
createIndexRequest
.
mapping
(
" {\n"
+
" \t\"properties\": {\n"
+
" \"name\":{\n"
+
" \"type\":\"keyword\"\n"
+
" },\n"
+
" \"description\": {\n"
+
" \"type\": \"text\"\n"
+
" },\n"
+
" \"price\":{\n"
+
" \"type\":\"long\"\n"
+
" },\n"
+
" \"pic\":{\n"
+
" \"type\":\"text\",\n"
+
" \"index\":false\n"
+
" }\n"
+
" \t}\n"
+
"}"
,
XContentType
.
JSON
);
//设置别名
createIndexRequest
.
alias
(
new
Alias
(
"itheima_index_new"
));
// 额外参数
//设置超时时间
createIndexRequest
.
setTimeout
(
TimeValue
.
timeValueMinutes
(
2
));
//设置主节点超时时间
createIndexRequest
.
setMasterTimeout
(
TimeValue
.
timeValueMinutes
(
1
));
//在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示
createIndexRequest
.
waitForActiveShards
(
ActiveShardCount
.
from
(
2
));
createIndexRequest
.
waitForActiveShards
(
ActiveShardCount
.
DEFAULT
);
//操作索引的客户端
IndicesClient
indices
=
client
.
indices
();
//执行创建索引库
ActionListener
<
CreateIndexResponse
>
listener
=
new
ActionListener
<
CreateIndexResponse
>()
{
@Override
public
void
onResponse
(
CreateIndexResponse
createIndexResponse
)
{
//得到响应(全部)
boolean
acknowledged
=
createIndexResponse
.
isAcknowledged
();
//得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本
boolean
shardsAcknowledged
=
createIndexResponse
.
isShardsAcknowledged
();
System
.
out
.
println
(
"acknowledged:"
+
acknowledged
);
System
.
out
.
println
(
"shardsAcknowledged:"
+
shardsAcknowledged
);
}
@Override
public
void
onFailure
(
Exception
e
)
{
e
.
printStackTrace
();
}
};
client
.
indices
().
createAsync
(
createIndexRequest
,
RequestOptions
.
DEFAULT
,
listener
);
try
{
Thread
.
sleep
(
5000
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
src/test/java/es_02_index/TestIndex_05_delete.java
0 → 100644
浏览文件 @
07d36e09
package
es_02_index
;
import
org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest
;
import
org.elasticsearch.action.support.master.AcknowledgedResponse
;
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/13 11:11
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestIndex_05_delete
{
@Autowired
private
RestHighLevelClient
client
;
@Test
public
void
testDeleteIndex
()
throws
IOException
{
//创建删除索引请求
DeleteIndexRequest
deleteIndexRequest
=
new
DeleteIndexRequest
(
"itheima_book"
);
// 执行
AcknowledgedResponse
delete
=
client
.
indices
().
delete
(
deleteIndexRequest
,
RequestOptions
.
DEFAULT
);
//得到相应
boolean
acknowledged
=
delete
.
isAcknowledged
();
System
.
out
.
println
(
"acknowledged:"
+
acknowledged
);
}
}
\ No newline at end of file
src/test/java/es_02_index/TestIndex_06_delete.java
0 → 100644
浏览文件 @
07d36e09
package
es_02_index
;
import
org.elasticsearch.action.ActionListener
;
import
org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest
;
import
org.elasticsearch.action.support.master.AcknowledgedResponse
;
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/13 11:13
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestIndex_06_delete
{
@Autowired
private
RestHighLevelClient
client
;
@Test
public
void
testDeleteIndexAsync
()
throws
IOException
{
//创建删除索引请求
DeleteIndexRequest
deleteIndexRequest
=
new
DeleteIndexRequest
(
"itheima_book"
);
// 执行
ActionListener
<
AcknowledgedResponse
>
listener
=
new
ActionListener
<
AcknowledgedResponse
>()
{
@Override
public
void
onResponse
(
AcknowledgedResponse
acknowledgedResponse
)
{
//得到相应
boolean
acknowledged
=
acknowledgedResponse
.
isAcknowledged
();
System
.
out
.
println
(
"acknowledged:"
+
acknowledged
);
}
@Override
public
void
onFailure
(
Exception
e
)
{
e
.
printStackTrace
();
}
};
}
}
\ No newline at end of file
src/test/java/es_02_index/TestIndex_07_exists.java
0 → 100644
浏览文件 @
07d36e09
package
es_02_index
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.client.indices.GetIndexRequest
;
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/13 11:14
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestIndex_07_exists
{
@Autowired
private
RestHighLevelClient
client
;
@Test
public
void
testExistIndex
()
throws
IOException
{
GetIndexRequest
request
=
new
GetIndexRequest
(
"itheima_book"
);
//参数
request
.
local
(
false
);
//从主节点返回本地索引信息状态
request
.
humanReadable
(
true
);
//以适合人类的格式返回
request
.
includeDefaults
(
false
);
//是否返回每个索引的所有默认配置
boolean
exists
=
client
.
indices
().
exists
(
request
,
RequestOptions
.
DEFAULT
);
System
.
out
.
println
(
"exists:"
+
exists
);
}
}
\ No newline at end of file
src/test/java/es_02_index/TestIndex_08_close.java
0 → 100644
浏览文件 @
07d36e09
package
es_02_index
;
import
org.elasticsearch.action.admin.indices.close.CloseIndexRequest
;
import
org.elasticsearch.action.support.master.AcknowledgedResponse
;
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/13 11:14
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestIndex_08_close
{
@Autowired
private
RestHighLevelClient
client
;
//关闭索引
@Test
public
void
testCloseIndex
()
throws
IOException
{
CloseIndexRequest
request
=
new
CloseIndexRequest
(
"itheima_book"
);
AcknowledgedResponse
close
=
client
.
indices
().
close
(
request
,
RequestOptions
.
DEFAULT
);
boolean
acknowledged
=
close
.
isAcknowledged
();
System
.
out
.
println
(
"acknowledged:"
+
acknowledged
);
}
}
\ No newline at end of file
src/test/java/es_02_index/TestIndex_09_open.java
0 → 100644
浏览文件 @
07d36e09
package
es_02_index
;
import
org.elasticsearch.action.admin.indices.open.OpenIndexRequest
;
import
org.elasticsearch.action.admin.indices.open.OpenIndexResponse
;
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/13 11:14
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TestIndex_09_open
{
@Autowired
private
RestHighLevelClient
client
;
//开启索引
@Test
public
void
testOpenIndex
()
throws
IOException
{
OpenIndexRequest
request
=
new
OpenIndexRequest
(
"itheima_book"
);
OpenIndexResponse
open
=
client
.
indices
().
open
(
request
,
RequestOptions
.
DEFAULT
);
boolean
acknowledged
=
open
.
isAcknowledged
();
System
.
out
.
println
(
"acknowledged:"
+
acknowledged
);
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录