Skip to content

Commit 666285b

Browse files
committed
elasticsearch len 0 if collection doesn't exist
1 parent 6a03b5e commit 666285b

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

optimade/server/entry_collections/elasticsearch.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,14 @@ def create_elastic_index_from_mapper(
130130

131131
def __len__(self):
132132
"""Returns the total number of entries in the collection."""
133+
from elasticsearch.exceptions import NotFoundError
133134
from elasticsearch_dsl import Search
134135

135-
return Search(using=self.client, index=self.name).execute().hits.total.value
136+
try:
137+
return Search(using=self.client, index=self.name).execute().hits.total.value
138+
except NotFoundError:
139+
# If the collection does not exist, return 0, behaving similarly to MongoDB
140+
return 0
136141

137142
def insert(self, data: list[EntryResource | dict]) -> None:
138143
"""Add the given entries to the underlying database.

0 commit comments

Comments
 (0)