Skip to content

Commit

Permalink
Make TestBatchIndexer.test_delete pass under ES 7
Browse files Browse the repository at this point in the history
Omit the `_type` field from the action payload under ES 7, to match
`TestBatchIndexer._prepare`.

For context, see notes in commit 63f042c.
  • Loading branch information
robertknight committed Nov 11, 2024
1 parent 1dd4a30 commit 3a06584
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions h/search/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def index(self, annotation_ids: list, windowsize: int = PG_WINDOW_SIZE):

def delete(self, annotation_ids: list[str]) -> None:
"""Delete `annotation_ids` from Elasticsearch."""

# Delete the given annotations from Elasticsearch by sending them to
# Elasticsearch's bulk API in chunks.
#
Expand All @@ -82,17 +83,22 @@ def delete(self, annotation_ids: list[str]) -> None:
# > memory.
# >
# > https://elasticsearch-py.readthedocs.io/en/6.8.2/helpers.html#elasticsearch.helpers.bulk

using_es7 = self._using_es7()

def delete_action(annotation_id):
action = {
"_index": self._target_index,
"_id": annotation_id,
"doc": {"deleted": True},
}
if not using_es7: # pragma: no cover
action["_type"] = self.es_client.mapping_type
return action

results = es_helpers.streaming_bulk(
client=self.es_client.conn,
actions=[
{
"_index": self._target_index,
"_type": "annotation",
"_id": annotation_id,
"doc": {"deleted": True},
}
for annotation_id in annotation_ids
],
actions=[delete_action(annotation_id) for annotation_id in annotation_ids],
chunk_size=2500,
raise_on_error=False,
)
Expand All @@ -109,7 +115,7 @@ def _prepare(self, annotation):
"_index": self._target_index,
"_id": annotation.id,
}
if self.es_client.server_version < Version("7.0.0"): # pragma: no cover
if not self._using_es7(): # pragma: no cover
operation["_type"] = self.es_client.mapping_type

data = presenters.AnnotationSearchIndexPresenter(
Expand All @@ -118,6 +124,9 @@ def _prepare(self, annotation):

return {self.op_type: operation}, data

def _using_es7(self):
return self.es_client.server_version >= Version("7.0.0")


def _filtered_annotations(session, ids):
annotations = (
Expand Down

0 comments on commit 3a06584

Please sign in to comment.