Commit 46b0ef9e by huangchengyi

1.0

parent d2747670
Showing with 29 additions and 10 deletions
......@@ -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)
......
......@@ -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
}
......
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