We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 51097c0 commit e1030c9Copy full SHA for e1030c9
1 file changed
optimade/server/entry_collections/elasticsearch.py
@@ -130,9 +130,14 @@ def create_elastic_index_from_mapper(
130
131
def __len__(self):
132
"""Returns the total number of entries in the collection."""
133
+ from elasticsearch.exceptions import NotFoundError
134
from elasticsearch_dsl import Search
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
141
142
def insert(self, data: list[EntryResource | dict]) -> None:
143
"""Add the given entries to the underlying database.
0 commit comments