Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docarray/index/backends/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,17 @@ def __init__(self, db_config=None, **kwargs):
mappings.update(self._db_config.index_mappings)

for col_name, col in self._column_infos.items():
if col.db_type == 'dense_vector' and (
not col.n_dim and col.config['dims'] < 0
):
self._logger.info(
f'Not indexing column {col_name}, the dimensionality is not specified'
)
continue

mappings['properties'][col_name] = self._create_index_mapping(col)

# print(mappings['properties'])
if self._client.indices.exists(index=self._index_name):
self._client_put_mapping(mappings)
else:
Expand Down Expand Up @@ -231,8 +240,8 @@ def __post_init__(self):

def dense_vector_config(self):
config = {
'dims': -1,
'index': True,
'dims': 128,
'similarity': 'cosine', # 'l2_norm', 'dot_product', 'cosine'
'm': 16,
'ef_construction': 100,
Expand Down
5 changes: 5 additions & 0 deletions tests/index/elastic/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pydantic import Field

from docarray import BaseDoc
from docarray.documents import ImageDoc
from docarray.typing import NdArray

pytestmark = [pytest.mark.slow, pytest.mark.index]
Expand Down Expand Up @@ -58,6 +59,10 @@ class DeepNestedDoc(BaseDoc):
d: NestedDoc


class MyImageDoc(ImageDoc):
embedding: NdArray = Field(dims=128)


@pytest.fixture(scope='function')
def ten_simple_docs():
return [SimpleDoc(tens=np.random.randn(10)) for _ in range(10)]
Expand Down
7 changes: 6 additions & 1 deletion tests/index/elastic/v7/test_index_get_del.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tests.index.elastic.fixture import ( # noqa: F401
DeepNestedDoc,
FlatDoc,
MyImageDoc,
NestedDoc,
SimpleDoc,
start_storage_v7,
Expand Down Expand Up @@ -247,7 +248,7 @@ class MySchema(BaseDoc):

def test_index_multi_modal_doc():
class MyMultiModalDoc(BaseDoc):
image: ImageDoc
image: MyImageDoc
text: TextDoc

store = ElasticV7DocIndex[MyMultiModalDoc]()
Expand All @@ -263,3 +264,7 @@ class MyMultiModalDoc(BaseDoc):
assert store[id_].id == id_
assert np.all(store[id_].image.embedding == doc[0].image.embedding)
assert store[id_].text.text == doc[0].text.text

query = doc[0]
docs, _ = store.find(query, limit=10, search_field='image__embedding')
assert len(docs) > 0
9 changes: 7 additions & 2 deletions tests/index/elastic/v8/test_index_get_del.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tests.index.elastic.fixture import ( # noqa: F401
DeepNestedDoc,
FlatDoc,
MyImageDoc,
NestedDoc,
SimpleDoc,
start_storage_v8,
Expand Down Expand Up @@ -234,7 +235,7 @@ class MyDoc(BaseDoc):
tensor: Union[NdArray, str]

class MySchema(BaseDoc):
tensor: NdArray
tensor: NdArray[128]

store = ElasticDocIndex[MySchema]()
doc = [MyDoc(tensor=np.random.randn(128))]
Expand All @@ -247,7 +248,7 @@ class MySchema(BaseDoc):

def test_index_multi_modal_doc():
class MyMultiModalDoc(BaseDoc):
image: ImageDoc
image: MyImageDoc
text: TextDoc

store = ElasticDocIndex[MyMultiModalDoc]()
Expand All @@ -264,6 +265,10 @@ class MyMultiModalDoc(BaseDoc):
assert np.all(store[id_].image.embedding == doc[0].image.embedding)
assert store[id_].text.text == doc[0].text.text

query = doc[0]
docs, _ = store.find(query, limit=10, search_field='image__embedding')
assert len(docs) > 0


def test_elasticv7_version_check():
with pytest.raises(ImportError):
Expand Down