From 78265dd08a17210ac67c1f82e2514b572c4734c8 Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期五, 12 七月 2019 15:42:01 +0800
Subject: [PATCH] fix query by id Multiple Id ([]id)
---
esutil/EsApi.go | 145 ++++++++-------
esutil/EsClient.go | 389 +++++++++++++++++++++---------------------
2 files changed, 270 insertions(+), 264 deletions(-)
diff --git a/esutil/EsApi.go b/esutil/EsApi.go
index 4de958e..7e7cbf1 100644
--- a/esutil/EsApi.go
+++ b/esutil/EsApi.go
@@ -1,24 +1,26 @@
package esutil
import (
- "errors"
- "strconv"
+ "errors"
+ "fmt"
+ "strconv"
+ "strings"
- "basic.com/pubsub/protomsg.git"
-)
+ "basic.com/pubsub/protomsg.git"
+ )
// 鏌ヨ搴曞簱浜哄憳淇℃伅
-func DbPersoninfos(queryindex int, querynums int) ([]*protomsg.Esinfo, error) {
- var dbinfos []*protomsg.Esinfo
- point := strconv.Itoa(queryindex)
- number := strconv.Itoa(querynums)
+func DbPersoninfos( queryindex int, querynums int ) ([]*protomsg.Esinfo, error){
+ var dbinfos []*protomsg.Esinfo
+ point := strconv.Itoa(queryindex)
+ number := strconv.Itoa(querynums)
- var dbinfoRequest = ` {
- "from": ` + point + `,
+ var dbinfoRequest = ` {
+ "from": ` + point + `,
"query": {
"match_all": {}
},
- "size":` + number + `,
+ "size":`+ number +`,
"_source": [
"id",
"tableId",
@@ -26,34 +28,34 @@
]
}`
- buf, err := EsReq("POST", "http://192.168.1.182:9200/dbtablepersons/dbpersons/_search", []byte(dbinfoRequest))
- if err != nil {
- return dbinfos, errors.New("http request dbtablename info is err!")
- }
+ buf, err := EsReq("POST", "http://192.168.1.182:9200/dbtablepersons/dbpersons/_search",[]byte(dbinfoRequest))
+ if err != nil {
+ return dbinfos ,errors.New("http request dbtablename info is err!")
+ }
- // 杩斿洖 _source 鏁扮粍
- sources, err := Sourcelist(buf)
- if err != nil {
- return dbinfos, err
- }
+ // 杩斿洖 _source 鏁扮粍
+ sources, err := Sourcelist(buf)
+ if err != nil {
+ return dbinfos,err
+ }
- // 杩斿洖鎵�鏈夋煡璇㈢殑鏁版嵁
- dbpersoninfos := Parsesources(sources)
- return dbpersoninfos, nil
+ // 杩斿洖鎵�鏈夋煡璇㈢殑鏁版嵁
+ dbpersoninfos := Parsesources(sources)
+ return dbpersoninfos, nil
}
// 鏍规嵁搴曞簱id鏌ヨ搴曞簱淇℃伅
-func Dbtablefosbyid(id string) (protomsg.Dbtable, error) {
- var dbinfo protomsg.Dbtable
-
- var dbinfoRequest = `
+func Dbtablefosbyid (id []string) ([]protomsg.Dbtable, error) {
+ var dbinfo []protomsg.Dbtable
+ dbtableId := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1)
+ var dbinfoRequest = `
{
"query": {
"bool": {
"filter": [{
"terms": {
"_id": [
- "` + id + `"
+ "`+ dbtableId +`"
]
}
}]
@@ -61,32 +63,32 @@
}
}
`
- buf, err := EsReq("POST", "http://192.168.1.182:9200/dbtables/_search", []byte(dbinfoRequest))
- if err != nil {
- return dbinfo, err
- }
+ buf, err := EsReq("POST", "http://192.168.1.182:9200/dbtables/_search",[]byte(dbinfoRequest))
+ if err != nil {
+ return dbinfo, err
+ }
- sources, err := Sourcelist(buf)
- if err != nil {
- return dbinfo, err
- }
+ sources, err := Sourcelist(buf)
+ if err != nil {
+ return dbinfo , err
+ }
- dbtable := Dbtablebyid(sources)
- return dbtable, nil
+ dbtable := Dbtablebyid(sources)
+ return dbtable, nil
}
// 鏍规嵁搴曞簱浜哄憳id鏌ヨ搴曞簱浜哄憳淇℃伅
-func Dbpersoninfosbyid(id string) (protomsg.Dbperson, error) {
- var dbinfo protomsg.Dbperson
-
- var dbinfoRequest = `
+func Dbpersoninfosbyid (id []string) ([]protomsg.Dbperson, error) {
+ var dbinfo []protomsg.Dbperson
+ dbtablePersonId := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1)
+ var dbinfoRequest = `
{
"query": {
"bool": {
"filter": [{
"terms": {
"_id": [
- "` + id + `"
+ "`+ dbtablePersonId +`"
]
}
}]
@@ -94,29 +96,30 @@
}
}
`
- buf, err := EsReq("POST", "http://192.168.1.182:9200/dbtablepersons/_search", []byte(dbinfoRequest))
- if err != nil {
- return dbinfo, err
- }
+ buf, err := EsReq("POST", "http://192.168.1.182:9200/dbtablepersons/_search",[]byte(dbinfoRequest))
+ if err != nil {
+ return dbinfo, err
+ }
- sources, err := Sourcelist(buf)
- if err != nil {
- return dbinfo, err
- }
+ sources, err := Sourcelist(buf)
+ if err != nil {
+ return dbinfo , err
+ }
- dbperson := Dbpersonbyid(sources)
- return dbperson, nil
+ dbperson := Dbpersonbyid(sources)
+ println(dbperson)
+ return dbperson,nil
}
// 鏍规嵁tableid 鏌ヨtablename
-func Dbtablename(tableid string) (tablename string, err error) {
- var dbinfotable = ` {
+func Dbtablename(tableid string ) (tablename string, err error) {
+ var dbinfotable =` {
"query": {
"bool": {
"filter": [
{
"term": {
- "id":"` + tableid + `"
+ "id":"`+tableid+`"
}
}
]
@@ -127,20 +130,20 @@
]
}
`
- buf, err := EsReq("POST", "http://192.168.1.182:9200/dbtables/_search", []byte(dbinfotable))
- if err != nil {
- return "", errors.New("http request dbtablename info is err!")
- }
- sources, err := Sourcelist(buf)
- if err != nil {
- return "", err
- }
+ buf, err := EsReq("POST", "http://192.168.1.182:9200/dbtables/_search",[]byte(dbinfotable))
+ if err != nil {
+ return "" ,errors.New("http request dbtablename info is err!")
+ }
+ sources, err := Sourcelist(buf)
+ if err != nil {
+ return "",err
+ }
- for _, source := range sources {
- if name, ok := source["tableName"].(string); ok {
- tablename = name
- break
- }
- }
- return tablename, nil
+ for _, source := range sources {
+ if name, ok := source["tableName"].(string); ok {
+ tablename = name
+ break
+ }
+ }
+ return tablename, nil
}
diff --git a/esutil/EsClient.go b/esutil/EsClient.go
index 636e7ca..9972c13 100644
--- a/esutil/EsClient.go
+++ b/esutil/EsClient.go
@@ -1,233 +1,236 @@
package esutil
import (
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "io/ioutil"
- "net/http"
- "time"
+ "bytes"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "time"
- "basic.com/pubsub/protomsg.git"
+ "basic.com/pubsub/protomsg.git"
)
+
func Parsesources(sources []map[string]interface{}) (esinfos []*protomsg.Esinfo) {
- var ok bool
- for _, source := range sources {
- var tmpinfo protomsg.Esinfo
- tmpinfo.FaceFeature, ok = source["faceFeature"].(string)
- if !ok {
- continue
- }
+ var ok bool
+ for _, source := range sources {
+ var tmpinfo protomsg.Esinfo
+ tmpinfo.FaceFeature, ok = source["faceFeature"].(string)
+ if !ok {
+ continue
+ }
- tmpinfo.Id, ok = source["id"].(string)
- if !ok {
- continue
- }
- tmpinfo.Tableid, ok = source["tableId"].(string)
- if !ok {
- continue
- }
+ tmpinfo.Id, ok = source["id"].(string)
+ if !ok {
+ continue
+ }
+ tmpinfo.Tableid, ok = source["tableId"].(string)
+ if !ok {
+ continue
+ }
- esinfos = append(esinfos, &tmpinfo)
+ esinfos = append(esinfos, &tmpinfo)
- // tmpinfo.PersonName,ok = source["personName"].(string)
- // Isnil("personName", ok)
- // tmpinfo.PersonPicUrl,ok = source["personPicUrl"].(string)
- // Isnil("personPicUrl", ok)
- // tmpinfo.PhoneNum,ok = source["phoneNum"].(string)
- // Isnil("phoneNum", ok)
- // tmpinfo.Sex,ok = source["sex"].(string)
- // Isnil("sex", ok)
- // tmpinfo.Idcard,ok = source["idCard"].(string)
- // Isnil("idCard", ok)
- // tmpinfo.MonitorLevel,ok = source["monitorLevel"].(string)
+// tmpinfo.PersonName,ok = source["personName"].(string)
+// Isnil("personName", ok)
+// tmpinfo.PersonPicUrl,ok = source["personPicUrl"].(string)
+// Isnil("personPicUrl", ok)
+// tmpinfo.PhoneNum,ok = source["phoneNum"].(string)
+// Isnil("phoneNum", ok)
+// tmpinfo.Sex,ok = source["sex"].(string)
+// Isnil("sex", ok)
+// tmpinfo.Idcard,ok = source["idCard"].(string)
+// Isnil("idCard", ok)
+// tmpinfo.MonitorLevel,ok = source["monitorLevel"].(string)
- // 鏍规嵁 tableid 鑾峰彇 tablename
- // name, _:= Dbtablename(tmpinfo.TableId)
- // tmpinfo.TableName= name
- // binfos.Infos = append(binfos.Infos, &tmpinfo)
+ // 鏍规嵁 tableid 鑾峰彇 tablename
+// name, _:= Dbtablename(tmpinfo.TableId)
+// tmpinfo.TableName= name
+// binfos.Infos = append(binfos.Infos, &tmpinfo)
- }
- return
+ }
+ return
}
-
//瑙f瀽搴曞簱浜哄憳缁撴瀯
-func Dbpersonbyid(sources []map[string]interface{}) protomsg.Dbperson {
- var ok bool
+func Dbpersonbyid(sources []map[string]interface{}) (tmpinfos []protomsg.Dbperson) {
+ var ok bool
- var tmpinfo protomsg.Dbperson
- for _, source := range sources {
- tmpinfo.FaceFeature, ok = source["faceFeature"].(string)
- if !ok {
- continue
- }
+ var tmpinfo protomsg.Dbperson
+ for _, source := range sources{
+ tmpinfo.FaceFeature, ok = source["faceFeature"].(string)
+ if !ok {
+ continue
+ }
- tmpinfo.Id, ok = source["id"].(string)
- if !ok {
- continue
- }
+ tmpinfo.Id, ok = source["id"].(string)
+ if !ok {
+ continue
+ }
- tmpinfo.TableId, ok = source["tableId"].(string)
- if !ok {
- continue
- }
+ tmpinfo.TableId, ok = source["tableId"].(string)
+ if !ok {
+ continue
+ }
- tmpinfo.PersonName, ok = source["personName"].(string)
- Isnil("personName", ok)
- tmpinfo.PersonPicUrl, ok = source["personPicUrl"].(string)
- Isnil("personPicUrl", ok)
- tmpinfo.PhoneNum, ok = source["phoneNum"].(string)
- Isnil("phoneNum", ok)
- tmpinfo.Sex, ok = source["sex"].(string)
- Isnil("sex", ok)
- tmpinfo.IdCard, ok = source["idCard"].(string)
- Isnil("idCard", ok)
- tmpinfo.MonitorLevel, ok = source["monitorLevel"].(string)
- Isnil("monitorLevel", ok)
- tmpinfo.UpdateTime, ok = source["updateTime"].(string)
- Isnil("updateTime", ok)
- tmpinfo.Age, ok = source["age"].(string)
- Isnil("age", ok)
+ tmpinfo.PersonName,ok = source["personName"].(string)
+ Isnil("personName", ok)
+ tmpinfo.PersonPicUrl,ok = source["personPicUrl"].(string)
+ Isnil("personPicUrl", ok)
+ tmpinfo.PhoneNum,ok = source["phoneNum"].(string)
+ Isnil("phoneNum", ok)
+ tmpinfo.Sex,ok = source["sex"].(string)
+ Isnil("sex", ok)
+ tmpinfo.IdCard,ok = source["idCard"].(string)
+ Isnil("idCard", ok)
+ tmpinfo.MonitorLevel,ok = source["monitorLevel"].(string)
+ Isnil("monitorLevel",ok)
+ tmpinfo.UpdateTime,ok = source["updateTime"].(string)
+ Isnil("updateTime",ok)
+ tmpinfo.Age,ok = source["age"].(string)
+ Isnil("age", ok)
- isDelete, ok := source["isDelete"].(float64)
- if ok {
- tmpinfo.IsDelete = int32(isDelete)
- }
- Isnil("isDelete", ok)
+ isDelete,ok := source["isDelete"].(float64)
+ if ok {
+ tmpinfo.IsDelete= int32(isDelete)
+ }
+ Isnil("isDelete", ok)
- enable, ok := source["enable"].(float64)
- if ok {
- tmpinfo.Enable = int32(enable)
- }
- Isnil("endTime", ok)
+ enable,ok := source["enable"].(float64)
+ if ok {
+ tmpinfo.Enable= int32(enable)
+ }
+ Isnil("endTime", ok)
- tmpinfo.Reserved, ok = source["reserved"].(string)
- Isnil("reserved", ok)
- tmpinfo.PicDesc, ok = source["picDesc"].(string)
- Isnil("picDesc", ok)
- tmpinfo.CreateTime, ok = source["createTime"].(string)
- Isnil("createTime", ok)
- tmpinfo.CreateBy, ok = source["createBy"].(string)
- Isnil("createBy", ok)
+ tmpinfo.Reserved,ok = source["reserved"].(string)
+ Isnil("reserved",ok)
+ tmpinfo.PicDesc,ok = source["picDesc"].(string)
+ Isnil("picDesc",ok)
+ tmpinfo.CreateTime,ok = source["createTime"].(string)
+ Isnil("createTime",ok)
+ tmpinfo.CreateBy,ok = source["createBy"].(string)
+ Isnil("createBy",ok)
- //鏍规嵁 tableid 鑾峰彇 tablename
- //name, _:= Dbtablename(tmpinfo.TableId)
- //tmpinfo.TableName= name
- }
+ //鏍规嵁 tableid 鑾峰彇 tablename
+ //name, _:= Dbtablename(tmpinfo.TableId)
+ //tmpinfo.TableName= name
+ tmpinfos = append(tmpinfos, tmpinfo)
+ }
- return tmpinfo
+ return tmpinfos
}
//瑙f瀽搴曞簱缁撴瀯
-func Dbtablebyid(sources []map[string]interface{}) protomsg.Dbtable {
- var ok bool
+func Dbtablebyid(sources []map[string]interface{}) (tmpinfos []protomsg.Dbtable) {
+ var ok bool
- var tmpinfo protomsg.Dbtable
- for _, source := range sources {
+ var tmpinfo protomsg.Dbtable
+ for _, source := range sources{
- tmpinfo.Id, ok = source["id"].(string)
- if !ok {
- continue
- }
- tmpinfo.TableName, ok = source["tableName"].(string)
- Isnil("tableName", ok)
- tmpinfo.TableType, ok = source["tableType"].(string)
- Isnil("tableType", ok)
- tmpinfo.CreateBy, ok = source["createBy"].(string)
- Isnil("createBy", ok)
- tmpinfo.TableDesc, ok = source["tableDesc"].(string)
- Isnil("tableDesc", ok)
- tmpinfo.BwType, ok = source["bwType"].(string)
- Isnil("bwType", ok)
- tmpinfo.StartTime, ok = source["startTime"].(string)
- Isnil("startTime", ok)
- tmpinfo.EndTime, ok = source["endTime"].(string)
- Isnil("endTime", ok)
+ tmpinfo.Id, ok = source["id"].(string)
+ if !ok {
+ continue
+ }
+ tmpinfo.TableName,ok = source["tableName"].(string)
+ Isnil("tableName", ok)
+ tmpinfo.TableType,ok = source["tableType"].(string)
+ Isnil("tableType", ok)
+ tmpinfo.CreateBy,ok = source["createBy"].(string)
+ Isnil("createBy", ok)
+ tmpinfo.TableDesc,ok = source["tableDesc"].(string)
+ Isnil("tableDesc", ok)
+ tmpinfo.BwType,ok = source["bwType"].(string)
+ Isnil("bwType", ok)
+ tmpinfo.StartTime,ok = source["startTime"].(string)
+ Isnil("startTime", ok)
+ tmpinfo.EndTime,ok = source["endTime"].(string)
+ Isnil("endTime", ok)
- isDelete, ok := source["isDelete"].(float64)
- if ok {
- tmpinfo.IsDelete = int32(isDelete)
- }
- Isnil("isDelete", ok)
+ isDelete,ok := source["isDelete"].(float64)
+ if ok {
+ tmpinfo.IsDelete= int32(isDelete)
+ }
+ Isnil("isDelete", ok)
- enable, ok := source["enable"].(float64)
- if ok {
- tmpinfo.Enable = int32(enable)
- }
- Isnil("endTime", ok)
+ enable,ok := source["enable"].(float64)
+ if ok {
+ tmpinfo.Enable= int32(enable)
+ }
+ Isnil("endTime", ok)
- tmpinfo.CreateTime, ok = source["createTime"].(string)
- Isnil("createTime", ok)
- tmpinfo.UpdateTime, ok = source["updateTime"].(string)
- Isnil("updateTime", ok)
- tmpinfo.IsForever, ok = source["isForever"].(string)
- Isnil("isForever", ok)
- tmpinfo.IsSync, ok = source["isSync"].(string)
- Isnil("isSync", ok)
- tmpinfo.AnalyServerId, ok = source["analyServerId"].(string)
- Isnil("analyServerId", ok)
- }
+ tmpinfo.CreateTime,ok = source["createTime"].(string)
+ Isnil("createTime", ok)
+ tmpinfo.UpdateTime,ok = source["updateTime"].(string)
+ Isnil("updateTime", ok)
+ tmpinfo.IsForever,ok = source["isForever"].(string)
+ Isnil("isForever", ok)
+ tmpinfo.IsSync,ok = source["isSync"].(string)
+ Isnil("isSync", ok)
+ tmpinfo.AnalyServerId,ok = source["analyServerId"].(string)
+ Isnil("analyServerId", ok)
- return tmpinfo
+ tmpinfos = append(tmpinfos, tmpinfo)
+ }
+
+ return tmpinfos
}
-func Sourcelist(buf []byte) (sources []map[string]interface{}, err error) {
- var info interface{}
- json.Unmarshal(buf, &info)
- out, ok := info.(map[string]interface{})
- if !ok {
- return nil, errors.New("http response interface can not change map[string]interface{}")
- }
+func Sourcelist(buf []byte)(sources []map[string]interface{}, err error){
+ var info interface{}
+ json.Unmarshal(buf, &info)
+ out, ok := info.(map[string]interface{})
+ if !ok {
+ return nil, errors.New("http response interface can not change map[string]interface{}")
+ }
- middle, ok := out["hits"].(map[string]interface{})
- if !ok {
- return nil, errors.New("first hits change error!")
- }
- for _, in := range middle["hits"].([]interface{}) {
- tmpbuf, ok := in.(map[string]interface{})
- if !ok {
- fmt.Println("change to source error!")
- continue
- }
- source, ok := tmpbuf["_source"].(map[string]interface{})
- if !ok {
- fmt.Println("change _source error!")
- continue
- }
- sources = append(sources, source)
- }
- return sources, nil
+ middle, ok := out["hits"].(map[string]interface{})
+ if !ok {
+ return nil, errors.New("first hits change error!")
+ }
+ for _, in := range middle["hits"].([]interface{}){
+ tmpbuf, ok := in.(map[string]interface{})
+ if !ok {
+ fmt.Println("change to source error!")
+ continue
+ }
+ source, ok := tmpbuf["_source"].(map[string]interface{})
+ if !ok {
+ fmt.Println("change _source error!")
+ continue
+ }
+ sources = append(sources, source )
+ }
+ return sources,nil
}
func EsReq(method string, url string, parama []byte) (buf []byte, err error) {
- defer elapsed("page")()
- timeout := time.Duration(10 * time.Second)
- client := http.Client{
- Timeout: timeout,
- }
- request, err := http.NewRequest(method, url, bytes.NewBuffer(parama))
- request.Header.Set("Content-type", "application/json")
+ defer elapsed("page")()
+ timeout := time.Duration(10 * time.Second)
+ client := http.Client{
+ Timeout: timeout,
+ }
+ request, err := http.NewRequest(method, url, bytes.NewBuffer(parama))
+ request.Header.Set("Content-type", "application/json")
- if err != nil {
- fmt.Println("build request fail !")
- return nil, err
- }
+ if err != nil {
+ fmt.Println("build request fail !")
+ return nil, err
+ }
- resp, err := client.Do(request)
- if err != nil {
- fmt.Println("request error: ", err)
- return nil, err
- }
+ resp, err := client.Do(request)
+ if err != nil{
+ fmt.Println("request error: ", err)
+ return nil, err
+ }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println(err)
- return nil, err
- }
- return body, nil
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ fmt.Println(err)
+ return nil , err
+ }
+ return body, nil
}
// 璁$畻鏃堕棿
@@ -239,8 +242,8 @@
}
// 璧嬪�兼椂妫�娴嬫槸鍚﹁兘澶熻祴鍊�
-func Isnil(key string, ok bool) {
- if !ok {
- fmt.Println(key, "is nil can not asign")
- }
+func Isnil(key string, ok bool){
+ if !ok {
+ fmt.Println(key, "is nil can not asign")
+ }
}
--
Gitblit v1.8.0