# Primary index

Prior to Server 6.0, primary index (PI) queries were called _scans_ and had policies defined through the scan policy. See [Queries](https://aerospike.com/docs/develop/learn/queries/) for more information.

Jump to the [Code block](#code-block) for a combined complete example.

Basic PI queries have the following features:

-   Filter records by set name.
-   Filter records by [filter expressions](https://aerospike.com/docs/develop/expressions/#record-filtering-with-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.

```go
import (

    "fmt"

    "github.com/aerospike/aerospike-client-go/v6"

)

// Establishes a connection to the server

client, err := aerospike.NewClient("127.0.0.1", 3000)

if err != nil {

  log.Fatal(err)

}

defer client.Close()
```

The record structure:

```asciidoc
Occurred: Integer

Reported: Integer

Posted: Integer

Report: Map

{

    shape: List,

    summary: String,

    city: String,

    state: String,

    duration: String

}

Location: GeoJSON
```

## Policies

See [Basic Queries](https://aerospike.com/docs/develop/client/go/usage/multi/queries/basic#policies) 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.

::: note
If the you are querying a smaller set, creating a [set index](https://aerospike.com/docs/database/learn/architecture/data-storage/set-index) may provide better performance.
:::

```go
// Create statement

stmt := aerospike.NewStatement("sandbox", "ufodata");

// Set max records to return

queryPolicy := aerospike.NewQueryPolicy()

queryPolicy.MaxRecords = 20;

// 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()
```

## 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.

```go
// Create query policy

queryPolicy := aerospike.NewQueryPolicy()

queryPolicy.FilterExpression = aerospike.ExpGreater(

    aerospike.ExpDeviceSize(),

    aerospike.ExpIntVal(1024 * 16))

// 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()
```

## 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](https://aerospike.com/docs/develop/client/go/usage/multi/queries/secondary) to see how this same query can be run more efficiently with an index.

```go
// 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()
```

## 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.

```go
// Create query policy

queryPolicy := 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 return

queryPolicy.MaxRecords = 10;

// Create statement

stmt := aerospike.NewStatement("sandbox", "ufodata")

// Create the partition filter

partitionFilter := aerospike.NewPartitionFilterAll()

// Execute the query

recordSet, err := client.QueryPartitions(queryPolicy, stmt, partitionFilter)

if err != nil {

  log.Fatal(err)

}

// Get the results

page := 0

for !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

```go
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()

}
```