Skip to content

feat: Add declarative API for vector and full-text indexes#109

Merged
Mini256 merged 6 commits intomainfrom
feat-index-decl-api
Jun 25, 2025
Merged

feat: Add declarative API for vector and full-text indexes#109
Mini256 merged 6 commits intomainfrom
feat-index-decl-api

Conversation

@Mini256
Copy link
Copy Markdown
Member

@Mini256 Mini256 commented Jun 24, 2025

close #89
close #103

Features

Declarative API:

  • Vector Index is automatically created when using a VectorField.
  • FullText Index is created automatically when using FullTextField.
  • Supports passing VectorIndex / FullTextIndex objects in the __table_args__ field of the TableModel to define indexes.

Command API:

  • Supports manual index creation by using VectorIndex("vec_idx", table.text_vec).create(db_engine)

Breaking changes

  • Since the way to add a columnar replica is implemented using ADD_COLUMNAR_REPLICA_ON_DEMAND, it may not work properly with the OP version, which does not support the ADD_COLUMNAR_REPLICA_ON_DEMAND syntax.

Example code

Define vector index via VectorField (best practice)

class Chunk(TableModel):
    __tablename__ = "test_vector_index"
    id: int = Field(primary_key=True)
    text_vec: list[float] = VectorField(dimensions=3)    #  👈 index=True by default

tbl = client.create_table(schema=Chunk, mode="overwrite")
assert tbl.has_vector_index("text_vec")

Manual create vector index

class Chunk(TableModel):
    __tablename__ = "test_vector_index"
    id: int = Field(primary_key=True)
    text_vec: list[float] = VectorField(dimensions=3, index=False)

tbl = client.create_table(schema=Chunk, mode="overwrite")
tbl.create_vector_index(name="vec_idx_on_text_vec", column_name="text_vec")

Define vector index via FullTextField (best practice)

class Chunk(TableModel):
    __tablename__ = "test_fulltext_index"
    id: int = Field(primary_key=True)
    text: str = FullTextField()    #  👈 index=True by default

tbl = client.create_table(schema=Chunk, mode="overwrite")
assert tbl.has_fts_index("text")

@Mini256 Mini256 requested review from Icemap and breezewish June 24, 2025 03:45
@Mini256 Mini256 changed the title feat: Add decl API for vector and full-text indexes feat: Add declarative API for vector and full-text indexes Jun 24, 2025
Copy link
Copy Markdown
Member

@Icemap Icemap left a comment

Choose a reason for hiding this comment

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

LGTM

@breezewish
Copy link
Copy Markdown
Member

breezewish commented Jun 25, 2025

Two ideas.

1

It may be better to be:

self.client.db_engine.create_vector_index("vec_idx_on_text_vec", Chunk.text_vec)

2

Normally it is better to only have one way (or try best to have minimal ways) to achieve one thing, instead of several ways to achieve the same thing. Because the later one leads user to think, to choose, to learn difference, to have "best practice", which is a waste of time. (BTW this is the philosophy Golang follows). For this reason, possibly we only need two examples for now:

A. Define a vector field (with vector index) using VectorField
B. Define a vector field without vector index using VectorField

I do think it may be useful to create index lazily over an existing data. However we need to carefully think about under what scenario we can bring this feature to users. Needs some more work.

The rest LGTM.

@Mini256 Mini256 merged commit 879bd01 into main Jun 25, 2025
1 of 2 checks passed
@Mini256 Mini256 deleted the feat-index-decl-api branch June 25, 2025 06:16
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.

declarative API: VectorField(index=True) for vector index declarative API: TextField(index=True) for fulltext index

3 participants