Skip to content

Index ontology_class_set on is_obsolete and name (closes #14)#30

Merged
sierra-moxon merged 5 commits into
mainfrom
issue-14-additional-indexes
Jun 2, 2026
Merged

Index ontology_class_set on is_obsolete and name (closes #14)#30
sierra-moxon merged 5 commits into
mainfrom
issue-14-additional-indexes

Conversation

@turbomam

Copy link
Copy Markdown
Member

Why

MongoDBLoader.upsert_ontology_data only declared the id index on ontology_class_set. Common downstream query patterns — filtering out obsoletes, looking up terms by label — would full-collection-scan at NCBITaxon scale (~2.7M docs).

What

Add is_obsolete and name indexes alongside the existing id index. Both are non-unique:

class_collection.index("id", unique=False, name="ontology_class_index")
class_collection.index("is_obsolete", unique=False, name="ontology_class_is_obsolete_index")
class_collection.index("name", unique=False, name="ontology_class_name_index")

Index maintenance during inserts has a small write cost; the speedup for downstream readers is much larger.

A text/substring index on name + alternative_names is deferred until specific search use cases land (per #14's own note).

Verification

make test still passes (11 passed, 8 skipped). The index calls are idempotent — running against an existing collection just confirms the index is present.

Closes #14.

`MongoDBLoader.upsert_ontology_data` previously only declared the `id`
index on `ontology_class_set`. Common downstream query patterns —
filter out obsoletes, lookup by label — would full-collection-scan at
NCBITaxon scale (~2.7M docs).

Add `is_obsolete` and `name` indexes alongside the existing `id` index.
Both are non-unique. Index maintenance during inserts has a small write
cost; the speedup for downstream readers is much larger.

A text/substring index on `name` + `alternative_names` is deferred until
specific search use cases land (per #14's own note).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 20, 2026 15:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves MongoDB query performance for large ontologies (e.g., NCBITaxon scale) by adding additional indexes to the ontology_class_set collection during MongoDBLoader.upsert_ontology_data.

Changes:

  • Add a non-unique index on is_obsolete in ontology_class_set.
  • Add a non-unique index on name in ontology_class_set.
  • Keep existing id and relation compound index behavior unchanged.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/ontology_loader/mongodb_loader.py Outdated
Comment on lines 214 to 216
@@ -216,6 +216,8 @@ def upsert_ontology_data(
relation_collection = self.db.create_collection(relation_collection_name, recreate_if_exists=False)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in commit 2e67c83 — replaced the misleading comment with an accurate description of what create_collection(recreate_if_exists=False) + the index calls actually do.

turbomam and others added 2 commits May 20, 2026 11:58
…iption

Addresses Copilot review on #30: the previous comment claimed the
collections and indexes were set up during initialization. In reality
this method calls create_collection (idempotent, recreate_if_exists=
False) and (re)declares each index right here. Rewrite the comment to
match the actual behavior.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sierra-moxon

Copy link
Copy Markdown
Member

I don't think the is_obsolete index will help much here (low cardinality and low selectivity) name is ok - could we remove the is_obsolete index?

sierra-moxon and others added 2 commits June 1, 2026 10:34
The is_obsolete field is low cardinality / low selectivity, so an index on
it provides little query benefit while still adding write overhead. Keep the
name index, which supports label lookups.

Co-Authored-By: Claude <noreply@anthropic.com>
@sierra-moxon sierra-moxon merged commit e4a047a into main Jun 2, 2026
6 checks passed
@turbomam turbomam deleted the issue-14-additional-indexes branch June 2, 2026 23:23
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.

Add indexes on is_obsolete and name to ontology_class_set

3 participants