feat:完成首页搜索功能
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
package es
|
||||
|
||||
import (
|
||||
"SciencesServer/utils"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/olivere/elastic/v7"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -16,9 +16,9 @@ var (
|
||||
)
|
||||
|
||||
type Student struct {
|
||||
Name string `json:"name"`
|
||||
Age int64 `json:"age"`
|
||||
AverageScore float64 `json:"average_score"`
|
||||
ID uint64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Age int64 `json:"age"`
|
||||
}
|
||||
|
||||
func _new() *elastic.Client {
|
||||
@ -33,65 +33,49 @@ func _new() *elastic.Client {
|
||||
return client
|
||||
}
|
||||
|
||||
func TestNewInstance(t *testing.T) {
|
||||
func TestNewInstanceCreate(t *testing.T) {
|
||||
client := _new()
|
||||
//client.Delete()
|
||||
|
||||
//newStudent := Student{
|
||||
// Name: "熊,桥,顺",
|
||||
// Age: 10,
|
||||
// AverageScore: 99.9,
|
||||
//}
|
||||
//dataJSON, _ := json.Marshal(newStudent)
|
||||
//
|
||||
//_, err = client.Index().
|
||||
// Index("students").
|
||||
// BodyJson(string(dataJSON)).
|
||||
// Do(context.Background())
|
||||
//
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
var students []Student
|
||||
|
||||
searchSource := elastic.NewSearchSource()
|
||||
searchSource.Query(elastic.NewMatchQuery("name", "顺"))
|
||||
|
||||
queryStr, _ := searchSource.Source()
|
||||
queryJs, _ := json.Marshal(queryStr)
|
||||
|
||||
fmt.Println("[esclient]Final ESQuery=\n", string(queryJs))
|
||||
|
||||
searchService := client.Search().Index("students").SearchSource(searchSource)
|
||||
|
||||
searchResult, err1 := searchService.Do(context.Background())
|
||||
|
||||
if err1 != nil {
|
||||
fmt.Println("[ProductsES][GetPIds]Error=", err1)
|
||||
return
|
||||
}
|
||||
for _, hit := range searchResult.Hits.Hits {
|
||||
var student Student
|
||||
err := json.Unmarshal(hit.Source, &student)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("[Getting Students][Unmarshal] Err=", err)
|
||||
}
|
||||
students = append(students, student)
|
||||
}
|
||||
t.Log(students)
|
||||
src := Student{ID: 2, Name: "henry", Age: 28}
|
||||
client.Index().Index("student").Id(fmt.Sprintf("%d", src.ID)).BodyJson(utils.AnyToJSON(src)).
|
||||
Do(context.Background())
|
||||
}
|
||||
|
||||
func TestNewInstance1(t *testing.T) {
|
||||
NewInstance(
|
||||
WithEsAddress([]string{"http://192.168.0.188:9200"}),
|
||||
).Init().Local()
|
||||
var b strings.Builder
|
||||
b.WriteString(`{"title" : "`)
|
||||
b.WriteString("我是好人")
|
||||
b.WriteString(`"}`)
|
||||
//resp, err := client.Create(dataIndex, "2", strings.NewReader(b.String()))
|
||||
//t.Log(err)
|
||||
//t.Log(resp)
|
||||
func TestNewInstanceUpdate(t *testing.T) {
|
||||
client := _new()
|
||||
//client.Delete()
|
||||
|
||||
src := &Student{ID: 1, Name: "Henry", Age: 28}
|
||||
_, err := client.Update().Index("student").Id(fmt.Sprintf("%d", src.ID)).Doc(map[string]interface{}{
|
||||
"age": 29,
|
||||
}).Do(context.Background())
|
||||
t.Log(err)
|
||||
}
|
||||
|
||||
func TestNewInstanceSearch(t *testing.T) {
|
||||
client := _new()
|
||||
//client.Delete()
|
||||
|
||||
query := elastic.NewBoolQuery()
|
||||
//query.Must(elastic.NewMatchQuery("age", "27"))
|
||||
|
||||
service := client.Search().Index("student")
|
||||
|
||||
result, err := service.Pretty(true).Query(query).Do(context.Background())
|
||||
|
||||
t.Log(err)
|
||||
t.Log(utils.AnyToJSON(result))
|
||||
|
||||
src := new(Student)
|
||||
|
||||
for _, hit := range result.Hits.Hits {
|
||||
if err = json.Unmarshal(hit.Source, src); err != nil {
|
||||
t.Log(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(utils.AnyToJSON(src))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInstance2(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user