# Account Filters

## Overview

Several of Carpool's account queries accept a filter object in the POST body. This is an abstraction on top of Elasticsearch and is made to help users perform powerful aggregations and searches. Below you can find all possible filters for **account queries**

## **Account Filters**

<details>

<summary>💰 ACCOUNT_LAMPORTS </summary>

#### Description:

Filter for accounts within a specified lamport range or value

**Schema fields:**

* type<mark style="color:red;">\*</mark>
* range<mark style="color:red;">\*</mark>

#### Schema

```json
"filter": {
    "type": "ACCOUNT_LAMPORTS",
    "range": NumericRange
}
```

#### NumericRange type

```typescript
// Either an equality check, or GTE/LTE range, one of which must be set.
export type NumericRange = XOR<XOR<{
  gte: number;
  lte?: number;
}, {
  gte?: number;
  lte: number;
}>, { eq: number }>;
```

#### Example

```json
"filter": {
    "type": "ACCOUNT_LAMPORTS",
    "range": {
        "gte": 1000000
    }
}
```

</details>

<details>

<summary>🎰 ACCOUNT_SLOTS</summary>

#### Description:

Filter for accounts within a specified slot range or value

**Schema fields:**

* type<mark style="color:red;">\*</mark>
* range<mark style="color:red;">\*</mark>

#### Schema

```json
"filter": {
    "type": "ACCOUNT_SLOTS",
    "range": NumericRange
}
```

#### NumericRange type

```typescript
// Either an equality check, or GTE/LTE range, one of which must be set.
export type NumericRange = XOR<XOR<{
  gte: number;
  lte?: number;
}, {
  gte?: number;
  lte: number;
}>, { eq: number }>;
```

#### Example

```json
"filter": {
    "type": "ACCOUNT_SLOTS",
    "range": {
        "gte": 1000000
    }
}
```

</details>

<details>

<summary>1️⃣  ACCOUNT_DATA_NUMERIC</summary>

#### Description:

Filter for accounts with specific number ranges or values present in the data object

**Schema fields:**

* type<mark style="color:red;">\*</mark>
* path<mark style="color:red;">\*</mark>
* range<mark style="color:red;">\*</mark>

#### Schema

```json
"filter": {
    "type": "ACCOUNT_DATA_NUMERIC",
    "path": "string" // Dot-delimited
    "range": NumericRange
}
```

#### NumericRange type

```typescript
// Either an equality check, or GTE/LTE range, one of which must be set.
export type NumericRange = XOR<XOR<{
  gte: number;
  lte?: number;
}, {
  gte?: number;
  lte: number;
}>, { eq: number }>;
```

#### Example

```json
"filter": {
    "type": "ACCOUNT_DATA_NUMERIC",
    "path": "data.creators.percentageShare",
    "range": {
        "eq": 100
    }
}
```

</details>

<details>

<summary>💭 ACCOUNT_DATA_KEYWORD</summary>

#### Description:

Filter for accounts with specific keywords present in the data object

**Schema fields:**

* type<mark style="color:red;">\*</mark>
* path<mark style="color:red;">\*</mark>
* value<mark style="color:red;">\*</mark>

#### Schema

```json
"filter": {
    "type": "ACCOUNT_DATA_KEYWORD",
    "path": "string", // Dot-delimited for nested fields
    "value": "string"
}
```

#### Example

{% code fullWidth="true" %}

```json
"filter": {
    "type": "ACCOUNT_DATA_KEYWORD",
    "path": "authority",
    "value": "BFB7td8D6f2qvg3cYgLmKSo75GcBFvn69JyjnNRyZuJb"
}
```

{% endcode %}

</details>

<details>

<summary>✅ ACCOUNT_DATA_EXISTS</summary>

#### Description:

Search for accounts where a specific key is present in the data

**Schema fields:**

* type<mark style="color:red;">\*</mark>
* path<mark style="color:red;">\*</mark>

#### Schema

```json
"filter": {
    "type": "ACCOUNT_DATA_EXISTS",
    "path": "string" // Dot-delimited for nested fields
}
```

#### Example

{% code fullWidth="true" %}

```json
"filter": {
    "type": "ACCOUNT_DATA_EXISTS",
    "path": "data.configLineSettings.nameLength"
}
```

{% endcode %}

</details>

## Combining Filters

It is important to note that you can also combine any of the above filters within a single query. You can return results based on an **AND** or **OR** condition.

### AND

#### Schema

```json
"filter": {
    "and": [
        {
            "type": "FILTER_TYPE",
            ...
        },
        {
            "type": "FILTER_TYPE",
            ...
        },
        ...
    ]
}
```

#### Example

```json
"filter": {
    "and": [
        {
            "type": "ACCOUNT_DATA_EXISTS",
            "path": "data.configLineSettings.nameLength"
        },
        {
            "type": "ACCOUNT_SLOTS",
            "path": "data.creators.percentageShare",
            "range": {
                "gte": 10000000
            }
        }
    ]
}
```

### OR

#### Schema

```json
"filter": {
    "or": [
        {
            "type": "FILTER_TYPE",
            ...
        },
        {
            "type": "FILTER_TYPE",
            ...
        },
        ...
    ]
}
```

#### Example

```json
"filter": {
    "or": [
        {
            "type": "ACCOUNT_DATA_EXISTS",
            "path": "data.configLineSettings.nameLength"
        },
        {
            "type": "ACCOUNT_SLOTS",
            "path": "data.creators.percentageShare",
            "range": {
                "gte": 10000000
            }
        }
    ]
}
```

## Negating Filters

You can also negate filters if you'd like to look for all accounts that do **not** match the filter criteria. To negate, simply add  `"negate": true` to the top level of the filter.

#### Example

```json
"filter": {
    "type": "ACCOUNT_DATA_EXISTS",
    "path": "data.configLineSettings.nameLength",
    "negate": true
}
```
