Amazon S3
full text search
data search
cloud storage
information retrieval

How to full-text search an Amazon S3 bucket

Codemia Knowledge Hub ·

You can't full-text search an S3 bucket with S3 alone. S3 is object storage: it lists objects by key and prefix, but it does not index what is inside them. The working approach is to extract text from each object, index that text in a search engine such as Amazon OpenSearch Service, and send your queries to the index.

What S3 does and does not do

S3 stores objects and their metadata, and retrieves them efficiently when you already know the key, prefix or surrounding workflow. It does not tokenize text, build inverted indexes, rank results, or pull content out of PDFs, Word documents or HTML files. So full-text search over a bucket takes three steps:

  1. Extract text from the objects.
  2. Index that text in a system built for search.
  3. Query the index, not S3.

The usual AWS architecture

  • S3 stores the original files.
  • An event trigger detects new or changed objects.
  • A processor extracts text and metadata.
  • Amazon OpenSearch Service holds the searchable index.
text
S3 upload -> Lambda or batch job -> text extraction -> OpenSearch index -> search API

S3 stays the durable source of truth for the files, and the search engine handles ranking and retrieval.

Indexing uploaded text files with Lambda

For plain text files, a Lambda function can read each new object and index it in OpenSearch. A simplified example:

python
1import boto3
2import json
3import urllib.parse
4from opensearchpy import OpenSearch
5
6s3 = boto3.client("s3")
7client = OpenSearch(
8    hosts=[{"host": "search-example.us-east-1.es.amazonaws.com", "port": 443}],
9    use_ssl=True,
10    verify_certs=True,
11)
12
13def lambda_handler(event, context):
14    record = event["Records"][0]
15    bucket = record["s3"]["bucket"]["name"]
16    key = urllib.parse.unquote_plus(record["s3"]["object"]["key"])
17
18    response = s3.get_object(Bucket=bucket, Key=key)
19    text = response["Body"].read().decode("utf-8")
20
21    document = {
22        "bucket": bucket,
23        "key": key,
24        "content": text,
25    }
26
27    client.index(index="s3-documents", id=key, body=document)
28
29    return {"statusCode": 200, "body": json.dumps("indexed")}

Extracting text from PDFs and Office files

For PDFs or Office documents, add a text extraction step before indexing. Without it, those files look "missing" from search results. Common options:

  • AWS Lambda with a library that parses PDF or Office formats
  • AWS Textract for scanned or image-heavy documents
  • A containerized batch job for large or complex documents

Index the extracted text together with useful metadata such as object key, upload time, tags, customer ID or document type. Indexing only filenames or object keys is not full-text search.

Why Athena and S3 Select are not enough

  • Amazon Athena runs SQL over structured data stored in S3, such as JSON, CSV or Parquet.
  • S3 Select retrieves subsets of structured object content.

Neither is a relevance-ranked document search engine. For logs, tables or newline-delimited JSON, Athena may be enough. For "search every word inside uploaded documents," using Athena gives a poor experience; use a search index.

Querying the index

Once the documents are in OpenSearch, a search is a simple query against the indexed content:

python
1query = {
2    "query": {
3        "match": {
4            "content": "customer refund policy"
5        }
6    }
7}
8
9results = client.search(index="s3-documents", body=query)
10for hit in results["hits"]["hits"]:
11    print(hit["_source"]["key"])

Keeping the index in sync with the bucket

A production setup needs more than the initial indexing:

  • Re-index documents when objects are replaced.
  • Delete index entries when S3 objects are deleted, or the index drifts away from the bucket.
  • Store ACL-related metadata if access control matters.
  • Normalize text, language and file encodings.
  • Monitor indexing failures and dead-letter events.

For large buckets, run a one-time backfill job so the index also covers objects that existed before the trigger was set up.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design