Package com.github.jelmerk.hnswlib.core
Interface Index<TId,TVector,TItem extends Item<TId,TVector>,TDistance>
- Type Parameters:
TId
- Type of the external identifier of an itemTVector
- Type of the vector to perform distance calculation onTItem
- Type of items stored in the indexTDistance
- Type of distance between items (expect any numeric type: float, double, int, ..)
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
BruteForceIndex
,HnswIndex
K-nearest neighbors search index.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
By default after indexing this many items progress will be reported to registered progress listeners. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Add a new item to the index.default void
addAll
(Collection<TItem> items) Add multiple items to the indexdefault void
addAll
(Collection<TItem> items, int numThreads, ProgressListener listener, int progressUpdateInterval) Add multiple items to the index.default void
addAll
(Collection<TItem> items, ProgressListener listener) Add multiple items to the index.default boolean
Check if an item is contained in this indexfindNearest
(TVector vector, int k) Find the items closest to the passed in vector.default List<SearchResult<TItem,
TDistance>> findNeighbors
(TId id, int k) Find the items closest to the item identified by the passed in id.Returns an item by its identifier.items()
Returns all items in the index.boolean
Removes an item from the index.default void
Saves the index to a file.void
save
(OutputStream out) Saves the index to an OutputStream.default void
Saves the index to a path.int
size()
Returns the size of the index.
-
Field Details
-
DEFAULT_PROGRESS_UPDATE_INTERVAL
static final int DEFAULT_PROGRESS_UPDATE_INTERVALBy default after indexing this many items progress will be reported to registered progress listeners.- See Also:
-
-
Method Details
-
add
Add a new item to the index. If an item with the same identifier already exists in the index then : If deletes are disabled on this index the method will return false and the item will not be updated. If deletes are enabled and the version of the item has is higher version than that of the item currently stored in the index the old item will be removed and the new item added, otherwise this method will return false and the item will not be updated.- Parameters:
item
- the item to add to the index- Returns:
- true if the item was added to the index
- Throws:
IllegalArgumentException
- thrown when the item has the wrong dimensionality
-
remove
Removes an item from the index. If the index does not support deletes or an item with the same identifier exists in the index with a higher version number, then this method will return false and the item will not be removed.- Parameters:
id
- unique identifier or the item to removeversion
- version of the delete. If your items don't override version use 0- Returns:
true
if an item was removed from the index. In case the index does not support removals this will always be false
-
contains
Check if an item is contained in this index- Parameters:
id
- unique identifier of the item- Returns:
- true if an item is contained in this index, false otherwise
-
addAll
Add multiple items to the index- Parameters:
items
- the items to add to the index- Throws:
InterruptedException
- thrown when the thread doing the indexing is interrupted
-
addAll
Add multiple items to the index. Reports progress to the passed in implementation ofProgressListener
everyDEFAULT_PROGRESS_UPDATE_INTERVAL
elements indexed.- Parameters:
items
- the items to add to the indexlistener
- listener to report progress to- Throws:
InterruptedException
- thrown when the thread doing the indexing is interrupted
-
addAll
default void addAll(Collection<TItem> items, int numThreads, ProgressListener listener, int progressUpdateInterval) throws InterruptedException Add multiple items to the index. Reports progress to the passed in implementation ofProgressListener
every progressUpdateInterval elements indexed.- Parameters:
items
- the items to add to the indexnumThreads
- number of threads to use for parallel indexinglistener
- listener to report progress toprogressUpdateInterval
- after indexing this many items progress will be reported. The last element will always be reported regardless of this setting.- Throws:
InterruptedException
- thrown when the thread doing the indexing is interrupted
-
size
int size()Returns the size of the index.- Returns:
- size of the index
-
get
Returns an item by its identifier.- Parameters:
id
- unique identifier or the item to return- Returns:
- an item
-
items
Collection<TItem> items()Returns all items in the index.- Returns:
- all items in the index
-
findNearest
Find the items closest to the passed in vector.- Parameters:
vector
- the vectork
- number of items to return- Returns:
- the items closest to the passed in vector
-
findNeighbors
Find the items closest to the item identified by the passed in id. If the id does not match an item an empty list is returned. the element itself is not included in the response.- Parameters:
id
- id of the item to find the neighbors ofk
- number of items to return- Returns:
- the items closest to the item
-
save
Saves the index to an OutputStream. Saving is not thread safe and you should not modify the index while saving.- Parameters:
out
- the output stream to write the index to- Throws:
IOException
- in case of I/O exception
-
save
Saves the index to a file. Saving is not thread safe and you should not modify the index while saving.- Parameters:
file
- file to write the index to- Throws:
IOException
- in case of I/O exception
-
save
Saves the index to a path. Saving is not thread safe and you should not modify the index while saving.- Parameters:
path
- file to write the index to- Throws:
IOException
- in case of I/O exception
-