Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
search_server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
46b0ef9e
authored
Jul 15, 2020
by
huangchengyi
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
1.0
parent
d2747670
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
10 deletions
cmd/test1.go
pkg/es/es.go
cmd/test1.go
View file @
46b0ef9e
...
...
@@ -5,6 +5,7 @@ import (
"os"
"search_server/boot"
"search_server/pkg/es"
"strings"
)
func
main
()
{
...
...
@@ -32,11 +33,16 @@ func main() {
//fmt.Println(attrName)
//os.Exit(1)
//dd := service.OutLink("LM358","-1")
result
,
err
:=
es
.
BulkES
(
`{ "index":{"_index":"hcy1","_type":"goods","_id":"8888888"} }
{ "name":"john doe","age":25 }
{ "index":{"_index":"hcy1","_type":"goods","_id":"9999999"} }
{ "name":"mary smith","age":32 }`
)
lines
:=
[]
string
{
`{"index":{"_index":"hcy1","_type":"goods","_id":"s1"} }`
,
`{"name":"john doe","age":25 }`
,
`{"index":{"_index":"hcy1","_type":"goods","_id":"s2"} }`
,
`{"name":"mary smith","age":32 }`
,
}
param
:=
strings
.
Join
(
lines
,
"
\n
"
)
+
"
\n
"
result
,
err
:=
es
.
BulkES
(
param
)
println
(
result
,
err
)
os
.
Exit
(
1
)
...
...
pkg/es/es.go
View file @
46b0ef9e
...
...
@@ -3,6 +3,7 @@ package es
import
(
"github.com/imroc/req"
"math/rand"
"net/http"
"search_server/pkg/config"
)
...
...
@@ -40,17 +41,29 @@ POST /_bulk
{ "index":{"_index":"hcy1","_type":"goods","_id":"6666"} }
{ "name":"mary smith","age":32 }
eg:
es.BulkES(`{"index":{"_index":"hcy1","_type":"goods","_id":"8888888"} }
{ "name":"john doe","age":25 }
{ "index":{"_index":"hcy1","_type":"goods","_id":"9999999"} }
{ "name":"mary smith","age":32 }`)
方法一:
param := `{"index":{"_index":"hcy1","_type":"goods","_id":"s1"} }`+"\n"+`{"name":"john doe","age":25 }`+"\n"+`{"index":{"_index":"hcy1","_type":"goods","_id":"s2"} }`+"\n"+`{"name":"john doe","age":25 }`+"\n"
result,err := es.BulkES(param)
println(result,err)
方法二:
lines := []string{
`{"index":{"_index":"hcy1","_type":"goods","_id":"s1"} }`,
`{"name":"john doe","age":25 }`,
`{"index":{"_index":"hcy1","_type":"goods","_id":"s2"} }`,
`{"name":"mary smith","age":32 }`,
}
param := strings.Join(lines, "\n")+"\n"
*/
func
BulkES
(
param
string
)
(
result
string
,
err
error
)
{
endpoints
:=
config
.
Get
(
"es.urls"
)
.
Strings
(
","
)
esUrl
:=
endpoints
[
rand
.
Intn
(
len
(
endpoints
))]
//随机获取一个节点进行请求
params
:=
req
.
BodyJSON
(
param
)
resp
,
err
:=
req
.
Post
(
esUrl
+
"/_bulk"
,
params
)
//params := req.BodyJSON(param)
//req.Debug = true
header
:=
make
(
http
.
Header
)
header
.
Set
(
"Content-Type"
,
"application/x-ndjson"
)
resp
,
err
:=
req
.
Post
(
esUrl
+
"/_bulk"
,
header
,
param
)
if
err
!=
nil
{
return
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment