Guest User

Untitled

a guest
May 16th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "github.com/couchbase/gocb"
  5. "fmt"
  6. "github.com/couchbase/gocb/cbft"
  7. )
  8.  
  9. type User struct {
  10. Id string `json:"uid"`
  11. Email string `json:"email"`
  12. Interests []string `json:"interests"`
  13. }
  14.  
  15. func main() {
  16. cluster, _ := gocb.Connect("couchbase://172.23.120.18")
  17. cluster.Authenticate(gocb.PasswordAuthenticator{
  18. Username: "Administrator",
  19. Password: "password",
  20. })
  21. bucket, _ := cluster.OpenBucket("default", "")
  22.  
  23. /*bucket.Insert("key1", nil, 0)
  24. bucket.Insert("key2", User{
  25. Id: "kingarthur",
  26. Email: "kingarthur@couchbase.com",
  27. Interests: []string{"Holy Grail", "African Swallows"},
  28. }, 0)
  29. */
  30. ftsIndexName := "ftsIndex"
  31. //ftsIndexName := "travel-index"
  32. //term := fmt.Sprintf("SampleValue%d", 1)
  33. term := fmt.Sprintf("SampleValue1")
  34. //term := fmt.Sprintf("office")
  35. query := gocb.NewSearchQuery(ftsIndexName, cbft.NewTermQuery(term))
  36. res, err := bucket.ExecuteSearchQuery(query)
  37. if err != nil {
  38. fmt.Printf("Err:%s", err)
  39. }
  40.  
  41. for _, hit := range res.Hits() {
  42. fmt.Printf("hit:%s\n", hit.Id)
  43. }
  44.  
  45.  
  46. if res.Errors != nil || res.Status().Total != 1 {
  47. if res.Errors != nil {
  48. for err := range res.Errors() {
  49. fmt.Printf("Err:%s", err)
  50. }
  51. }
  52. if len(res.Hits()) != 1 {
  53. fmt.Printf("Err:Hits=%d", res.Status().Total)
  54. }
  55. return
  56. }
  57. }
Add Comment
Please, Sign In to add comment