|
| 1 | +package es |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/olivere/elastic/v7" |
| 8 | + "os" |
| 9 | + "reflect" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +var client *elastic.Client |
| 15 | + |
| 16 | +func TestMain(m *testing.M) { |
| 17 | + var err error |
| 18 | + client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL("http://172.13.3.160:9200/")) |
| 19 | + if err != nil { |
| 20 | + panic(err) |
| 21 | + } |
| 22 | + info, code, err := client.Ping("http://172.13.3.160:9200/").Do(context.Background()) |
| 23 | + if err != nil { |
| 24 | + panic(err) |
| 25 | + } |
| 26 | + fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number) |
| 27 | + esversion, err := client.ElasticsearchVersion("http://172.13.3.160:9200/") |
| 28 | + if err != nil { |
| 29 | + panic(err) |
| 30 | + } |
| 31 | + fmt.Printf("Elasticsearch version %s\n", esversion) |
| 32 | + os.Exit(m.Run()) |
| 33 | +} |
| 34 | + |
| 35 | +type User struct { |
| 36 | + FirstName string `json:"first_name"` |
| 37 | + LastName string `json:"last_name"` |
| 38 | + Age int `json:"age"` |
| 39 | +} |
| 40 | + |
| 41 | +var indexName = "hwholiday" |
| 42 | + |
| 43 | +func getId() string { |
| 44 | + return fmt.Sprintf("%d", time.Now().UnixNano()/1e6) |
| 45 | +} |
| 46 | + |
| 47 | +func TestCreate(t *testing.T) { |
| 48 | + res, err := client.Index().Index(indexName).Id("1").BodyJson(User{ |
| 49 | + FirstName: "a", |
| 50 | + LastName: "b", |
| 51 | + Age: 10, |
| 52 | + }).Do(context.Background()) |
| 53 | + if err != nil { |
| 54 | + panic(err) |
| 55 | + } |
| 56 | + fmt.Println(res) |
| 57 | + |
| 58 | + res, err = client.Index().Index(indexName).Id("2").BodyJson(User{ |
| 59 | + FirstName: "a", |
| 60 | + LastName: "c", |
| 61 | + Age: 20, |
| 62 | + }).Do(context.Background()) |
| 63 | + if err != nil { |
| 64 | + panic(err) |
| 65 | + } |
| 66 | + fmt.Println(res) |
| 67 | + |
| 68 | + res, err = client.Index().Index(indexName).Id("3").BodyJson(User{ |
| 69 | + FirstName: "abcd e", |
| 70 | + LastName: "f", |
| 71 | + Age: 30, |
| 72 | + }).Do(context.Background()) |
| 73 | + if err != nil { |
| 74 | + panic(err) |
| 75 | + } |
| 76 | + fmt.Println(res) |
| 77 | +} |
| 78 | + |
| 79 | +func TestUpdate(t *testing.T) { |
| 80 | + res, err := client.Update().Index(indexName).Id("1").Doc(map[string]interface{}{ |
| 81 | + "age": 50, |
| 82 | + "last_name": "c", |
| 83 | + }).Do(context.Background()) |
| 84 | + if err != nil { |
| 85 | + panic(err) |
| 86 | + } |
| 87 | + fmt.Println(res) |
| 88 | +} |
| 89 | + |
| 90 | +func TestGet(t *testing.T) { |
| 91 | + res, err := client.Get().Index(indexName).Id("1").Do(context.Background()) |
| 92 | + if err != nil { |
| 93 | + panic(err) |
| 94 | + } |
| 95 | + var data User |
| 96 | + err = json.Unmarshal(res.Source, &data) |
| 97 | + if err != nil { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + fmt.Println(data) |
| 101 | +} |
| 102 | + |
| 103 | +func TestDel(t *testing.T) { |
| 104 | + res, err := client.Delete().Index(indexName).Id("1").Do(context.Background()) |
| 105 | + if err != nil { |
| 106 | + panic(err) |
| 107 | + } |
| 108 | + fmt.Println(res) |
| 109 | +} |
| 110 | + |
| 111 | +func TestInsertArray(t *testing.T) { |
| 112 | + res, err := client.Search(indexName).Do(context.Background()) |
| 113 | + if err != nil { |
| 114 | + panic(err) |
| 115 | + } |
| 116 | + for _, v := range res.Each(reflect.TypeOf(User{})) { |
| 117 | + t := v.(User) |
| 118 | + fmt.Println(t) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestQuery(t *testing.T) { |
| 123 | + //字段相等 |
| 124 | + q := elastic.NewQueryStringQuery("last_name:c") |
| 125 | + printQuery(q) |
| 126 | + //条件查询 |
| 127 | + bQ := elastic.NewBoolQuery() |
| 128 | + bQ.Must(elastic.NewMatchQuery("last_name", "c")) |
| 129 | + bQ.Filter(elastic.NewRangeQuery("age").Gt(10)) |
| 130 | + printQuery(bQ) |
| 131 | + |
| 132 | + //短语搜索 搜索 first_name字段中有 a |
| 133 | + mQ := elastic.NewMatchPhraseQuery("first_name", "e") |
| 134 | + printQuery(mQ) |
| 135 | +} |
| 136 | + |
| 137 | +func TestList(t *testing.T) { |
| 138 | + var ( |
| 139 | + size = 10 |
| 140 | + page = 1 |
| 141 | + ) |
| 142 | + if size < 0 || page < 1 { |
| 143 | + return |
| 144 | + } |
| 145 | + res, err := client.Search(indexName). |
| 146 | + Size(size). |
| 147 | + From((page - 1) * size). |
| 148 | + Do(context.Background()) |
| 149 | + if err != nil { |
| 150 | + panic(err) |
| 151 | + } |
| 152 | + for _, v := range res.Each(reflect.TypeOf(User{})) { |
| 153 | + t := v.(User) |
| 154 | + fmt.Println(t) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func printQuery(q elastic.Query) { |
| 159 | + res, err := client.Search(indexName).Query(q).Do(context.Background()) |
| 160 | + if err != nil { |
| 161 | + panic(err) |
| 162 | + } |
| 163 | + for _, v := range res.Each(reflect.TypeOf(User{})) { |
| 164 | + t := v.(User) |
| 165 | + fmt.Println(t) |
| 166 | + } |
| 167 | +} |
0 commit comments