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.
 
 
 

144 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. searchResults, err := conn.Search(query, []string{"someindex"}, []string{""})
  75. if err != nil {
  76. panic(err)
  77. }
  78. fmt.Printf("%s", searchResults)
  79. }
  80. func ExampleConnection_Index() {
  81. conn := goes.NewConnection("localhost", "9200")
  82. d := goes.Document{
  83. Index: "twitter",
  84. Type: "tweet",
  85. Fields: map[string]interface{}{
  86. "user": "foo",
  87. "message": "bar",
  88. },
  89. }
  90. extraArgs := make(url.Values, 1)
  91. extraArgs.Set("ttl", "86400000")
  92. response, err := conn.Index(d, extraArgs)
  93. if err != nil {
  94. panic(err)
  95. }
  96. fmt.Printf("%s", response)
  97. }
  98. func ExampleConnection_Delete() {
  99. conn := goes.NewConnection("localhost", "9200")
  100. //[create index, index document ...]
  101. d := goes.Document{
  102. Index: "twitter",
  103. Type: "tweet",
  104. Id: "1",
  105. Fields: map[string]interface{}{
  106. "user": "foo",
  107. },
  108. }
  109. response, err := conn.Delete(d, url.Values{})
  110. if err != nil {
  111. panic(err)
  112. }
  113. fmt.Printf("%s", response)
  114. }