Created
May 11, 2018 05:57
-
-
Save jaekwonpark/ca30422599e43f4703d1f7b643bc47c8 to your computer and use it in GitHub Desktop.
Go FTS Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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