Skip to content

fix(vector-stores): add agent_id and run_id to Elasticsearch/OpenSearch default mappings#4906

Merged
kartik-mem0 merged 1 commit intomem0ai:mainfrom
jessai2026:fix/es-opensearch-mapping-missing-agent-run-id
Apr 20, 2026
Merged

fix(vector-stores): add agent_id and run_id to Elasticsearch/OpenSearch default mappings#4906
kartik-mem0 merged 1 commit intomem0ai:mainfrom
jessai2026:fix/es-opensearch-mapping-missing-agent-run-id

Conversation

@jessai2026
Copy link
Copy Markdown
Contributor

Linked Issue

Closes #3744

Root Cause

create_index() in both ElasticsearchDB and OpenSearchDB only declared user_id as a keyword in the metadata mapping:

# Before — only user_id is explicit
"metadata": {"type": "object", "properties": {"user_id": {"type": "keyword"}}}

mem0 scopes memories by three canonical entity IDs: user_id, agent_id, and run_id (see memory/main.py:292–297). When a memory with agent_id or run_id is inserted first, both Elasticsearch and OpenSearch dynamically infer a type for those fields from the first value seen. If the inferred type differs from keyword (e.g. ES may infer object when an enum-style value is encountered), subsequent term filter queries on metadata.agent_id or metadata.run_id silently return empty results or raise a TypeError.

Fix

Explicitly declare agent_id and run_id as keyword in the mapping for both stores, matching the existing user_id declaration:

# After — all three scoping IDs are explicit keywords
"metadata": {
    "type": "object",
    "properties": {
        "user_id": {"type": "keyword"},
        "agent_id": {"type": "keyword"},
        "run_id": {"type": "keyword"},
    },
},

Files Changed

File Change
mem0/vector_stores/elasticsearch.py Add agent_id, run_id keyword fields to create_index mapping
mem0/vector_stores/opensearch.py Same fix — identical bug in parallel implementation
tests/vector_stores/test_elasticsearch.py Assert all three ID fields are present as keyword
tests/vector_stores/test_opensearch.py Same assertion

Notes

  • No migration needed for existing indices — ES/OS PUT mapping allows adding new explicit fields to an existing dynamic mapping without reindexing.
  • The OpenSearch file had the same pattern as reported in Incorrect Elasticsearch mapping: cannot get memory results #3744 for Elasticsearch; both are fixed together to keep the two backends consistent.

…ch default mappings

Without explicit keyword mappings for agent_id and run_id, Elasticsearch
and OpenSearch dynamically infer types when the first document is indexed.
This causes filter queries on metadata.agent_id and metadata.run_id to
return empty results or raise a TypeError because the inferred type does
not match the expected keyword term query.

mem0 scopes memories by user_id, agent_id, and run_id. All three fields
must be declared as keyword in the index mapping so that term filters
work reliably from the first insert.

Fixes mem0ai#3744

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kartik-mem0 kartik-mem0 merged commit 573e521 into mem0ai:main Apr 20, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect Elasticsearch mapping: cannot get memory results

2 participants