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.
 
 
 

161 lines
3.0 KiB

  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. "time"
  10. "github.com/belogik/goes"
  11. )
  12. func ExampleConnection_CreateIndex() {
  13. conn := goes.NewConnection("localhost", "9200")
  14. mapping := map[string]interface{}{
  15. "settings": map[string]interface{}{
  16. "index.number_of_shards": 1,
  17. "index.number_of_replicas": 0,
  18. },
  19. "mappings": map[string]interface{}{
  20. "_default_": map[string]interface{}{
  21. "_source": map[string]interface{}{
  22. "enabled": true,
  23. },
  24. "_all": map[string]interface{}{
  25. "enabled": false,
  26. },
  27. },
  28. },
  29. }
  30. resp, err := conn.CreateIndex("test", mapping)
  31. if err != nil {
  32. panic(err)
  33. }
  34. fmt.Printf("%s", resp)
  35. }
  36. func ExampleConnection_DeleteIndex() {
  37. conn := goes.NewConnection("localhost", "9200")
  38. resp, err := conn.DeleteIndex("yourinde")
  39. if err != nil {
  40. panic(err)
  41. }
  42. fmt.Printf("%s", resp)
  43. }
  44. func ExampleConnection_RefreshIndex() {
  45. conn := goes.NewConnection("localhost", "9200")
  46. resp, err := conn.RefreshIndex("yourindex")
  47. if err != nil {
  48. panic(err)
  49. }
  50. fmt.Printf("%s", resp)
  51. }
  52. func ExampleConnection_Search() {
  53. conn := goes.NewConnection("localhost", "9200")
  54. var query = map[string]interface{}{
  55. "query": map[string]interface{}{
  56. "bool": map[string]interface{}{
  57. "must": map[string]interface{}{
  58. "match_all": map[string]interface{}{},
  59. },
  60. },
  61. },
  62. "from": 0,
  63. "size": 100,
  64. "fields": []string{"onefield"},
  65. "filter": map[string]interface{}{
  66. "range": map[string]interface{}{
  67. "somefield": map[string]interface{}{
  68. "from": "some date",
  69. "to": "some date",
  70. "include_lower": false,
  71. "include_upper": false,
  72. },
  73. },
  74. },
  75. }
  76. extraArgs := make(url.Values, 1)
  77. searchResults, err := conn.Search(query, []string{"someindex"}, []string{""}, extraArgs)
  78. if err != nil {
  79. panic(err)
  80. }
  81. fmt.Printf("%s", searchResults)
  82. }
  83. func ExampleConnection_Index() {
  84. conn := goes.NewConnection("localhost", "9200")
  85. d := goes.Document{
  86. Index: "twitter",
  87. Type: "tweet",
  88. Fields: map[string]interface{}{
  89. "user": "foo",
  90. "message": "bar",
  91. },
  92. }
  93. extraArgs := make(url.Values, 1)
  94. extraArgs.Set("ttl", "86400000")
  95. response, err := conn.Index(d, extraArgs)
  96. if err != nil {
  97. panic(err)
  98. }
  99. fmt.Printf("%s", response)
  100. }
  101. func ExampleConnection_Delete() {
  102. conn := goes.NewConnection("localhost", "9200")
  103. //[create index, index document ...]
  104. d := goes.Document{
  105. Index: "twitter",
  106. Type: "tweet",
  107. Id: "1",
  108. Fields: map[string]interface{}{
  109. "user": "foo",
  110. },
  111. }
  112. response, err := conn.Delete(d, url.Values{})
  113. if err != nil {
  114. panic(err)
  115. }
  116. fmt.Printf("%s", response)
  117. }
  118. func ExampleConnectionOverrideHttpClient() {
  119. tr := &http.Transport{
  120. ResponseHeaderTimeout: 1 * time.Second,
  121. }
  122. cl := &http.Client{
  123. Transport: tr,
  124. }
  125. conn := goes.NewConnection("localhost", "9200").WithClient(cl)
  126. fmt.Printf("%v\n", conn.Client)
  127. }