Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jaekwonpark/ca30422599e43f4703d1f7b643bc47c8 to your computer and use it in GitHub Desktop.
Save jaekwonpark/ca30422599e43f4703d1f7b643bc47c8 to your computer and use it in GitHub Desktop.
Go FTS Code
package main
import (
"github.com/couchbase/gocb"
"fmt"
"github.com/couchbase/gocb/cbft"
)
type User struct {
Id string `json:"uid"`
Email string `json:"email"`
Interests []string `json:"interests"`
}
func main() {
cluster, _ := gocb.Connect("couchbase://172.23.120.18")
cluster.Authenticate(gocb.PasswordAuthenticator{
Username: "Administrator",
Password: "password",
})
bucket, _ := cluster.OpenBucket("default", "")
/*bucket.Insert("key1", nil, 0)
bucket.Insert("key2", User{
Id: "kingarthur",
Email: "[email protected]",
Interests: []string{"Holy Grail", "African Swallows"},
}, 0)
*/
ftsIndexName := "ftsIndex"
//ftsIndexName := "travel-index"
//term := fmt.Sprintf("SampleValue%d", 1)
term := fmt.Sprintf("SampleValue1")
//term := fmt.Sprintf("office")
query := gocb.NewSearchQuery(ftsIndexName, cbft.NewTermQuery(term))
res, err := bucket.ExecuteSearchQuery(query)
if err != nil {
fmt.Printf("Err:%s", err)
}
for _, hit := range res.Hits() {
fmt.Printf("hit:%s\n", hit.Id)
}
if res.Errors != nil || res.Status().Total != 1 {
if res.Errors != nil {
for err := range res.Errors() {
fmt.Printf("Err:%s", err)
}
}
if len(res.Hits()) != 1 {
fmt.Printf("Err:Hits=%d", res.Status().Total)
}
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment