Skip to content

how to index datetime field in metadata within s3 vectors index for query filtering?

0

I want to index some documents into a s3 vector index , but would also like to add some metadata , as a datetime , in a string format. ( see sample below) . as the publication_date field is a string type . would it be possible to filter based on this field ? say i want to search/filter documents between publication_date. how would one do so ? or do i have to use certain datetime format or datetime type instead of string type?

import boto3
from datetime import datetime

# Initialize the S3 Vectors client
s3_vectors_client = boto3.client('s3vectors', region_name='your-aws-region')

# Define your vector data and metadata
vector_id = "document_123"
embedding = [0.1, 0.2, 0.3, ...] # Replace with your actual vector embedding
document_content = "This is the content of the document."
publication_date = "2025-10-09"  #sample date format

# Prepare the metadata dictionary
# You can include other metadata fields as well
metadata = {
    "document_type": "article",
    "publication_date": publication_date,
    "author": "John Doe",
    "source_text": document_content # Can store original text for retrieval
}

# Put the vector with metadata into the S3 Vector index
try:
    response = s3_vectors_client.put_vectors(
        BucketName="your-s3-vector-bucket-name",
        IndexName="your-vector-index-name",
        Vectors=[
            {
                "VectorId": vector_id,
                "Vector": embedding,
                "Metadata": metadata
            }
        ]
    )
    print("Vectors successfully put with filterable date metadata.")
    print(response)
except Exception as e:
    print(f"Error putting vectors: {e}")

asked a year ago254 views

2 Answers
0

Hi! The AI's answer is wrong, so please don't follow it. Two problems.

  1. There is no FilterExpression parameter. S3 Vectors takes a MongoDB-style JSON filter document, not a SQL-like expression string.

  2. $gt, $gte, $lt and $lte only work on numbers. Strings support $eq, $ne, $in, $nin and $exists, nothing else. There is no lexicographic comparison, so "publication_date": "2025-10-09" can't be range-filtered at all, no matter how correct the ISO 8601 formatting is. See Metadata filtering.

Store dates as numbers instead, either YYYYMMDD (20260701) or epoch seconds if you need time-of-day granularity. Whichever you pick, use it for every vector in the index.

resp = client.query_vectors(
    vectorBucketName="my-bucket",
    indexName="my-index",
    topK=10,
    queryVector={"float32": embedding},
    filter={"$and": [
        {"publication_date": {"$gte": 20240101}},
        {"publication_date": {"$lte": 20251231}},
    ]},
    returnMetadata=True,
)

If the records themselves cover a date range, such as events with a start and an end, flip the comparisons to get overlap:

filter={"$and": [
    {"start_date": {"$lte": window_end}},
    {"end_date":   {"$gte": window_start}},
]}

answered a month ago

-1

Yes, you can filter based on the publication_date field in your S3 Vectors index even when it's stored as a string. By default, all metadata attached to vectors is filterable unless explicitly specified as non-filterable during vector index creation.

For datetime filtering, you should use a consistent ISO 8601 format for your dates, such as "2025-10-09" or more precisely "2025-10-09T12:30:10+00:00" if you need time components. This standardized format allows for proper string comparison when filtering.

When querying, you can filter based on this date string using string comparison operators. For example:

# Filter for documents published after a certain date
response = s3_vectors_client.query_vectors(
BucketName="your-s3-vector-bucket-name",
IndexName="your-vector-index-name",
QueryVector=[0.1, 0.2, 0.3, ...],
FilterExpression="publication_date > '2024-01-01'"
)

# Filter for documents within a date range
response = s3_vectors_client.query_vectors(
BucketName="your-s3-vector-bucket-name",
IndexName="your-vector-index-name",
QueryVector=[0.1, 0.2, 0.3, ...],
FilterExpression="publication_date >= '2024-01-01' AND publication_date <= '2025-12-31'"
)

Keep in mind that S3 Vectors supports string, number, boolean, and list types for metadata. There's a limit of 40 KB total metadata per vector, with filterable metadata limited to 2 KB per vector. If you need to store the original document content but don't need to filter on it, consider marking it as non-filterable during vector index creation to save on your filterable metadata quota.
Sources
Working with S3 Vectors and vector buckets - Amazon Simple Storage Service
Prerequisites for using a vector store you created for a knowledge base - Amazon Bedrock
Tutorial: Getting started with S3 Vectors - Amazon Simple Storage Service
Vector indexes - Amazon Simple Storage Service

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.