nmslib / hnswlib

Header-only C++/python library for fast approximate nearest neighbors
https://github.com/nmslib/hnswlib
Apache License 2.0
4.34k stars 642 forks source link

How to change a data point after building index? #22

Closed universewill closed 6 years ago

universewill commented 6 years ago

I want to change a data point in the index. How can i do that?

import hnswlib
import numpy as np

# Generating sample data
a = np.array([1.0, 0.0, 0.0])
b = np.array([0.0, 1.0, 0.0])
c = np.array([0.0, 0.0, 1.0])
d = np.array([1.0, 1.0, 0.0])

# Declaring index
p = hnswlib.Index(space='l2', dim = 3)

# Initing index - the maximum number of elements should be known beforehand
p.init_index(max_elements = 50, ef_construction = 200, M = 16)

# Element insertion (can be called several times):
p.add_items(a)
p.add_items(b)
p.add_items(c)

labels, distances = p.knn_query(c, k =1)
print labels, distances

#change data points
p.add_items(d, 2)
p.add_items(c, 0)

labels, distances = p.knn_query(d, k =1)
print labels, distances

labels, distances = p.knn_query(c, k =1)
print labels, distances

The query result is not changed after i rewrite the data point label index.

yurymalkov commented 6 years ago

@universewill Changing the element would require deleting the element from the index and adding a new one. Not supported currently, see #4. Another possible solution (in case the update does not change the point significantly) is to rewire the links, which is not that much easier than support deletion. What happens in your code is that you add 5 elements with two pairs of which share the same labels.