Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
easy-es
easy-es
提交
6db3fbbb
E
easy-es
项目概览
easy-es
/
easy-es
通知
96
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
easy-es
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6db3fbbb
编写于
3月 25, 2022
作者:
FelixHPP
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
处理不同版本不能识别include_type_name报错问题
上级
9acb1c13
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
173 addition
and
33 deletion
+173
-33
easy-es-core/src/main/java/indi/felix/easy/core/elastic/EasyEs.java
...re/src/main/java/indi/felix/easy/core/elastic/EasyEs.java
+28
-0
easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/DSLEnum.java
...ain/java/indi/felix/easy/core/elastic/client/DSLEnum.java
+6
-2
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/BaseRest.java
...main/java/indi/felix/easy/core/elastic/rest/BaseRest.java
+10
-0
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/ClusterRestApi.java
...ava/indi/felix/easy/core/elastic/rest/ClusterRestApi.java
+2
-1
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/EasyRest.java
...main/java/indi/felix/easy/core/elastic/rest/EasyRest.java
+8
-2
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/IndexRestApi.java
.../java/indi/felix/easy/core/elastic/rest/IndexRestApi.java
+48
-24
easy-es-core/src/test/java/indi/felix/easy/core/elastic/rest/EasyRestTest.java
.../java/indi/felix/easy/core/elastic/rest/EasyRestTest.java
+71
-4
未找到文件。
easy-es-core/src/main/java/indi/felix/easy/core/elastic/EasyEs.java
浏览文件 @
6db3fbbb
...
...
@@ -7,14 +7,19 @@ import indi.felix.easy.core.elastic.client.EasyEsService;
import
indi.felix.easy.core.elastic.utool.Const
;
import
indi.felix.easy.core.elastic.utool.EsUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.elasticsearch.Build
;
import
org.elasticsearch.ElasticsearchException
;
import
org.elasticsearch.Version
;
import
org.elasticsearch.action.bulk.BulkRequest
;
import
org.elasticsearch.action.bulk.BulkResponse
;
import
org.elasticsearch.action.index.IndexRequest
;
import
org.elasticsearch.action.main.MainResponse
;
import
org.elasticsearch.client.ClusterClient
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
indi.felix.easy.core.elastic.client.ClientManage
;
import
indi.felix.easy.core.elastic.rest.EasyRest
;
import
org.elasticsearch.cluster.ClusterName
;
import
org.elasticsearch.common.xcontent.XContentType
;
import
org.elasticsearch.rest.RestStatus
;
...
...
@@ -37,6 +42,8 @@ public class EasyEs {
private
volatile
EasyEsService
esService
;
private
volatile
EasyRest
esRest
;
private
String
version
;
/**
* 一个集群名称 创建一个实例
*/
...
...
@@ -139,6 +146,8 @@ public class EasyEs {
String
newHost
=
String
.
join
(
","
,
host
);
HOST
=
newHost
;
client
=
getSingleClient
(
newHost
);
getEsInfo
();
}
...
...
@@ -146,6 +155,8 @@ public class EasyEs {
public
EasyEs
(
ClientConfigProperties
params
)
{
HOST
=
params
.
getHost
();
client
=
getSingleClient
(
params
);
getEsInfo
();
}
/**
...
...
@@ -157,6 +168,23 @@ public class EasyEs {
return
client
.
ping
(
RequestOptions
.
DEFAULT
);
}
public
MainResponse
getEsInfo
(){
MainResponse
response
=
null
;
try
{
response
=
client
.
info
(
RequestOptions
.
DEFAULT
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
//返回集群的各种信息
ClusterName
clusterName
=
response
.
getClusterName
();
//集群名称
String
clusterUuid
=
response
.
getClusterUuid
();
//群集的唯一标识符
String
nodeName
=
response
.
getNodeName
();
//已执行请求的节点的名称
Version
version
=
response
.
getVersion
();
//已执行请求的节点的版本
Build
build
=
response
.
getBuild
();
//已执行请求的节点的构建信息
return
response
;
}
/**
* 调用高级客户端search接口
...
...
easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/DSLEnum.java
浏览文件 @
6db3fbbb
...
...
@@ -106,7 +106,7 @@ public enum DSLEnum {
private
String
requestType
;
private
String
dslMethod
;
// 默认的模板
private
String
dslTemp
;
DSLEnum
(
String
requestType
,
String
dslMethod
,
String
dslTemp
)
{
...
...
@@ -131,7 +131,11 @@ public enum DSLEnum {
public
static
DSLEnum
getByQuery
(
String
requestType
,
String
dslMethod
)
{
if
(
StringUtils
.
isNotEmpty
(
requestType
)
&&
StringUtils
.
isNotEmpty
(
dslMethod
))
{
for
(
DSLEnum
dslEnum
:
DSLEnum
.
values
())
{
if
(
dslEnum
.
getRequestType
().
equals
(
requestType
)
&&
dslEnum
.
getDslMethod
().
equals
(
dslMethod
))
{
requestType
=
requestType
.
toLowerCase
();
dslMethod
=
dslMethod
.
toLowerCase
();
String
curRequestType
=
dslEnum
.
getRequestType
().
toLowerCase
();
String
curDslMethod
=
dslEnum
.
getDslMethod
().
toLowerCase
();
if
(
curRequestType
.
equals
(
requestType
)
&&
curDslMethod
.
equals
(
dslMethod
))
{
return
dslEnum
;
}
}
...
...
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/BaseRest.java
浏览文件 @
6db3fbbb
...
...
@@ -11,6 +11,7 @@ import org.apache.http.HttpEntity;
import
org.apache.http.entity.ContentType
;
import
org.apache.http.nio.entity.NStringEntity
;
import
org.apache.http.util.EntityUtils
;
import
org.elasticsearch.action.main.MainResponse
;
import
org.elasticsearch.client.Request
;
import
org.elasticsearch.client.Response
;
import
org.elasticsearch.client.RestClient
;
...
...
@@ -33,6 +34,7 @@ public abstract class BaseRest {
public
static
final
String
GET
=
"GET"
,
POST
=
"POST"
,
PUT
=
"PUT"
,
DELETE
=
"DELETE"
,
HEAD
=
"HEAD"
;
public
String
version
;
public
String
defaultDocType
;
private
final
static
String
DEFAULT_ES_VERSION
=
"6.1.3"
;
...
...
@@ -94,6 +96,14 @@ public abstract class BaseRest {
return
responseBody
;
}
/**
* 判断版本是否允许用include_type_name
* @return
*/
protected
boolean
isAllowIncludeType
(){
return
!(
compareVersion
(
this
.
version
,
"7.0.0"
)
>
0
||
compareVersion
(
this
.
version
,
"6.3.0"
)
<
0
);
}
private
String
response
(
String
method
,
String
endpoint
,
Map
<
String
,
String
>
params
,
Header
[]
headers
,
String
dsl
)
{
Response
response
=
null
;
String
responseBody
=
null
;
...
...
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/ClusterRestApi.java
浏览文件 @
6db3fbbb
...
...
@@ -36,7 +36,7 @@ public class ClusterRestApi extends BaseRest {
if
(
index
.
length
==
1
){
newEndpoint
=
endpoint
+
"/state/"
+
index
[
0
];
}
else
{
newEndpoint
=
endpoint
+
"/state/_all/
"
+
String
.
join
(
","
,
index
);
newEndpoint
=
endpoint
+
"/state/_all/"
+
String
.
join
(
","
,
index
);
}
return
responseBody
(
defaultMethod
,
newEndpoint
);
...
...
@@ -104,6 +104,7 @@ public class ClusterRestApi extends BaseRest {
return
responseBody
(
defaultMethod
,
newEndpoint
);
}
// todo setSetting
public
String
setSetting
()
{
String
newEndpoint
=
endpoint
+
"/settings"
;
return
responseBody
(
PUT
,
newEndpoint
);
...
...
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/EasyRest.java
浏览文件 @
6db3fbbb
...
...
@@ -191,8 +191,14 @@ public class EasyRest extends BaseRest {
map
.
put
(
"mappings"
,
mappingMap
);
String
jsonString
=
JSON
.
toJSONString
(
map
);
// 6.x版本无法识别include_type_name参数
// String newEndpoint = index + "?include_type_name=true";
String
newEndpoint
=
"/"
+
index
;
String
newEndpoint
=
""
;
if
(
isAllowIncludeType
()){
newEndpoint
=
index
+
"?include_type_name=true"
;
}
else
{
newEndpoint
=
"/"
+
index
;
}
Request
request
=
new
Request
(
"PUT"
,
newEndpoint
);
...
...
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/IndexRestApi.java
浏览文件 @
6db3fbbb
...
...
@@ -2,7 +2,6 @@ package indi.felix.easy.core.elastic.rest;
import
indi.felix.easy.core.elastic.client.XContentUtils
;
import
indi.felix.easy.core.elastic.model.ESFieldTypeEnum
;
import
indi.felix.easy.core.elastic.model.ElasticConstant
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.entity.ContentType
;
import
org.apache.http.nio.entity.NStringEntity
;
...
...
@@ -18,9 +17,6 @@ import java.text.MessageFormat;
import
java.util.Collections
;
import
static
indi
.
felix
.
easy
.
core
.
elastic
.
utool
.
Const
.
DOC_TYPE
;
import
static
org
.
elasticsearch
.
common
.
xcontent
.
ToXContent
.
EMPTY_PARAMS
;
import
static
org
.
elasticsearch
.
common
.
xcontent
.
XContentFactory
.
jsonBuilder
;
import
static
org
.
elasticsearch
.
common
.
xcontent
.
XContentHelper
.
toXContent
;
public
class
IndexRestApi
extends
BaseRest
{
public
IndexRestApi
(
RestClient
restClient
)
{
...
...
@@ -35,12 +31,19 @@ public class IndexRestApi extends BaseRest {
* 查看集群状态 api
* curl -XHEAD 'localhost:9200/twitter
*
* @param index, 多个索引用英文逗号分隔
* @return
*/
public
boolean
exists
(
String
index
)
{
// 6.x版本无法识别include_type_name参数
// String newEndpoint = index + "?include_type_name=true";
String
newEndpoint
=
"/"
+
index
;
String
newEndpoint
=
""
;
if
(!
isAllowIncludeType
())
{
// 7.x 版本中_doc 为默认路径
newEndpoint
=
"/"
+
index
;
}
else
{
// 6.x版本无法识别include_type_name参数
newEndpoint
=
index
+
"?include_type_name=true"
;
}
Response
response
=
res
(
HEAD
,
newEndpoint
,
Collections
.
EMPTY_MAP
,
null
);
int
code
=
response
.
getStatusLine
().
getStatusCode
();
return
code
==
200
;
...
...
@@ -60,10 +63,16 @@ public class IndexRestApi extends BaseRest {
/**
* curl -XGET 'localhost:9200/_mapping
*
* @return
* @return
多个索引用英文逗号分隔
*/
public
String
getMapping
()
{
String
newEndpoint
=
"_mapping"
;
String
newEndpoint
=
""
;
if
(!
isAllowIncludeType
())
{
newEndpoint
=
"_mapping"
;
}
else
{
// 6.x版本无法识别include_type_name参数
newEndpoint
=
"_mapping?include_type_name=true"
;
}
return
responseBody
(
defaultMethod
,
newEndpoint
);
}
...
...
@@ -74,7 +83,14 @@ public class IndexRestApi extends BaseRest {
* @return
*/
public
String
getMapping
(
String
index
)
{
String
newEndpoint
=
index
+
"/_mapping"
;
String
newEndpoint
=
""
;
if
(!
isAllowIncludeType
())
{
newEndpoint
=
index
+
"/_mapping"
;
}
else
{
// 6.x版本无法识别include_type_name参数
newEndpoint
=
index
+
"/_mapping?include_type_name=true"
;
}
return
responseBody
(
defaultMethod
,
newEndpoint
);
}
...
...
@@ -84,7 +100,14 @@ public class IndexRestApi extends BaseRest {
* @return
*/
public
String
putMapping
(
String
index
,
String
body
)
{
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/_mapping/{1}"
,
index
,
DOC_TYPE
);
String
newEndpoint
=
""
;
if
(!
isAllowIncludeType
())
{
newEndpoint
=
MessageFormat
.
format
(
"/{0}/_mapping/{1}"
,
index
,
DOC_TYPE
);
}
else
{
// 6.x版本无法识别include_type_name参数
newEndpoint
=
MessageFormat
.
format
(
"/{0}/_mapping/{1}?include_type_name=true"
,
index
,
DOC_TYPE
);
}
return
responseBody
(
PUT
,
newEndpoint
,
body
);
}
...
...
@@ -116,18 +139,19 @@ public class IndexRestApi extends BaseRest {
return
null
;
}
public
String
putDateProperty
(
String
index
,
String
field
)
{
String
newEndpoint
=
index
+
"/_mapping/"
+
DOC_TYPE
+
"?pretty"
;
String
jsonStr
=
"{\n"
+
" \"properties\": {\n"
+
" \" "
+
field
+
"\": {\n"
+
" \"type\": \"date\"\n"
+
" }\n"
+
" }\n"
+
"}"
;
HttpEntity
entity
=
new
NStringEntity
(
jsonStr
,
ContentType
.
APPLICATION_JSON
);
return
responseBody
(
PUT
,
newEndpoint
,
Collections
.
emptyMap
(),
entity
);
}
// public String putDateProperty(String index, String field) {
// String newEndpoint = index + "/_mapping/" + DOC_TYPE + "?pretty";
//
// String jsonStr = "{\n" +
// " \"properties\": {\n" +
// " \" " + field + "\": {\n" +
// " \"type\": \"date\"\n" +
// " }\n" +
// " }\n" +
// "}";
// HttpEntity entity = new NStringEntity(jsonStr, ContentType.APPLICATION_JSON);
//
// return responseBody(PUT, newEndpoint, Collections.emptyMap(), entity);
// }
}
easy-es-core/src/test/java/indi/felix/easy/core/elastic/rest/EasyRestTest.java
浏览文件 @
6db3fbbb
...
...
@@ -2,9 +2,11 @@ package indi.felix.easy.core.elastic.rest;
import
indi.felix.easy.core.elastic.EasyEs
;
import
indi.felix.easy.core.elastic.model.ESFieldTypeEnum
;
import
indi.felix.easy.core.enums.EndpointType
;
import
junit.framework.TestCase
;
import
org.junit.Assert
;
import
java.io.UnsupportedEncodingException
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -43,8 +45,8 @@ public class EasyRestTest extends TestCase {
easyEs
.
rest
().
document
().
addDoc
(
testIndexName
,
"123457"
,
"123457"
,
map2
);
Assert
.
assertEquals
(
"{\"docs\":[{\"_index\":\"test_20210907\",\"_type\":\"_doc\",\"_id\":\"123456\",\"_version\":1,\"_seq_no\":1,\"_primary_term\":1,\"found\":true,\"_source\":{\"name\":\"李四\",\"age\":\"11\"}}]}"
,
easyEs
.
rest
().
search
().
requestById
(
testIndexName
,
"123456"
));
//
Assert.assertEquals("{\"docs\":[{\"_index\":\"test_20210907\",\"_type\":\"_doc\",\"_id\":\"123456\",\"_version\":1,\"_seq_no\":1,\"_primary_term\":1,\"found\":true,\"_source\":{\"name\":\"李四\",\"age\":\"11\"}}]}",
//
easyEs.rest().search().requestById(testIndexName, "123456"));
easyEs
.
rest
().
document
().
deleteDoc
(
testIndexName
,
"123457"
);
...
...
@@ -55,14 +57,14 @@ public class EasyRestTest extends TestCase {
public
void
testIndex
()
{
EasyEs
easyEs
=
new
EasyEs
(
ip
);
String
testIndexName
=
"test_20210907"
;
String
testIndexName
=
"test_2021090
123
7"
;
easyEs
.
rest
().
createIndex
(
testIndexName
);
// 获取全部索引
String
allMapping
=
easyEs
.
rest
().
index
().
getMapping
();
System
.
out
.
println
(
allMapping
);
String
mapping
=
easyEs
.
rest
().
index
().
getMapping
(
testIndexName
);
Assert
.
assertEquals
(
"{\"test_20210907\":{\"mappings\":{\"doc\":{}}}}"
,
mapping
);
Assert
.
assertEquals
(
"{\"test_2021090
123
7\":{\"mappings\":{\"doc\":{}}}}"
,
mapping
);
//putMapping test
...
...
@@ -81,6 +83,71 @@ public class EasyRestTest extends TestCase {
.
putMapping
(
testIndexName
,
"name11"
,
ESFieldTypeEnum
.
KEYWORD
);
System
.
out
.
println
(
easyEs
.
rest
().
index
().
getMapping
(
testIndexName
));
easyEs
.
rest
().
deleteIndex
(
testIndexName
);
}
public
void
testIndexExists
(){
EasyEs
easyEs
=
new
EasyEs
(
ip
);
String
testIndexName
=
"test_202109077"
;
easyEs
.
rest
().
createIndex
(
testIndexName
);
String
testIndexName1
=
"test_202109088"
;
easyEs
.
rest
().
createIndex
(
testIndexName1
);
Assert
.
assertTrue
(
easyEs
.
rest
().
indexExists
(
testIndexName
));
Assert
.
assertTrue
(
easyEs
.
rest
().
indexExists
(
testIndexName1
));
Assert
.
assertFalse
(
easyEs
.
rest
().
indexExists
(
"asdada"
));
Assert
.
assertTrue
(
easyEs
.
rest
().
indexExists
(
testIndexName
+
","
+
testIndexName1
));
Assert
.
assertFalse
(
easyEs
.
rest
().
indexExists
(
testIndexName
+
",aaaa"
));
System
.
out
.
println
(
easyEs
.
rest
().
index
().
getMapping
());
System
.
out
.
println
(
easyEs
.
rest
().
index
().
getMapping
(
testIndexName
));
System
.
out
.
println
(
easyEs
.
rest
().
index
().
getMapping
(
testIndexName
+
","
+
testIndexName1
));
System
.
out
.
println
(
easyEs
.
rest
().
stats
());
// cat TEST
System
.
out
.
println
(
easyEs
.
rest
().
cat
().
aliases
());
System
.
out
.
println
(
easyEs
.
rest
().
cat
().
allocation
());
System
.
out
.
println
(
easyEs
.
rest
().
cat
().
count
());
System
.
out
.
println
(
easyEs
.
rest
().
cat
().
health
());
System
.
out
.
println
(
easyEs
.
rest
().
cat
().
master
());
System
.
out
.
println
(
easyEs
.
rest
().
cat
().
nodeAttrs
());
System
.
out
.
println
(
easyEs
.
rest
().
cat
().
shards
());
System
.
out
.
println
(
easyEs
.
rest
().
cat
().
fielddata
());
System
.
out
.
println
(
easyEs
.
rest
().
cat
().
count
(
testIndexName
));
// cluster TEST
System
.
out
.
println
(
easyEs
.
rest
().
cluster
().
health
());
System
.
out
.
println
(
easyEs
.
rest
().
cluster
().
state
());
System
.
out
.
println
(
easyEs
.
rest
().
cluster
().
state
(
testIndexName
,
testIndexName1
));
System
.
out
.
println
(
easyEs
.
rest
().
cluster
().
health
(
testIndexName
));
System
.
out
.
println
(
easyEs
.
rest
().
cluster
().
nodesStats
());
System
.
out
.
println
(
easyEs
.
rest
().
cluster
().
nodesStats
(
testIndexName
,
testIndexName1
));
System
.
out
.
println
(
easyEs
.
rest
().
cluster
().
getSetting
());
// DSL
try
{
Map
<
String
,
Object
>
res
=
easyEs
.
rest
().
queryDsl
(
"POST"
,
EndpointType
.
ANALYZER
,
testIndexName
,
"{\n"
+
" \"analyzer\": \"whitespace\",\n"
+
" \"text\": \"The quick brown fox.\"\n"
+
"}\n"
);
System
.
out
.
println
(
res
.
toString
());
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
}
public
void
testMain
(){
testIndexExists
();
testPing
();
testIndex
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录