Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
easy-es
easy-es
提交
aee90670
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 搜索 >>
提交
aee90670
编写于
11月 01, 2022
作者:
FelixHPP
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
build1.3.7版本 处理创建索引是 number 不兼容问题
上级
bce63fc6
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
203 addition
and
4 deletion
+203
-4
easy-es-core/pom.xml
easy-es-core/pom.xml
+6
-1
easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/XContentUtils.java
...va/indi/felix/easy/core/elastic/client/XContentUtils.java
+1
-0
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/BaseRest.java
...main/java/indi/felix/easy/core/elastic/rest/BaseRest.java
+1
-0
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java
...java/indi/felix/easy/core/elastic/rest/SearchRestApi.java
+172
-2
easy-es-core/src/test/java/indi/felix/easy/core/elastic/rest/SearchRestApiTest.java
.../indi/felix/easy/core/elastic/rest/SearchRestApiTest.java
+22
-0
pom.xml
pom.xml
+1
-1
未找到文件。
easy-es-core/pom.xml
浏览文件 @
aee90670
...
...
@@ -5,7 +5,7 @@
<parent>
<artifactId>
easy-es
</artifactId>
<groupId>
indi.felix.easy
</groupId>
<version>
1.3.
6
</version>
<version>
1.3.
7
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
easy-es-core
</artifactId>
...
...
@@ -100,5 +100,10 @@
<version>
1.2
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
com.squareup.okhttp3
</groupId>
<artifactId>
okhttp
</artifactId>
<version>
3.9.0
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/XContentUtils.java
浏览文件 @
aee90670
...
...
@@ -165,6 +165,7 @@ public class XContentUtils {
break
;
case
"integer"
:
case
"int"
:
case
"number"
:
NumberFieldMetadata
integetFieldMetadata
=
(
NumberFieldMetadata
)
baseFieldMetadata
;
builder
.
startObject
(
curName
);
builder
.
field
(
"type"
,
integetFieldMetadata
.
getType
());
...
...
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/BaseRest.java
浏览文件 @
aee90670
...
...
@@ -117,6 +117,7 @@ public abstract class BaseRest {
}
response
=
restClient
.
performRequest
(
request
);
responseBody
=
EntityUtils
.
toString
(
response
.
getEntity
(),
"UTF-8"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
...
...
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java
浏览文件 @
aee90670
package
indi.felix.easy.core.elastic.rest
;
import
com.alibaba.fastjson.JSONObject
;
import
indi.felix.easy.core.elastic.client.RequestHelper
;
import
indi.felix.easy.core.elastic.client.XContentUtils
;
import
okhttp3.*
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.HttpStatus
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.client.utils.HttpClientUtils
;
import
org.apache.http.client.utils.URIBuilder
;
import
org.apache.http.entity.ContentType
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.HttpClientBuilder
;
import
org.apache.http.nio.entity.NStringEntity
;
import
org.apache.http.util.EntityUtils
;
import
org.elasticsearch.client.RestClient
;
import
org.elasticsearch.common.bytes.BytesReference
;
import
org.elasticsearch.common.xcontent.XContentBuilder
;
...
...
@@ -12,16 +26,27 @@ import org.elasticsearch.common.xcontent.XContentHelper;
import
org.elasticsearch.index.query.QueryBuilder
;
import
org.elasticsearch.search.builder.SearchSourceBuilder
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.text.MessageFormat
;
import
java.util.
Arrays
;
import
java.util.
Collections
;
import
java.util.
*
;
import
java.util.
concurrent.TimeUnit
;
import
static
indi
.
felix
.
easy
.
core
.
elastic
.
utool
.
Const
.
DOC_TYPE
;
public
class
SearchRestApi
extends
BaseRest
{
private
SearchRest
rest
;
private
OkHttpClient
httpClient
;
public
SearchRestApi
(
RestClient
restClient
)
{
super
(
restClient
);
httpClient
=
new
OkHttpClient
.
Builder
()
.
connectTimeout
(
60
,
TimeUnit
.
SECONDS
)
// default 10s
.
writeTimeout
(
30
,
TimeUnit
.
SECONDS
)
// default 10s
.
readTimeout
(
30
,
TimeUnit
.
SECONDS
)
// default 10s
.
build
();
rest
=
new
SearchRest
();
}
...
...
@@ -30,6 +55,7 @@ public class SearchRestApi extends BaseRest {
String
endpoint
=
"_search"
;
public
SearchRest
getRest
(){
return
rest
;
}
...
...
@@ -59,6 +85,150 @@ public class SearchRestApi extends BaseRest {
return
responseBody
(
defaultMethod
,
newEndpoint
,
dsl
);
}
private
URI
buildUri
(
String
pathPrefix
,
String
path
,
Map
<
String
,
String
>
params
)
{
Objects
.
requireNonNull
(
path
,
"path must not be null"
);
try
{
String
fullPath
;
if
(
pathPrefix
!=
null
&&
!
pathPrefix
.
isEmpty
())
{
if
(
pathPrefix
.
endsWith
(
"/"
)
&&
path
.
startsWith
(
"/"
))
{
fullPath
=
pathPrefix
.
substring
(
0
,
pathPrefix
.
length
()
-
1
)
+
path
;
}
else
if
(!
pathPrefix
.
endsWith
(
"/"
)
&&
!
path
.
startsWith
(
"/"
))
{
fullPath
=
pathPrefix
+
"/"
+
path
;
}
else
{
fullPath
=
pathPrefix
+
path
;
}
}
else
{
fullPath
=
path
;
}
URIBuilder
uriBuilder
=
new
URIBuilder
(
fullPath
);
Iterator
var5
=
params
.
entrySet
().
iterator
();
while
(
var5
.
hasNext
())
{
Map
.
Entry
<
String
,
String
>
param
=
(
Map
.
Entry
)
var5
.
next
();
uriBuilder
.
addParameter
((
String
)
param
.
getKey
(),
(
String
)
param
.
getValue
());
}
return
uriBuilder
.
build
();
}
catch
(
URISyntaxException
var7
)
{
throw
new
IllegalArgumentException
(
var7
.
getMessage
(),
var7
);
}
};
public
String
httpSearchUrl
(
String
index
)
{
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/_search"
,
index
);
try
{
Map
<
String
,
String
>
params
=
Collections
.
singletonMap
(
"pretty"
,
"true"
);
org
.
elasticsearch
.
client
.
Request
pingRequest
=
RequestHelper
.
getRequest
(
"GET"
,
"/"
,
params
);
org
.
elasticsearch
.
client
.
Response
pingResponse
=
null
;
pingResponse
=
restClient
.
performRequest
(
pingRequest
);
String
url
=
pingResponse
.
getHost
().
toURI
()
+
newEndpoint
;
return
url
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
""
;
}
public
String
httpSearch
(
String
index
,
String
dsl
)
{
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/_search"
,
index
);
try
{
// 先ping一下获取URL
Map
<
String
,
String
>
params
=
Collections
.
singletonMap
(
"pretty"
,
"true"
);
org
.
elasticsearch
.
client
.
Request
pingRequest
=
RequestHelper
.
getRequest
(
"GET"
,
"/"
,
params
);
org
.
elasticsearch
.
client
.
Response
pingResponse
=
restClient
.
performRequest
(
pingRequest
);
String
url
=
pingResponse
.
getHost
().
toURI
()
+
newEndpoint
;
try
{
HttpClient
hclient
=
HttpClientBuilder
.
create
().
build
();
HttpPost
post
=
new
HttpPost
(
url
);
// 解决中文乱码问题
StringEntity
entity
=
new
StringEntity
(
dsl
,
"utf-8"
);
entity
.
setContentEncoding
(
"UTF-8"
);
entity
.
setContentType
(
"application/json"
);
post
.
setEntity
(
entity
);
HttpResponse
result11
=
hclient
.
execute
(
post
);
// 请求发送成功,并得到响应
if
(
result11
.
getStatusLine
().
getStatusCode
()
==
HttpStatus
.
SC_OK
)
{
String
str
=
""
;
try
{
// 读取服务器返回过来的json字符串数据
str
=
EntityUtils
.
toString
(
result11
.
getEntity
(),
"utf-8"
);
// 把json字符串转换成json对象
// jsonResult = JSONObject.parseObject(str);
}
catch
(
Exception
e
)
{
log
.
error
(
"post请求提交失败:"
+
url
,
e
);
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
RequestBody
body
=
RequestBody
.
create
(
MediaType
.
parse
(
"application/x-www-form-urlencoded; charset=utf-8"
),
dsl
);
Request
request
=
new
Request
.
Builder
()
.
url
(
url
)
// 可以字符串,也可以是HttpUrl
// .addHeader("Content-Type", "application/json")
.
post
(
body
)
.
build
();
Call
call
=
httpClient
.
newCall
(
request
);
// call.enqueue(new Callback() {
// @Override
// public void onFailure(Call call, IOException e) {
// log.info("whq登录","失败了");
// }
//
// @Override
// public void onResponse(Call call, Response response) throws IOException {
// log.info("whq登录",response+"---------response---------");
// log.info("whq登录",response.message()+"---------message---------");
// log.info("whq登录",response.body().toString()+"------------------");
// }
// });
Response
resp
=
null
;
resp
=
call
.
execute
();
System
.
out
.
println
(
resp
.
body
().
toString
());
if
(
resp
.
isSuccessful
())
{
//return response;
}
else
{
throw
new
IOException
(
"Unexpected code "
+
resp
);
}
if
(
resp
.
code
()
==
200
)
{
System
.
out
.
println
(
"Response: "
+
resp
.
body
().
string
());
}
else
{
// Error handle
System
.
out
.
println
(
"Code:"
+
resp
.
code
()
+
", Msg:"
+
resp
.
message
());
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
// return responseBody;
// return responseBody(defaultMethod, newEndpoint, dsl);
return
null
;
}
/**
* 查询指定ID的文档
* 如果索引设置了routing, 会报routing_missing_exception 异常
...
...
easy-es-core/src/test/java/indi/felix/easy/core/elastic/rest/SearchRestApiTest.java
浏览文件 @
aee90670
package
indi.felix.easy.core.elastic.rest
;
import
indi.felix.easy.core.elastic.EasyEs
;
import
indi.felix.easy.core.elastic.client.ClientConfigProperties
;
import
junit.framework.TestCase
;
import
org.elasticsearch.index.query.TermQueryBuilder
;
public
class
SearchRestApiTest
extends
TestCase
{
private
static
final
String
ip
=
"localhost:9200"
;
public
void
testAuth
()
{
ClientConfigProperties
properties
=
new
ClientConfigProperties
();
properties
.
setHost
(
ip
);
properties
.
setUsername
(
"admin"
);
properties
.
setPassword
(
"csm@123!"
);
EasyEs
easyEs
=
new
EasyEs
(
properties
);
String
re1s
=
easyEs
.
rest
().
cat
().
indices
();
System
.
out
.
println
(
re1s
);
Boolean
re2s
=
easyEs
.
search
().
indexExist
(
"sub_project_125"
);
Boolean
re3s
=
easyEs
.
search
().
indexExist
(
"sub_project_121115"
);
System
.
out
.
println
(
re2s
);
}
public
void
testRequestByIds
()
{
EasyEs
easyEs
=
new
EasyEs
(
ip
);
String
testIndexName
=
"edc_index"
;
...
...
pom.xml
浏览文件 @
aee90670
...
...
@@ -7,7 +7,7 @@
<groupId>
indi.felix.easy
</groupId>
<artifactId>
easy-es
</artifactId>
<packaging>
pom
</packaging>
<version>
1.3.
6
</version>
<version>
1.3.
7
</version>
<modules>
<module>
easy-es-core
</module>
<!-- <module>easy-es-spring</module>-->
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录