

# Filter search results

A `filter` restricts search results to one entity type or to entities whose attributes match a condition. Use `query` for text matching and `filter` to narrow the entity type or attributes.

A filter is one condition (`equalTo`, `notEqualTo`, `greaterThan`, `greaterThanOrEqualTo`, `lessThan`, `lessThanOrEqualTo`, or `in`) or an `andAll` of 2 to 5 conditions. Each condition names a `key` and a `value`. The key is a structural field (`nodeType`, `entityId`, `entityType`, `entityClass`) or a property that your ontology declares as filterable. Range conditions work on numeric and date properties. There is no `between`; write a range as an `andAll` of two comparisons.

Entities that don’t have the attribute are left out. If two entity types define the same property name with different types, add a `nodeType` condition. Without it, the request fails with `ValidationException` because the key is ambiguous.

For the `filter` fields, see `SearchAgentContext` in the * AWS Context API Reference*.

## To filter search results
<a name="to-filter-search-results"></a>

The following example searches for `revenue` and restricts the results to columns.

```bash
aws context search-agent-context --cli-input-json '{
  "agentContextId": "ac-1234567890abcdef",
  "query": "revenue",
  "searchType": "AUTO",
  "filter": { "equalTo": { "key": "nodeType", "value": { "stringValue": "AWS::GLUE::COLUMN" } } }
}'
```

The following example runs the same search with the AWS SDK for Python (Boto3).

```python
import boto3

client = boto3.client("context", region_name="us-east-1")
response = client.search_agent_context(
    agentContextId="ac-1234567890abcdef",
    query="revenue",
    searchType="AUTO",
    filter={"equalTo": {"key": "nodeType", "value": {"stringValue": "AWS::GLUE::COLUMN"}}},
)
for item in response["items"]:
    print(item["nodeId"], item.get("summary"))
```