Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
easy-es
easy-es
提交
929cb21c
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 搜索 >>
提交
929cb21c
编写于
12月 01, 2021
作者:
FelixHPP
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 修改批量新增文档错误问题
上级
3c743163
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
87 addition
and
26 deletion
+87
-26
easy-es-core/src/main/java/indi/felix/easy/core/elastic/EasyEs.java
...re/src/main/java/indi/felix/easy/core/elastic/EasyEs.java
+33
-20
easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/AbstractService.java
.../indi/felix/easy/core/elastic/client/AbstractService.java
+0
-1
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java
...java/indi/felix/easy/core/elastic/rest/SearchRestApi.java
+4
-4
easy-es-core/src/main/java/indi/felix/easy/core/elastic/utool/Const.java
...c/main/java/indi/felix/easy/core/elastic/utool/Const.java
+5
-1
easy-es-core/src/test/java/indi/felix/easy/core/elastic/EasyEsTest.java
...rc/test/java/indi/felix/easy/core/elastic/EasyEsTest.java
+45
-0
未找到文件。
easy-es-core/src/main/java/indi/felix/easy/core/elastic/EasyEs.java
浏览文件 @
929cb21c
...
...
@@ -177,30 +177,43 @@ public class EasyEs {
return
getEsRest
(
client
);
}
public
String
bulkAddDoc
(
String
index
,
List
<
Map
>
objects
)
{
// public String getEsVersion(){
// client.getLowLevelClient().
// return responseBody(defaultMethod, newEndpoint);
// }
public
void
bulkAddDoc
(
String
index
,
List
<
Map
>
objects
)
{
BulkRequest
request
=
new
BulkRequest
();
BulkResponse
bulkResponse
=
null
;
try
{
if
(
objects
==
null
||
objects
.
size
()
<
1
)
{
throw
new
indi
.
felix
.
easy
.
core
.
exceptions
.
ElasticsearchException
(
"mappers can not be empty"
);
}
for
(
int
i
=
0
;
i
<
objects
.
size
();
i
++)
{
Map
mapper
=
objects
.
get
(
i
);
IndexRequest
indexRequest
=
new
IndexRequest
(
index
,
DOC_TYPE
);
EsUtils
.
setDefaultProperties
(
indexRequest
,
mapper
);
request
.
add
(
indexRequest
.
opType
(
"create"
)
.
source
(
JSON
.
toJSONString
(
mapper
),
XContentType
.
JSON
));
}
bulkResponse
=
client
.
bulk
(
request
,
RequestOptions
.
DEFAULT
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
if
(!
rest
().
indexExists
(
index
)){
throw
new
ElasticsearchException
(
"index is not exists"
);
}
if
(
objects
==
null
||
objects
.
size
()
<
1
)
{
throw
new
ElasticsearchException
(
"mappers can not be empty"
);
}
RestStatus
stat
=
bulkResponse
.
status
();
return
bulkResponse
.
toString
();
search
().
bulkInsert
(
index
,
objects
,
2000
);
//
// BulkResponse bulkResponse = null;
// try {
// if (objects == null || objects.size() < 1) {
// throw new ElasticsearchException("mappers can not be empty");
// }
//
// for (int i = 0; i < objects.size(); i++) {
// Map mapper = objects.get(i);
// IndexRequest indexRequest = new IndexRequest(index);
// EsUtils.setDefaultProperties(indexRequest, mapper);
//
// request.add(indexRequest
// .opType("create")
// .source(JSON.toJSONString(mapper), XContentType.JSON));
// }
// bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
// } catch (IOException e) {
// e.printStackTrace();
// }
// RestStatus stat = bulkResponse.status();
}
}
easy-es-core/src/main/java/indi/felix/easy/core/elastic/client/AbstractService.java
浏览文件 @
929cb21c
...
...
@@ -135,7 +135,6 @@ public abstract class AbstractService {
}
catch
(
Exception
e
)
{
logger
.
error
(
"the failed json element index range is {} - {}."
,
count
-
cursor
,
count
);
logger
.
error
(
"Cause by: {}."
,
e
.
getMessage
());
e
.
printStackTrace
();
}
if
(
failure
.
size
()
>
0
)
{
...
...
easy-es-core/src/main/java/indi/felix/easy/core/elastic/rest/SearchRestApi.java
浏览文件 @
929cb21c
...
...
@@ -41,7 +41,7 @@ public class SearchRestApi extends BaseRest {
* @return
*/
public
String
requestDsl
(
String
index
,
String
dsl
)
{
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/
{1}/_search"
,
index
,
DOC_TYPE
);
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/
_search"
,
index
);
return
responseBody
(
defaultMethod
,
newEndpoint
,
dsl
);
}
...
...
@@ -54,7 +54,7 @@ public class SearchRestApi extends BaseRest {
* @return
*/
public
String
requestById
(
String
index
,
String
id
)
{
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/{1}
/{2}"
,
index
,
DOC_TYPE
,
id
);
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/{1}
"
,
index
,
id
);
return
responseBody
(
defaultMethod
,
newEndpoint
);
}
...
...
@@ -71,7 +71,7 @@ public class SearchRestApi extends BaseRest {
* @return
*/
public
String
requestByIds
(
String
index
,
String
...
ids
)
{
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/{1}
/{2}"
,
index
,
DOC_TYPE
,
"_mget"
);
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/{1}
"
,
index
,
"_mget"
);
XContentBuilder
builder
=
null
;
try
{
...
...
@@ -130,7 +130,7 @@ public class SearchRestApi extends BaseRest {
public
String
requestById
(
String
id
)
{
String
indexs
=
String
.
join
(
","
,
defaultIndex
);
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/{
1}/{2}"
,
indexs
,
DOC_TYPE
,
id
);
String
newEndpoint
=
MessageFormat
.
format
(
"/{0}/{
2}"
,
indexs
,
id
);
return
responseBody
(
defaultMethod
,
newEndpoint
);
}
...
...
easy-es-core/src/main/java/indi/felix/easy/core/elastic/utool/Const.java
浏览文件 @
929cb21c
...
...
@@ -2,7 +2,11 @@ package indi.felix.easy.core.elastic.utool;
public
class
Const
{
public
static
final
String
DOC_TYPE
=
"doc"
;
/*
* 注意: 在7.x中type移除, 为了保证6.x和7.x路径一致, doctype强制使用_doc
* include_type_name参数设为true
*/
public
static
final
String
DOC_TYPE
=
"_doc"
;
/**
* 默认的ID字段, 对象中如果有该字段, 默认存储ES将按该字段设置ID
...
...
easy-es-core/src/test/java/indi/felix/easy/core/elastic/EasyEsTest.java
0 → 100644
浏览文件 @
929cb21c
package
indi.felix.easy.core.elastic
;
import
junit.framework.TestCase
;
import
org.elasticsearch.index.query.TermQueryBuilder
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
EasyEsTest
extends
TestCase
{
private
static
final
String
ip
=
"localhost:9200"
;
public
void
testBulkAddDoc
()
{
EasyEs
easyEs
=
new
EasyEs
(
ip
);
String
index
=
"test_index"
;
List
<
Map
>
object
=
new
ArrayList
<>();
Map
<
String
,
Object
>
m1
=
new
HashMap
<>();
m1
.
put
(
"name"
,
"ceshi1"
);
m1
.
put
(
"age"
,
2
);
Map
<
String
,
Object
>
m2
=
new
HashMap
<>();
m2
.
put
(
"name"
,
"dsadsa"
);
m2
.
put
(
"age"
,
12
);
Map
<
String
,
Object
>
m3
=
new
HashMap
<>();
m3
.
put
(
"name"
,
"sdav"
);
m3
.
put
(
"age"
,
40
);
object
.
add
(
m1
);
object
.
add
(
m2
);
object
.
add
(
m3
);
easyEs
.
bulkAddDoc
(
index
,
object
);
TermQueryBuilder
termQueryBuilder
=
new
TermQueryBuilder
(
"age"
,
12
);
String
res
=
easyEs
.
rest
().
search
().
requestByQuery
(
index
,
termQueryBuilder
);
System
.
out
.
println
(
res
);
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录