feat:优化信息,完善es查询,完善网站首页公司信息查询

This commit is contained in:
henry
2021-12-23 17:43:27 +08:00
parent 741138b3ea
commit eb2cfcb06b
18 changed files with 441 additions and 93 deletions

View File

@ -108,7 +108,6 @@ func TestNewInstance2(t *testing.T) {
function := func(src interface{}) {
obj := reflect.ValueOf(src).Interface()
t.Log(obj)
var a interface{} = &Manage{ID: 123123}
out := make([]interface{}, 0)

View File

@ -7,39 +7,57 @@ import (
"reflect"
)
type (
// SearchParams 搜索参数
SearchParams struct {
MustParams map[string]interface{}
ShouldParams map[string]interface{}
}
)
// Create 创建操作
func Create(index string, body []byte) error {
_, err := client.Index().
Index(index).
//Id(fmt.Sprintf("%d", 1)).
BodyJson(string(body)).
Do(context.Background())
return err
}
// Search 搜索操作
func Search(src interface{}, index string, params map[string]interface{}, page, pageSize int) ([]interface{}, error) {
func Search(src interface{}, index string, params *SearchParams, page, pageSize int) ([]interface{}, error) {
query := elastic.NewBoolQuery()
for k, v := range params {
query.Should(elastic.NewMatchQuery(k, v))
if params != nil {
if params.MustParams != nil && len(params.MustParams) > 0 {
for k, v := range params.MustParams {
query.Must(elastic.NewMatchQuery(k, v))
}
}
if params.ShouldParams != nil && len(params.ShouldParams) > 0 {
for k, v := range params.ShouldParams {
query.Should(elastic.NewMatchQuery(k, v))
}
}
}
service := client.Search().Index(index).Pretty(true).Query(query)
service := client.Search().Index(index)
if page > 0 && pageSize > 0 {
// 游标分页
service.From((page - 1) * pageSize)
service.Size(pageSize)
service = service.From((page - 1) * pageSize).Size(pageSize)
}
result, err := service.Do(context.Background())
result, err := service.Pretty(true).Query(query).Do(context.Background())
if err != nil {
return nil, err
}
out := make([]interface{}, 0)
_type := reflect.TypeOf(src)
for _, hit := range result.Hits.Hits {
data := new(interface{})
reflect.DeepEqual(data, src)
data := reflect.New(_type).Interface()
if err = json.Unmarshal(hit.Source, data); err != nil {
return nil, err
@ -48,3 +66,9 @@ func Search(src interface{}, index string, params map[string]interface{}, page,
}
return out, err
}
// Delete 删除操作
func Delete(index string) error {
_, err := client.DeleteIndex(index).Do(context.Background())
return err
}