build_index_from_sources

View as MarkdownOpen in Claude

Build a complete search index from multiple source files and directories. This is the primary method for index construction. It handles file discovery, text extraction, chunking, embedding generation, and storage.

Parameters

sources
list[Path]Required

List of Path objects pointing to files and/or directories to index.

output_file
strRequired

Output path for the .swsearch file (SQLite backend) or collection name (pgvector).

file_types
list[str]Required

File extensions to include when scanning directories (e.g., ["md", "txt", "py"]).

exclude_patterns
list[str] | NoneDefaults to None

Glob patterns for files to exclude (e.g., ["**/node_modules/**"]).

languages
list[str] | NoneDefaults to None

List of language codes to support. Defaults to ["en"].

tags
list[str] | NoneDefaults to None

Global tags to add to every chunk in the index.

overwrite
boolDefaults to false

For the pgvector backend, drop and recreate the collection if it already exists.

Returns

None

Example

from pathlib import Path
from signalwire.search import IndexBuilder
builder = IndexBuilder(chunking_strategy="markdown", verbose=True)
builder.build_index_from_sources(
sources=[Path("./docs"), Path("./examples")],
output_file="knowledge.swsearch",
file_types=["md", "txt", "py"],
exclude_patterns=["**/test_*"],
tags=["documentation"],
)