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.
 
 
 

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