Commit 46b0ef9e by huangchengyi

1.0

parent d2747670
Showing with 29 additions and 10 deletions
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"os" "os"
"search_server/boot" "search_server/boot"
"search_server/pkg/es" "search_server/pkg/es"
"strings"
) )
func main() { func main() {
...@@ -32,11 +33,16 @@ func main() { ...@@ -32,11 +33,16 @@ func main() {
//fmt.Println(attrName) //fmt.Println(attrName)
//os.Exit(1) //os.Exit(1)
//dd := service.OutLink("LM358","-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) println(result,err)
os.Exit(1) os.Exit(1)
......
...@@ -3,6 +3,7 @@ package es ...@@ -3,6 +3,7 @@ package es
import ( import (
"github.com/imroc/req" "github.com/imroc/req"
"math/rand" "math/rand"
"net/http"
"search_server/pkg/config" "search_server/pkg/config"
) )
...@@ -40,17 +41,29 @@ POST /_bulk ...@@ -40,17 +41,29 @@ POST /_bulk
{ "index":{"_index":"hcy1","_type":"goods","_id":"6666"} } { "index":{"_index":"hcy1","_type":"goods","_id":"6666"} }
{ "name":"mary smith","age":32 } { "name":"mary smith","age":32 }
eg: eg:
es.BulkES(`{"index":{"_index":"hcy1","_type":"goods","_id":"8888888"} } 方法一:
{ "name":"john doe","age":25 } 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"
{ "index":{"_index":"hcy1","_type":"goods","_id":"9999999"} } result,err := es.BulkES(param)
{ "name":"mary smith","age":32 }`) 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) { func BulkES(param string) (result string, err error) {
endpoints := config.Get("es.urls").Strings(",") endpoints := config.Get("es.urls").Strings(",")
esUrl := endpoints[rand.Intn(len(endpoints))] //随机获取一个节点进行请求 esUrl := endpoints[rand.Intn(len(endpoints))] //随机获取一个节点进行请求
params := req.BodyJSON(param) //params := req.BodyJSON(param)
resp, err := req.Post(esUrl+"/_bulk", params) //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 { if err != nil {
return return
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment