Hey @sid1 your question keeps changing so I would highly recommend you checkout our client docs and tutorials to cover more of these scenarios.
Here is some code that you can run in our sandbox to see it working. Just select Go
as the language and click connect. Run these snippets in order.
- Write the record
// Import Aerospike client library
import (
"fmt"
as "github.com/aerospike/aerospike-client-go/v6"
)
// Establish connection to the server
client, err := as.NewClient("127.0.0.1", 3000)
// Initialize a write policy
policy := as.NewWritePolicy(0,0)
policy.SendKey = true
// Create a key in namespace "test" and set "data"
key, err := as.NewKey("test", "data", 1)
// Create the bin
bin := as.NewBin("A", map[string]interface{}{
"B": map[string]interface{}{
"E": []int{909},
"G": 0},
"TEST": map[string]interface{}{
"NEW": []int{468, 198}},
"D": map[string]interface{}{
"id": []int{468, 198}}})
// Write the record to Aerospike
err = client.PutBins(policy, key, bin)
if err != nil {
fmt.Printf("Error: %s", err)
}
record, err := client.Get(nil, key)
fmt.Printf("Create succeeded\nKey: %s\nRecord: %s\n", key.Value(), record)
- Create the index
task, err := client.CreateComplexIndex(nil, // write policy
"test", // namespace
"data", // set name
"test_idx", // index name
"A", // bin name
as.STRING,
as.ICT_MAPKEYS,
as.CtxMapKey(as.NewStringValue("TEST")))
// Wait for the task to complete
<-task.OnComplete()
- Execute the query
// Create statement
stmt := as.NewStatement("test", "data")
// Create index filter
stmt.SetFilter(as.NewContainsFilter("A", as.ICT_MAPKEYS, as.NewStringValue("NEW"), as.CtxMapKey(as.NewStringValue("TEST"))))
// Execute the query
recordSet, err := client.Query(nil, stmt);
// Get the results
for records := range recordSet.Results() {
if records != nil {
// Do something
fmt.Printf("Record: %v \n", records.Record)
}
}
recordSet.Close()