Commit 7c66b1b2 by mushishixian

es请求

parent 460cb341
...@@ -11,7 +11,7 @@ api_url = http://192.168.2.232:60004 ...@@ -11,7 +11,7 @@ api_url = http://192.168.2.232:60004
[es] [es]
url = http://192.168.2.232:9200 url = http://192.168.2.232:9200
urls = http://192.168.2.232:9200,http://192.168.2.232:9200 urls = http://192.168.2.232:9200,http://192.168.2.232:9200
index_name = future,rochester,tme,verical,element14,digikey,chip1stop,aipco,arrow,alliedelec,avnet,mouser,peigenesis,powell,rs,buerklin index_name = future,rochester,tme,verical,element14,digikey,chip1stop,aipco,arrow,alliedelec,avnet,mouser,zhuanmai,peigenesis,powell,rs,buerklin,liexin_ziying
[database] [database]
user_name = root user_name = root
......
...@@ -5,12 +5,26 @@ import ( ...@@ -5,12 +5,26 @@ import (
"search_server/pkg/config" "search_server/pkg/config"
) )
func CurlEs(param string) { type Response struct {
goodsServerUrl := config.Get("es.api_url").String() Took int `json:"took"`
TimeOut bool `json:"time_out"`
Shard map[string]int `json:"_shard"`
HitsResult HitsResult `json:"hits"`
}
type HitsResult struct {
Total int `json:"total"`
MaxScore float64 `json:"max_score"`
Hits string `json:"hits"`
}
func CurlES(index string, param string) (err error, result string) {
goodsServerUrl := config.Get("es.url").String()
params := req.BodyJSON(param) params := req.BodyJSON(param)
resp, err := req.Post(goodsServerUrl+"/synchronization", params) resp, err := req.Get(goodsServerUrl+"/"+index+"/_search", params)
if err != nil { if err != nil {
return return
} }
return resp.String() result = resp.String()
return
} }
...@@ -46,3 +46,6 @@ mgo包的文档 : https://godoc.org/gopkg.in/mgo.v2 ...@@ -46,3 +46,6 @@ mgo包的文档 : https://godoc.org/gopkg.in/mgo.v2
#### 6.商品信息获取 #### 6.商品信息获取
直接调用```service.GetGoodsInfo```函数即可,传入goods_id的字符串,多个goods_id用逗号隔开(商品服务的请求本来亦是如此),会返回json字符串,然后可以根据自己的需求用gjson获取对应的信息即可 直接调用```service.GetGoodsInfo```函数即可,传入goods_id的字符串,多个goods_id用逗号隔开(商品服务的请求本来亦是如此),会返回json字符串,然后可以根据自己的需求用gjson获取对应的信息即可
#### 7.ES的基本请求
直接调用```es.CurlES```函数即可,传入索引名称和请求的json参数即可,得到的返回结果是ES的原生结果,可以根据自己的需求用gjson获取对应的信息
...@@ -2,8 +2,11 @@ package main ...@@ -2,8 +2,11 @@ package main
import ( import (
"flag" "flag"
"fmt"
"log" "log"
"search_server/boot" "search_server/boot"
"search_server/pkg/config"
"search_server/pkg/es"
) )
func main() { func main() {
...@@ -13,4 +16,31 @@ func main() { ...@@ -13,4 +16,31 @@ func main() {
if err := boot.Boot(path); err != nil { if err := boot.Boot(path); err != nil {
log.Println(err) log.Println(err)
} }
fmt.Println(es.CurlES(config.Get("es.index_name").String(), `{
"query": {
"bool": {
"filter": [
{
"term": {
"goods_id": "1154020374326836400"
}
}
]
}
},
"sort": [
{
"single_price": {
"order": "asc"
}
},
{
"_score": {
"order": "desc"
}
}
],
"from": 0,
"size": 10
}`))
} }
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