feat:优化信息
This commit is contained in:
@ -1,13 +1,50 @@
|
||||
package es
|
||||
|
||||
func Create() error {
|
||||
//_, err := client.Index().
|
||||
// Index("students").
|
||||
// BodyJson(string(dataJSON)).
|
||||
// Do(context.Background())
|
||||
return nil
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/olivere/elastic/v7"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Create 创建操作
|
||||
func Create(index string, body []byte) error {
|
||||
_, err := client.Index().
|
||||
Index(index).
|
||||
BodyJson(string(body)).
|
||||
Do(context.Background())
|
||||
return err
|
||||
}
|
||||
|
||||
func Search() {
|
||||
//resp, err := client.Search()
|
||||
// Search 搜索操作
|
||||
func Search(src interface{}, index string, params map[string]interface{}, page, pageSize int) ([]interface{}, error) {
|
||||
query := elastic.NewBoolQuery()
|
||||
|
||||
for k, v := range params {
|
||||
query.Should(elastic.NewMatchQuery(k, v))
|
||||
}
|
||||
service := client.Search().Index(index).Pretty(true).Query(query)
|
||||
|
||||
if page > 0 && pageSize > 0 {
|
||||
// 游标分页
|
||||
service.From((page - 1) * pageSize)
|
||||
service.Size(pageSize)
|
||||
}
|
||||
result, err := service.Do(context.Background())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]interface{}, 0)
|
||||
|
||||
for _, hit := range result.Hits.Hits {
|
||||
data := new(interface{})
|
||||
reflect.DeepEqual(data, src)
|
||||
|
||||
if err = json.Unmarshal(hit.Source, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, data)
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user