2021-12-09 16:17:23 +08:00
|
|
|
|
package es
|
|
|
|
|
|
|
|
|
|
import (
|
2022-01-18 16:29:29 +08:00
|
|
|
|
"SciencesServer/utils"
|
2021-12-20 17:07:39 +08:00
|
|
|
|
"context"
|
2021-12-22 14:11:14 +08:00
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2021-12-20 17:07:39 +08:00
|
|
|
|
"github.com/olivere/elastic/v7"
|
2021-12-09 16:17:23 +08:00
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
dataIndex = "test"
|
2021-12-20 17:07:39 +08:00
|
|
|
|
//client *elastic.Client
|
|
|
|
|
host = "http://192.168.0.188:9200"
|
2021-12-09 16:17:23 +08:00
|
|
|
|
)
|
|
|
|
|
|
2021-12-22 14:11:14 +08:00
|
|
|
|
type Student struct {
|
2022-01-18 16:29:29 +08:00
|
|
|
|
ID uint64 `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Age int64 `json:"age"`
|
2021-12-22 14:11:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 09:13:16 +08:00
|
|
|
|
func _new() *elastic.Client {
|
2021-12-20 17:07:39 +08:00
|
|
|
|
client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host))
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
if _, _, err = client.Ping(host).Do(context.Background()); err != nil {
|
2021-12-24 09:13:16 +08:00
|
|
|
|
panic(err)
|
2021-12-20 17:07:39 +08:00
|
|
|
|
}
|
2021-12-24 09:13:16 +08:00
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 11:05:47 +08:00
|
|
|
|
type ESAchievement struct {
|
|
|
|
|
ID uint64 `json:"id"` // 成果ID
|
|
|
|
|
Title string `json:"title"` // 成果名称
|
|
|
|
|
Industry string `json:"industry"` // 行业领域
|
|
|
|
|
Keyword string `json:"keyword"` // 关键词
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 16:29:29 +08:00
|
|
|
|
func TestNewInstanceCreate(t *testing.T) {
|
2021-12-24 09:13:16 +08:00
|
|
|
|
client := _new()
|
2022-01-18 16:29:29 +08:00
|
|
|
|
//client.Delete()
|
2021-12-22 14:11:14 +08:00
|
|
|
|
|
2022-02-08 11:05:47 +08:00
|
|
|
|
//src := Student{ID: 2, Name: "henry", Age: 28}
|
|
|
|
|
src := &ESAchievement{
|
|
|
|
|
ID: 6,
|
|
|
|
|
Title: "测试6",
|
|
|
|
|
Industry: "测试测试",
|
|
|
|
|
Keyword: "关键词1;关键词2",
|
|
|
|
|
}
|
|
|
|
|
_, err := client.Index().Index("es_achievement_index").Id(fmt.Sprintf("%d", src.ID)).BodyJson(utils.AnyToJSON(src)).
|
2022-01-18 16:29:29 +08:00
|
|
|
|
Do(context.Background())
|
2022-02-08 11:05:47 +08:00
|
|
|
|
t.Log(err)
|
2022-01-18 16:29:29 +08:00
|
|
|
|
}
|
2021-12-22 14:11:14 +08:00
|
|
|
|
|
2022-01-18 16:29:29 +08:00
|
|
|
|
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)
|
2021-12-20 17:07:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 16:29:29 +08:00
|
|
|
|
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))
|
|
|
|
|
}
|
2021-12-09 16:17:23 +08:00
|
|
|
|
}
|
2021-12-20 17:07:39 +08:00
|
|
|
|
|
|
|
|
|
func TestNewInstance2(t *testing.T) {
|
2021-12-24 09:13:16 +08:00
|
|
|
|
client := _new()
|
2021-12-23 10:33:31 +08:00
|
|
|
|
|
2021-12-24 09:13:16 +08:00
|
|
|
|
query := elastic.NewBoolQuery()
|
2022-01-25 11:45:53 +08:00
|
|
|
|
//query.Must(elastic.NewMatchQuery("title", "一种"))
|
2022-01-20 15:49:44 +08:00
|
|
|
|
//query.Must(elastic.NewMatchPhraseQuery("industry", "你是"))
|
2021-12-23 10:33:31 +08:00
|
|
|
|
|
2022-01-20 15:49:44 +08:00
|
|
|
|
service := client.Search().
|
|
|
|
|
//Index("es_patent_index")
|
2022-01-25 11:45:53 +08:00
|
|
|
|
Index("es_manage_index_2")
|
2021-12-23 10:33:31 +08:00
|
|
|
|
|
2022-01-20 15:49:44 +08:00
|
|
|
|
result, err := service.From(0).Size(10).
|
|
|
|
|
//Explain(true).
|
|
|
|
|
AllowPartialSearchResults(true).
|
|
|
|
|
TrackTotalHits(true).
|
|
|
|
|
TrackScores(true).Query(query).Do(context.Background())
|
2021-12-23 10:33:31 +08:00
|
|
|
|
|
2021-12-24 09:13:16 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
2021-12-23 10:33:31 +08:00
|
|
|
|
}
|
2022-01-20 15:49:44 +08:00
|
|
|
|
t.Log(utils.AnyToJSON(result))
|
2022-01-20 11:51:02 +08:00
|
|
|
|
t.Log(utils.AnyToJSON(result.Hits.TotalHits.Value))
|
2021-12-24 09:13:16 +08:00
|
|
|
|
for _, v := range result.Hits.Hits {
|
2022-01-20 15:49:44 +08:00
|
|
|
|
t.Log(*v.Score)
|
2021-12-24 09:13:16 +08:00
|
|
|
|
t.Log(string(v.Source))
|
2021-12-23 10:33:31 +08:00
|
|
|
|
}
|
2021-12-20 17:07:39 +08:00
|
|
|
|
}
|