Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

example_test.go 3.1 KiB

11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
11 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2013 Belogik. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package goes_test
  5. import (
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "time"
  11. "github.com/belogik/goes"
  12. )
  13. var (
  14. ES_HOST = "localhost"
  15. ES_PORT = "9200"
  16. )
  17. func getConnection() (conn *goes.Connection) {
  18. h := os.Getenv("TEST_ELASTICSEARCH_HOST")
  19. if h == "" {
  20. h = ES_HOST
  21. }
  22. p := os.Getenv("TEST_ELASTICSEARCH_PORT")
  23. if p == "" {
  24. p = ES_PORT
  25. }
  26. conn = goes.NewConnection(h, p)
  27. return
  28. }
  29. func ExampleConnection_CreateIndex() {
  30. conn := getConnection()
  31. mapping := map[string]interface{}{
  32. "settings": map[string]interface{}{
  33. "index.number_of_shards": 1,
  34. "index.number_of_replicas": 0,
  35. },
  36. "mappings": map[string]interface{}{
  37. "_default_": map[string]interface{}{
  38. "_source": map[string]interface{}{
  39. "enabled": true,
  40. },
  41. "_all": map[string]interface{}{
  42. "enabled": false,
  43. },
  44. },
  45. },
  46. }
  47. resp, err := conn.CreateIndex("test", mapping)
  48. if err != nil {
  49. panic(err)
  50. }
  51. fmt.Printf("%s", resp)
  52. }
  53. func ExampleConnection_DeleteIndex() {
  54. conn := getConnection()
  55. resp, err := conn.DeleteIndex("yourinde")
  56. if err != nil {
  57. panic(err)
  58. }
  59. fmt.Printf("%s", resp)
  60. }
  61. func ExampleConnection_RefreshIndex() {
  62. conn := getConnection()
  63. resp, err := conn.RefreshIndex("yourindex")
  64. if err != nil {
  65. panic(err)
  66. }
  67. fmt.Printf("%s", resp)
  68. }
  69. func ExampleConnection_Search() {
  70. conn := getConnection()
  71. var query = map[string]interface{}{
  72. "query": map[string]interface{}{
  73. "bool": map[string]interface{}{
  74. "must": map[string]interface{}{
  75. "match_all": map[string]interface{}{},
  76. },
  77. },
  78. },
  79. "from": 0,
  80. "size": 100,
  81. "fields": []string{"onefield"},
  82. "filter": map[string]interface{}{
  83. "range": map[string]interface{}{
  84. "somefield": map[string]interface{}{
  85. "from": "some date",
  86. "to": "some date",
  87. "include_lower": false,
  88. "include_upper": false,
  89. },
  90. },
  91. },
  92. }
  93. extraArgs := make(url.Values, 1)
  94. searchResults, err := conn.Search(query, []string{"someindex"}, []string{""}, extraArgs)
  95. if err != nil {
  96. panic(err)
  97. }
  98. fmt.Printf("%s", searchResults)
  99. }
  100. func ExampleConnection_Index() {
  101. conn := getConnection()
  102. d := goes.Document{
  103. Index: "twitter",
  104. Type: "tweet",
  105. Fields: map[string]interface{}{
  106. "user": "foo",
  107. "message": "bar",
  108. },
  109. }
  110. extraArgs := make(url.Values, 1)
  111. extraArgs.Set("ttl", "86400000")
  112. response, err := conn.Index(d, extraArgs)
  113. if err != nil {
  114. panic(err)
  115. }
  116. fmt.Printf("%s", response)
  117. }
  118. func ExampleConnection_Delete() {
  119. conn := getConnection()
  120. //[create index, index document ...]
  121. d := goes.Document{
  122. Index: "twitter",
  123. Type: "tweet",
  124. Id: "1",
  125. Fields: map[string]interface{}{
  126. "user": "foo",
  127. },
  128. }
  129. response, err := conn.Delete(d, url.Values{})
  130. if err != nil {
  131. panic(err)
  132. }
  133. fmt.Printf("%s", response)
  134. }
  135. func ExampleConnectionOverrideHttpClient() {
  136. tr := &http.Transport{
  137. ResponseHeaderTimeout: 1 * time.Second,
  138. }
  139. cl := &http.Client{
  140. Transport: tr,
  141. }
  142. conn := getConnection()
  143. conn.WithClient(cl)
  144. fmt.Printf("%v\n", conn.Client)
  145. }