Primary index
Prior to Server 6.0, primary index (PI) queries were called scans and had policies defined through the scan policy. See Queries for more information.
Jump to the Code block for a combined complete example.
Basic PI queries have the following features:
- Filter records by set name.
- Filter records by filter expressions.
- Limit the number of records returned, useful for pagination.
- Return only record digests and metadata (generation and TTL).
- Return specified bins.
Setup
The following examples will use the setup and record structure below to illustrate primary index queries in an Aerospike database.
import ( "fmt" "github.com/aerospike/aerospike-client-go/v6")
// Establishes a connection to the serverclient, err := aerospike.NewClient("127.0.0.1", 3000)if err != nil { log.Fatal(err)}defer client.Close()The record structure:
Occurred: IntegerReported: IntegerPosted: IntegerReport: Map{ shape: List, summary: String, city: String, state: String, duration: String}Location: GeoJSONPolicies
See Basic Queries for query policy information.
Query a set
The following example queries the sandbox namespace and ufodata set name, while limiting the record set to only 20 records.
// Create statementstmt := aerospike.NewStatement("sandbox", "ufodata");
// Set max records to returnqueryPolicy := aerospike.NewQueryPolicy()queryPolicy.MaxRecords = 20;
// Execute the queryrecordSet, err := client.Query(queryPolicy, stmt);if err != nil { log.Fatal(err)}
// Get the resultsfor records := range recordSet.Results() { if records != nil { // Do something fmt.Printf("Record: %v \\n", records.Record.Bins) }}
recordSet.Close()Query with a metadata filter
The following example queries the same namespace and set as the example above, but also adds a metadata Filter Expression that will only return records that are greater than 16 KiB.
// Create query policyqueryPolicy := aerospike.NewQueryPolicy()queryPolicy.FilterExpression = aerospike.ExpGreater( aerospike.ExpDeviceSize(), aerospike.ExpIntVal(1024 * 16))
// Create statementstmt := aerospike.NewStatement("sandbox", "ufodata")
// Execute the queryrecordSet, err := client.Query(queryPolicy, stmt)if err != nil { log.Fatal(err)}
// Get the resultsfor records := range recordSet.Results() { if records != nil { // Do something fmt.Printf("Record: %v \\n", records.Record.Bins) }}
recordSet.Close()Query with a data filter
The following example queries the same namespace and set as the example above, but also adds a data Filter Expression that
will only return records where the occurred bin value is in the inclusive range 20200101 to 20211231.
Take a look at secondary index queries to see how this same query can be run more efficiently with an index.
// Create query policyqueryPolicy := aerospike.NewQueryPolicy()queryPolicy.FilterExpression = aerospike.ExpLet( aerospike.ExpDef("bin", aerospike.ExpIntBin("occurred")), aerospike.ExpAnd( aerospike.ExpGreaterEq(aerospike.ExpVar("bin"), aerospike.ExpIntVal(20210101)), aerospike.ExpLessEq(aerospike.ExpVar("bin"), aerospike.ExpIntVal(20211231))))
// Create statementstmt := aerospike.NewStatement("sandbox", "ufodata")
// Execute the queryrecordSet, err := client.Query(queryPolicy, stmt)if err != nil { log.Fatal(err)}
// Get the resultsfor records := range recordSet.Results() { if records != nil { // Do something fmt.Printf("Record: %v \\n", records.Record.Bins) }}
recordSet.Close()Pagination
Pagination uses a combination of a partition filter and a defined maximum records to return query results. The partition filter maintains a cursor identifying the end of the current page and the beginning of the next. Moving to the next page of results is as simple as executing the query again, with the previously defined partition filter.
Defining a maximum number of records per page to return guarantees that no page will contain more than the maximum number, but some pages may contain fewer than the maximum number. Also, if you run the same paginated query multiple times, the number of results per page may differ, depending on the order in which they are delivered by the nodes in the cluster.
The following example executes a query with an Expression Filter identifying records with more than 3 shape items in the report map,
returning 10 records per page. The partition filter is set to query all 4096 partitions in the database.
// Create query policyqueryPolicy := aerospike.NewQueryPolicy()queryPolicy.FilterExpression = aerospike.ExpGreater( aerospike.ExpListSize( aerospike.ExpMapGetByKey(aerospike.MapReturnType.VALUE, aerospike.ExpTypeLIST, aerospike.ExpStringVal("shape"), aerospike.ExpMapBin("report"))), aerospike.ExpIntVal(3))
// Set max records to returnqueryPolicy.MaxRecords = 10;
// Create statementstmt := aerospike.NewStatement("sandbox", "ufodata")
// Create the partition filterpartitionFilter := aerospike.NewPartitionFilterAll()
// Execute the queryrecordSet, err := client.QueryPartitions(queryPolicy, stmt, partitionFilter)if err != nil { log.Fatal(err)}
// Get the resultspage := 0for !partitionFilter.IsDone() { count := 0 for records := range recordSet.Results() { if records != nil { // Do something fmt.Printf("Record: %v \\n", records.Record.Bins) count ++ } } page++ fmt.Printf("\\nPage %v | %v records\\n\\n", page, count) // Execute the query reusing the partitionFilter until all records returned recordSet, err = client.QueryPartitions(queryPolicy, stmt, partitionFilter) if err != nil { log.Fatal(err) }}
recordSet.Close()Code block
Expand this section for a single code block to execute a basic PI query
import ( "fmt" "github.com/aerospike/aerospike-client-go/v6")
func main() { // Establishes a connection to the server client, err := aerospike.NewClient("127.0.0.1", 3000) if err != nil { log.Fatal(err) } defer client.Close()
// Create query policy queryPolicy := aerospike.NewQueryPolicy() queryPolicy.FilterExpression = aerospike.ExpLet( aerospike.ExpDef("bin", aerospike.ExpIntBin("occurred")), aerospike.ExpAnd( aerospike.ExpGreaterEq(aerospike.ExpVar("bin"), aerospike.ExpIntVal(20210101)), aerospike.ExpLessEq(aerospike.ExpVar("bin"), aerospike.ExpIntVal(20211231))))
// Create statement stmt := aerospike.NewStatement("sandbox", "ufodata")
// Execute the query recordSet, err := client.Query(queryPolicy, stmt) if err != nil { log.Fatal(err) }
// Get the results for records := range recordSet.Results() { if records != nil { // Do something fmt.Printf("Record: %v \\n", records.Record.Bins) } }
recordSet.Close()}