---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://mixpeek.com/.well-known/search?q={query}"
  method: GET
  documentation: "https://mixpeek.com/llms.txt"
---

1.  [](/)
2.  [Code examples](/recipes)
3.  Legal Document RAG Pipeline

Back to code examples

Enhanced

# Legal Document RAG Pipeline

Purpose-built RAG pipeline for legal documents. High-precision retrieval with strong keyword matching for legal terminology, citations, and clause references.

text

Production

[Run in Builder](/build?recipe=legal-document-rag)

Python

cURL

from mixpeek import Mixpeek

client \= Mixpeek(api\_key\="YOUR\_API\_KEY", namespace\="legal-docs")

\# 1. A bucket for contracts, and a collection that splits pages into layout blocks

bucket \= client.buckets.create(

    bucket\_name\="contracts",

    bucket\_schema\={"properties": {"contract": {"type": "pdf"}}},

)

collection \= client.collections.create(

    collection\_name\="contracts",

    source\={"type": "bucket", "bucket\_ids": \[bucket\["bucket\_id"\]\]},

    feature\_extractor\={

        "feature\_extractor\_name": "document\_graph\_extractor",

        "version": "v1",

    },

)

\# 2. Upload and process

client.buckets.upload(

    bucket\["bucket\_id"\],

    blobs\=\[{"property": "contract", "type": "pdf", "data": "s3://your-bucket/contracts/msa-acme.pdf"}\],

)

client.collections.trigger(collection\["collection\_id"\])

\# 3. Dense and BM25 search fused with RRF (BM25 reads the namespace text payload

\#    indexes), then a cross-encoder for precision

retriever \= client.retrievers.create(

    retriever\_name\="legal-search",

    collection\_identifiers\=\["contracts"\],

    input\_schema\={"query": {"type": "text", "required": True}},

    stages\=\[

        {

            "stage\_name": "search",

            "stage\_id": "feature\_search",

            "parameters": {

                "searches": \[

                    {

                        "feature\_uri": "mixpeek://document\_graph\_extractor@v1/intfloat\_\_multilingual\_e5\_large\_instruct",

                        "query": {"input\_mode": "text", "value": "{{INPUT.query}}"},

                        "top\_k": 50,

                    },

                    {

                        "feature\_uri": "mixpeek://document\_graph\_extractor@v1/intfloat\_\_multilingual\_e5\_large\_instruct",

                        "query": {"input\_mode": "text", "value": "{{INPUT.query}}"},

                        "top\_k": 50,

                        "lexical": True,

                    },

                \],

                "fusion": "rrf",

                "final\_top\_k": 50,

            },

        },

        {

            "stage\_name": "rerank",

            "stage\_id": "rerank",

            "parameters": {

                "inference\_name": "BAAI\_\_bge\_reranker\_v2\_m3",

                "query": "{{INPUT.query}}",

                "document\_field": "text\_raw",

                "top\_k": 10,

            },

        },

    \],

)

results \= client.retrievers.execute(retriever\["retriever\_id"\], inputs\={"query": "Section 14.2 indemnification obligations"})

for doc in results\["documents"\]:

    print(doc\["page\_number"\], doc\["text\_raw"\]\[:120\], doc\["score"\])

### Feature Extractors

Document Graph Extractor

Decompose PDFs into spatial blocks (paragraphs, tables, forms, headers) with layout classification and E5 text embeddings.

### Retriever Stages

feature search

Search and filter documents by vector similarity using feature embeddings

filter

rerank

Rerank documents using cross-encoder models for accurate relevance

sort

## Frequently asked questions

### Why is BM25 weight higher than vector weight?

Legal documents rely heavily on exact terminology and section references. Higher BM25 weight ensures precise term matching alongside semantic understanding.

### Does it handle redacted documents?

Yes. The OCR and PDF extractors process visible content; redacted sections are identified and excluded from indexing.

### Can I search across multiple contract versions?

Yes. Upload all versions and use metadata filters to compare specific clauses across document versions.

## Related code examples and resources

Pages on the same stages, extractors and patterns.

[

Extractor

### Document Graph Extractor

Decompose PDFs into spatial blocks (paragraphs, tables, forms, headers) with layout classification and E5 text embeddings.

Learn more





](/extractors/document-graph)[

Extractor

### Web Scraper

Extract structured data from webpages while maintaining semantic context and relationships

Learn more





](/extractors/web-scraper)[

Extractor

### Text Embedding

Extract semantic embeddings from documents, transcripts and text content

Learn more





](/extractors/text)[

Recipe

### Multimodal RAG Pipeline

Build a retrieval-augmented generation system that works with text, images, and video. Feed relevant multimodal context to LLMs for grounded responses.

Learn more





](/recipes/multimodal-rag-pipeline)[

Recipe

### Taxonomy Enrichment Pipeline

Automatically classify and tag content using custom taxonomies. Map your content to IAB categories, custom hierarchies, or industry-specific classifications.

Learn more





](/recipes/taxonomy-enrichment-pipeline)[

Recipe

### Content Clustering Pipeline

Automatically group similar content together using embedding-based clustering. Discover themes, identify duplicates, and organize large content libraries.

Learn more





](/recipes/clustering-pipeline)