Skip to content

Configuration

Configuration options for HNSW index.

max_connections, metric and precision are fixed when an index is created, with the defaults below for any left out. Opening it again takes each from the file unless it's given, and checks any that is.

Attributes:

Name Type Description
max_connections int | None

Maximum connections per node (M parameter). Higher = better recall, more memory. Default: 16 when creating

ef_construction int

Construction quality parameter. Higher = better index quality, slower build. Default: 200

ef_search int

Search quality parameter. Higher = better search quality, slower search. Default: 50

metric str | None

"euclidean" (L2) or "cosine" (1 - cosine similarity; vectors are stored at unit length and zero vectors are rejected). Default: "euclidean" when creating

precision str | None

"full" keeps each component of a vector as a 32-bit float, "half" as a 16-bit float: half the file and half the memory, each component rounded to about three decimal digits, and a vector with a component of 65,520 or more in magnitude refused. Default: "full" when creating

huge_pages bool | None

Whether to ask the operating system to keep the vectors on huge pages. On an index too large for the CPU's caches, searches are up to a quarter faster. Only on Linux, and only where the kernel and filesystem keep files on huge pages (ext4 on Linux 6.17 does); elsewhere it does nothing. True asks from the moment the index is opened: a page not yet in memory is then read 2 MB at a time, which an index much larger than memory pays for on every miss. False never asks. None leaves it to warm. Default: None

warm bool

Read the whole index into memory on another thread from the moment it is opened, so that searches stop waiting for the disk one page at a time. For an index that fits in memory. It takes Linux 5.14 or macOS: an older Linux reads only some of the index, and Windows none. Unless huge_pages says otherwise, it asks for huge pages under vectors of 2 KB or more. Default: False

Source code in chassisdb/index.py
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@dataclass
class IndexOptions:
    """Configuration options for HNSW index.

    ``max_connections``, ``metric`` and ``precision`` are fixed when an index is
    created, with the defaults below for any left out. Opening it again takes
    each from the file unless it's given, and checks any that is.

    Attributes:
        max_connections: Maximum connections per node (M parameter).
            Higher = better recall, more memory. Default: 16 when creating
        ef_construction: Construction quality parameter.
            Higher = better index quality, slower build. Default: 200
        ef_search: Search quality parameter.
            Higher = better search quality, slower search. Default: 50
        metric: "euclidean" (L2) or "cosine" (1 - cosine similarity; vectors
            are stored at unit length and zero vectors are rejected).
            Default: "euclidean" when creating
        precision: "full" keeps each component of a vector as a 32-bit float,
            "half" as a 16-bit float: half the file and half the memory, each
            component rounded to about three decimal digits, and a vector with
            a component of 65,520 or more in magnitude refused.
            Default: "full" when creating
        huge_pages: Whether to ask the operating system to keep the vectors
            on huge pages. On an index too large for the CPU's caches,
            searches are up to a quarter faster. Only on Linux, and only where
            the kernel and filesystem keep files on huge pages (ext4 on Linux
            6.17 does); elsewhere it does nothing. True asks from the moment
            the index is opened: a page not yet in memory is then read 2 MB at
            a time, which an index much larger than memory pays for on every
            miss. False never asks. None leaves it to ``warm``. Default: None
        warm: Read the whole index into memory on another thread from the
            moment it is opened, so that searches stop waiting for the disk
            one page at a time. For an index that fits in memory. It takes
            Linux 5.14 or macOS: an older Linux reads only some of the
            index, and Windows none. Unless ``huge_pages`` says otherwise, it
            asks for huge pages under vectors of 2 KB or more. Default: False
    """

    max_connections: int | None = None
    ef_construction: int = 200
    ef_search: int = 50
    metric: str | None = None
    precision: str | None = None
    huge_pages: bool | None = None
    warm: bool = False

    def validate(self) -> None:
        """Check the options, and spell metric and precision as Chassis does:
        "L2" becomes "euclidean" and "float16" becomes "half".

        Raises:
            InvalidArgumentError: If an option is out of range or unknown
            TypeError: If an option has the wrong type
        """
        for name in ("ef_construction", "ef_search"):
            _check_int(name, getattr(self, name))
        if (
            self.max_connections is not None
            and not 2 <= _check_int("max_connections", self.max_connections) <= 32767
        ):
            raise InvalidArgumentError(
                f"max_connections is {self.max_connections}, but has to be between 2 and "
                "32,767\nhelp: 16 suits most indexes; 32 gives higher recall for more memory"
            )
        for name in ("ef_construction", "ef_search"):
            if getattr(self, name) < 1:
                raise InvalidArgumentError(
                    f"{name} is {getattr(self, name)}, but has to be at least 1\nhelp: higher "
                    f"finds more of the true nearest for more time; the default is "
                    f"{getattr(IndexOptions(), name)}"
                )
        if self.metric is not None:
            self.metric = _named("metric", self.metric, _METRICS, _METRIC_NAMES)
        if self.precision is not None:
            self.precision = _named("precision", self.precision, _PRECISIONS, _PRECISION_NAMES)

validate()

Check the options, and spell metric and precision as Chassis does: "L2" becomes "euclidean" and "float16" becomes "half".

Raises:

Type Description
InvalidArgumentError

If an option is out of range or unknown

TypeError

If an option has the wrong type

Source code in chassisdb/index.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def validate(self) -> None:
    """Check the options, and spell metric and precision as Chassis does:
    "L2" becomes "euclidean" and "float16" becomes "half".

    Raises:
        InvalidArgumentError: If an option is out of range or unknown
        TypeError: If an option has the wrong type
    """
    for name in ("ef_construction", "ef_search"):
        _check_int(name, getattr(self, name))
    if (
        self.max_connections is not None
        and not 2 <= _check_int("max_connections", self.max_connections) <= 32767
    ):
        raise InvalidArgumentError(
            f"max_connections is {self.max_connections}, but has to be between 2 and "
            "32,767\nhelp: 16 suits most indexes; 32 gives higher recall for more memory"
        )
    for name in ("ef_construction", "ef_search"):
        if getattr(self, name) < 1:
            raise InvalidArgumentError(
                f"{name} is {getattr(self, name)}, but has to be at least 1\nhelp: higher "
                f"finds more of the true nearest for more time; the default is "
                f"{getattr(IndexOptions(), name)}"
            )
    if self.metric is not None:
        self.metric = _named("metric", self.metric, _METRICS, _METRIC_NAMES)
    if self.precision is not None:
        self.precision = _named("precision", self.precision, _PRECISIONS, _PRECISION_NAMES)

A single search result.

Attributes:

Name Type Description
id int

Vector ID in the index

distance float

Distance to the query vector (lower is closer)

Source code in chassisdb/index.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
@dataclass
class SearchResult:
    """A single search result.

    Attributes:
        id: Vector ID in the index
        distance: Distance to the query vector (lower is closer)
    """

    id: int
    distance: float

    def __repr__(self) -> str:
        return f"SearchResult(id={self.id}, distance={self.distance:.6f})"