TOOL WIKI · VECTOR & SEARCH

Elasticsearch

Combines document search with vector retrieval and filtering capabilities.

Download example files Code + setup + checks in a ZIP
Jump to a section 10 sections

EXAMPLE USE CASE

Add vector recall to an existing search index

A policy assistant needs relevant records with source ownership; vector similarity alone does not establish who may read them.

Input
A dense_vector field, policy document and tenant predicate.
Expected output
The indexed policy among filtered nearest-neighbor hits.
  • Success check: The nearest authorized record is the returns policy; the unrelated tenant never appears.
  • Try a failure: An accepted write may need refresh visibility before search; test each state separately.

Example scope: Actual Elasticsearch API integration; match client/server majors and supply a scratch index.

Official reference · Elasticsearch

WHAT IT ISSearch engine
WHERE IT FITSVector & search
SEE HOW THE SYSTEM FITS TOGETHERSELECT A COMPONENT
ingestindexembed queryscopematchesevidenceSource recordsText + access metadataEmbedding processOne compatible spaceElasticsearchSearch engineUser queryAuthenticated scopeFilter + rankRelevant candidatesAgent contextPassages + provenance
  1. Preserve source identifiers, version, tenant, and deletion status.

  2. Use the same embedding model and dimensions for indexed records and queries.

  3. An application that needs lexical and semantic search over a shared corpus. Evaluate relevance tuning and index refresh behavior for your data.

  4. Resolve allowed tenants and filters before searching for evidence.

  5. Apply resource permissions and select the evidence that supports this task.

  6. Return source text and identifiers, not a vector distance presented as factual certainty.

    Result or stopping point
Elasticsearch

An application that needs lexical and semantic search over a shared corpus. Evaluate relevance tuning and index refresh behavior for your data.

Follow the arrows, then select a component to inspect its responsibility. On mobile, downward arrows connect the next step; bent arrows identify branches and return paths. Select a destination to jump to that component.

When to consider it

  • An application that needs lexical and semantic search over a shared corpus.

A design decision to make

  • Evaluate relevance tuning and index refresh behavior for your data.

Prerequisites and setup

  • An existing Elasticsearch deployment and an API key limited to a dedicated scratch index. Match the Python client major version to the service.
Shell
python3 -m venv .venv
source .venv/bin/activate
pip install elasticsearch
export ELASTIC_URL="https://YOUR_ELASTIC_ENDPOINT"
export ELASTIC_API_KEY="YOUR_SCOPED_KEY"

Try a small integration

search.py

Python · search.py
import os
from elasticsearch import Elasticsearch

client = Elasticsearch(os.environ["ELASTIC_URL"], api_key=os.environ["ELASTIC_API_KEY"])
name = "asg-policies"
if not client.indices.exists(index=name):
    client.indices.create(index=name, mappings={"properties":{
        "tenant":{"type":"keyword"}, "text":{"type":"text"},
        "embedding":{"type":"dense_vector","dims":3,"index":True,"similarity":"cosine"}}})
client.index(index=name,id="returns-a",document={"tenant":"a","text":"Returns: 30 days","embedding":[1.,0.,0.]},refresh="wait_for")
client.index(index=name,id="returns-b",document={"tenant":"b","text":"Private policy","embedding":[1.,0.,0.]},refresh="wait_for")
result = client.search(index=name, knn={"field":"embedding","query_vector":[1.,0.,0.],
    "k":1,"num_candidates":10,"filter":{"term":{"tenant":"a"}}}, source=["tenant","text"])
assert result["hits"]["hits"][0]["_source"]["tenant"] == "a"
print(result["hits"]["hits"])

Run and check the result

Shell
python search.py
  • The nearest authorized record is the returns policy; the unrelated tenant never appears.
  • Change the query vector or tenant filter and inspect the returned IDs, metadata and score ordering.

Common failures

  • TLS errors require the correct CA trust configuration, not verify_certs=False.
  • Refresh visibility, indexing success and search relevance need separate checks.

Production decisions

  • Use embeddings from one named model/version with a fixed dimension and distance metric. The three-value vectors here demonstrate mechanics, not semantic quality.
  • Enforce authenticated tenant/resource filters outside model control. Test filtered recall, deletion, backups, index rebuilds and p95 latency on realistic data.

Keep exploring

Retrieval that can ask a better questionUse PostgreSQL and pgvector for agent memory

Explore the same layer

pgvectorQdrantPineconeWeaviate

Compare candidates on your own workload. This entry describes a product’s role; it is not a claim that products in the category are interchangeable.