You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

example_test.go 3.1 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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/OwnLocal/goes"
  12. )
  13. var (
  14. ES_HOST = "localhost"
  15. ES_PORT = "9200"
  16. )
  17. func getClient() (conn *goes.Client) {
  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.NewClient(h, p)
  27. return
  28. }
  29. func ExampleClient_CreateIndex() {
  30. conn := getClient()
  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 ExampleClient_DeleteIndex() {
  54. conn := getClient()
  55. resp, err := conn.DeleteIndex("yourinde")
  56. if err != nil {
  57. panic(err)
  58. }
  59. fmt.Printf("%s", resp)
  60. }
  61. func ExampleClient_RefreshIndex() {
  62. conn := getClient()
  63. resp, err := conn.RefreshIndex("yourindex")
  64. if err != nil {
  65. panic(err)
  66. }
  67. fmt.Printf("%s", resp)
  68. }
  69. func ExampleClient_Search() {
  70. conn := getClient()
  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 ExampleClient_Index() {
  101. conn := getClient()
  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 ExampleClient_Delete() {
  119. conn := getClient()
  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 ExampleClient_WithHTTPClient() {
  136. tr := &http.Transport{
  137. ResponseHeaderTimeout: 1 * time.Second,
  138. }
  139. cl := &http.Client{
  140. Transport: tr,
  141. }
  142. conn := getClient()
  143. conn.WithHTTPClient(cl)
  144. fmt.Printf("%v\n", conn.Client)
  145. }