Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录
切换导航
L
library
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
stbz
library
Commits
c8d92aa2
提交
c8d92aa2
authored
1月 12, 2022
作者:
limeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
整理代码
上级
86ff6ece
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
31 行增加
和
20 行删除
+31
-20
service.go
elastic/service.go
+29
-20
struct.go
elastic/struct.go
+2
-0
没有找到文件。
elastic/service.go
浏览文件 @
c8d92aa2
...
@@ -16,37 +16,40 @@ const (
...
@@ -16,37 +16,40 @@ const (
)
)
//根据文档Id 快速获取内容
//根据文档Id 快速获取内容
func
GetDoc
(
req
GetDocReq
)
{
func
GetDoc
(
req
GetDocReq
)
(
docContent
*
elastic
.
GetResult
,
err
error
)
{
client
,
err
:=
connection
()
client
,
err
:=
connection
()
if
err
!=
nil
||
client
==
nil
{
if
err
!=
nil
||
client
==
nil
{
err
=
errors
.
New
(
"elastic连接失败
"
)
err
=
errors
.
New
(
"elastic连接失败
:"
+
err
.
Error
()
)
return
return
}
}
defer
client
.
Stop
()
defer
client
.
Stop
()
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
5
*
time
.
Second
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
5
*
time
.
Second
)
defer
cancel
()
defer
cancel
()
docContent
,
err
:=
client
.
Get
()
.
Index
(
req
.
IndexName
)
.
Id
(
req
.
DocId
)
.
Do
(
ctx
)
docContent
,
err
=
client
.
Get
()
.
Index
(
req
.
IndexName
)
.
Id
(
req
.
DocId
)
.
Do
(
ctx
)
fmt
.
Println
(
docContent
)
if
err
!=
nil
{
err
=
errors
.
New
(
"获取文档失败,错误原因:"
+
err
.
Error
())
return
}
return
return
}
}
//根据搜索条件 获取总数
//根据搜索条件 获取总数
func
GetCount
(
req
GetCountReq
)
(
count
int64
)
{
func
GetCount
(
req
GetCountReq
)
(
count
int64
,
err
error
)
{
client
,
err
:=
connection
()
client
,
err
:=
connection
()
if
err
!=
nil
||
client
==
nil
{
if
err
!=
nil
||
client
==
nil
{
err
=
errors
.
New
(
"elastic连接失败"
)
err
=
errors
.
New
(
"elastic连接失败"
+
err
.
Error
()
)
return
return
}
}
defer
client
.
Stop
()
defer
client
.
Stop
()
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
5
*
time
.
Second
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
5
*
time
.
Second
)
defer
cancel
()
defer
cancel
()
countService
:=
client
.
Count
(
"index_name"
)
.
Pretty
(
true
)
countService
:=
client
.
Count
(
req
.
IndexName
)
.
Pretty
(
true
)
count
,
err
=
countService
.
Query
(
req
.
Condition
)
.
Do
(
ctx
)
count
,
err
=
countService
.
Query
(
req
.
Condition
)
.
Do
(
ctx
)
if
err
!=
nil
{
if
err
!=
nil
{
err
=
errors
.
New
(
"获取总数失败,错误原因:"
+
err
.
Error
())
return
return
}
}
return
return
count
}
}
//根据搜索条件 获取数据
//根据搜索条件 获取数据
...
@@ -56,14 +59,14 @@ func GetList(req GetListReq) (searchResult *elastic.SearchResult) {
...
@@ -56,14 +59,14 @@ func GetList(req GetListReq) (searchResult *elastic.SearchResult) {
//连接Es
//连接Es
client
,
err
:=
connection
()
client
,
err
:=
connection
()
if
err
!=
nil
||
client
==
nil
{
if
err
!=
nil
||
client
==
nil
{
err
=
errors
.
New
(
"elastic连接失败"
)
err
=
errors
.
New
(
"elastic连接失败"
+
err
.
Error
()
)
return
return
}
}
defer
client
.
Stop
()
defer
client
.
Stop
()
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
5
*
time
.
Second
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
5
*
time
.
Second
)
defer
cancel
()
defer
cancel
()
//准备查询条件
//准备查询条件
searchService
:=
client
.
Search
()
.
Index
(
"index_name"
)
.
Pretty
(
true
)
.
Query
(
req
.
Condition
)
searchService
:=
client
.
Search
()
.
Index
(
req
.
IndexName
)
.
Pretty
(
true
)
.
Query
(
req
.
Condition
)
if
isSearchAfter
==
false
{
if
isSearchAfter
==
false
{
//分页查询Es
//分页查询Es
...
@@ -101,7 +104,7 @@ func Bulk(req BulkReq) (bulkIndex *BulkIndex, err error) {
...
@@ -101,7 +104,7 @@ func Bulk(req BulkReq) (bulkIndex *BulkIndex, err error) {
bulkIndexAdd
:=
new
(
elastic
.
BulkIndexRequest
)
bulkIndexAdd
:=
new
(
elastic
.
BulkIndexRequest
)
bulkIndexUpdate
:=
new
(
elastic
.
BulkUpdateRequest
)
bulkIndexUpdate
:=
new
(
elastic
.
BulkUpdateRequest
)
bulkIndexDel
:=
new
(
elastic
.
BulkDeleteRequest
)
bulkIndexDel
:=
new
(
elastic
.
BulkDeleteRequest
)
fmt
.
Println
(
bulkIndexUpdate
,
bulkIndexDel
)
switch
req
.
Type
{
switch
req
.
Type
{
case
"add"
:
case
"add"
:
bulkIndexAdd
=
BulkAdd
(
req
.
IndexName
)
bulkIndexAdd
=
BulkAdd
(
req
.
IndexName
)
...
@@ -145,7 +148,7 @@ func BulkUpdate(indexName string) (indexReq *elastic.BulkUpdateRequest) {
...
@@ -145,7 +148,7 @@ func BulkUpdate(indexName string) (indexReq *elastic.BulkUpdateRequest) {
func
BulkDo
(
esRequest
[]
elastic
.
BulkableRequest
,
ctx
context
.
Context
)
(
bulkResponse
*
elastic
.
BulkResponse
,
err
error
)
{
func
BulkDo
(
esRequest
[]
elastic
.
BulkableRequest
,
ctx
context
.
Context
)
(
bulkResponse
*
elastic
.
BulkResponse
,
err
error
)
{
client
,
err
:=
connection
()
client
,
err
:=
connection
()
if
err
!=
nil
||
client
==
nil
{
if
err
!=
nil
||
client
==
nil
{
err
=
errors
.
New
(
"elastic连接失败"
)
err
=
errors
.
New
(
"elastic连接失败"
+
err
.
Error
()
)
return
return
}
}
defer
client
.
Stop
()
defer
client
.
Stop
()
...
@@ -159,7 +162,7 @@ func BulkDo(esRequest []elastic.BulkableRequest, ctx context.Context) (bulkRespo
...
@@ -159,7 +162,7 @@ func BulkDo(esRequest []elastic.BulkableRequest, ctx context.Context) (bulkRespo
func
UpdateByScript
(
req
UpdateByScriptReq
)
(
err
error
)
{
func
UpdateByScript
(
req
UpdateByScriptReq
)
(
err
error
)
{
client
,
err
:=
connection
()
client
,
err
:=
connection
()
if
err
!=
nil
||
client
==
nil
{
if
err
!=
nil
||
client
==
nil
{
err
=
errors
.
New
(
"elastic连接失败"
)
err
=
errors
.
New
(
"elastic连接失败"
+
err
.
Error
()
)
return
return
}
}
defer
client
.
Stop
()
defer
client
.
Stop
()
...
@@ -167,16 +170,21 @@ func UpdateByScript(req UpdateByScriptReq) (err error) {
...
@@ -167,16 +170,21 @@ func UpdateByScript(req UpdateByScriptReq) (err error) {
defer
cancel
()
defer
cancel
()
_
,
err
=
client
.
Update
()
.
Index
(
"1"
)
.
Id
(
req
.
DocId
)
.
Routing
(
req
.
RoutingId
)
.
Script
(
elastic
.
NewScript
(
req
.
ScriptCtx
))
.
Do
(
ctx
)
_
,
err
=
client
.
Update
()
.
Index
(
"1"
)
.
Id
(
req
.
DocId
)
.
Routing
(
req
.
RoutingId
)
.
Script
(
elastic
.
NewScript
(
req
.
ScriptCtx
))
.
Do
(
ctx
)
if
err
!=
nil
{
if
err
!=
nil
{
err
=
errors
.
New
(
"script更新文档失败"
+
err
.
Error
())
return
return
}
}
return
return
}
}
//同BulkDo BulkDo http处理 BulkProcessor tcp处理 相对更安全些
//同BulkDo BulkDo http处理 BulkProcessor tcp处理 相对更安全些
func
BulkProcessor
(
esRequest
[]
elastic
.
BulkableRequest
,
ctx
context
.
Context
,
numDocs
int
)
{
func
BulkProcessor
(
esRequest
[]
elastic
.
BulkableRequest
,
ctx
context
.
Context
,
numDocs
int
)
(
err
error
){
if
numDocs
>
30
{
err
=
errors
.
New
(
"请合理输入参数,批量处理最大为30"
)
return
}
client
,
err
:=
connection
()
client
,
err
:=
connection
()
if
err
!=
nil
||
client
==
nil
{
if
err
!=
nil
||
client
==
nil
{
err
=
errors
.
New
(
"elastic连接失败"
)
err
=
errors
.
New
(
"elastic连接失败"
+
err
.
Error
()
)
return
return
}
}
defer
client
.
Stop
()
defer
client
.
Stop
()
...
@@ -200,14 +208,15 @@ func BulkProcessor(esRequest []elastic.BulkableRequest, ctx context.Context,numD
...
@@ -200,14 +208,15 @@ func BulkProcessor(esRequest []elastic.BulkableRequest, ctx context.Context,numD
}
}
atomic
.
AddInt64
(
&
afterRequests
,
int64
(
len
(
requests
)))
atomic
.
AddInt64
(
&
afterRequests
,
int64
(
len
(
requests
)))
if
response
.
Errors
{
if
response
.
Errors
{
logs
.
Error
(
"
1"
)
logs
.
Error
(
"
%s"
,
response
.
Failed
()
)
}
else
{
}
else
{
logs
.
Info
(
"log"
,
"序号:{
} ,执行 {} 条数据批量操作成功,共耗费{
}毫秒"
,
executionId
,
len
(
requests
),
response
.
Took
)
logs
.
Info
(
"log"
,
"序号:{
%s} ,执行 {%s} 条数据批量操作成功,共耗费{%d
}毫秒"
,
executionId
,
len
(
requests
),
response
.
Took
)
}
}
}
}
//每添加30个request,执行一次bulk操作
//每添加30个request,执行一次bulk操作
p
,
err
:=
client
.
BulkProcessor
()
.
Name
(
"Worker-1"
)
.
Before
(
beforeFn
)
.
After
(
afterFn
)
.
Stats
(
true
)
.
BulkActions
(
30
)
.
Do
(
ctx
)
p
,
err
:=
client
.
BulkProcessor
()
.
Name
(
"Worker-1"
)
.
Before
(
beforeFn
)
.
After
(
afterFn
)
.
Stats
(
true
)
.
BulkActions
(
numDocs
)
.
Do
(
ctx
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
err
)
fmt
.
Println
(
err
)
}
}
...
@@ -232,5 +241,5 @@ func BulkProcessor(esRequest []elastic.BulkableRequest, ctx context.Context,numD
...
@@ -232,5 +241,5 @@ func BulkProcessor(esRequest []elastic.BulkableRequest, ctx context.Context,numD
fmt
.
Printf
(
"Worker %d: Number of requests queued: %d
\n
"
,
i
,
w
.
Queued
)
fmt
.
Printf
(
"Worker %d: Number of requests queued: %d
\n
"
,
i
,
w
.
Queued
)
fmt
.
Printf
(
" Last response time : %v
\n
"
,
w
.
LastDuration
)
fmt
.
Printf
(
" Last response time : %v
\n
"
,
w
.
LastDuration
)
}
}
return
}
}
elastic/struct.go
浏览文件 @
c8d92aa2
...
@@ -22,12 +22,14 @@ type GetDocReq struct {
...
@@ -22,12 +22,14 @@ type GetDocReq struct {
}
}
type
GetCountReq
struct
{
type
GetCountReq
struct
{
Condition
*
elastic
.
BoolQuery
Condition
*
elastic
.
BoolQuery
IndexName
string
`json:"index_name"`
}
}
type
GetListReq
struct
{
type
GetListReq
struct
{
Condition
*
elastic
.
BoolQuery
Condition
*
elastic
.
BoolQuery
Page
int
`json:"page"`
Page
int
`json:"page"`
Limit
int
`json:"limit"`
Limit
int
`json:"limit"`
SearchAfter
*
SearchAfter
`json:"search_after"`
SearchAfter
*
SearchAfter
`json:"search_after"`
IndexName
string
`json:"index_name"`
}
}
type
SearchAfter
struct
{
type
SearchAfter
struct
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论