def test_get_client_cache(self): """Tests that the OSWrapper.get_client() method caches previous OpenSearch objects and returns them""" opensearch_client = get_open_search() self.assertIs(opensearch_client, OSWrapper._os_client) OSWrapper._os_client = None new_opensearch_client = get_open_search() self.assertFalse(opensearch_client is new_opensearch_client)
class DocumentManager(models.Manager): @staticmethod def open_search( query, case=None, confidential_status=None, organisation=None, user_type=None, **kwargs, # noqa ): case = get_case(case) organisation = get_organisation(organisation) if isinstance(query, dict): _query = query else: _query = { "bool": { "must": [ { "multi_match": { "query": query, "fields": ["name^2", "content"], "type": "phrase_prefix", } } ] } } if case: _query["bool"].setdefault("filter", []) _query["bool"]["filter"].append({"match": {"case_id": case.id}}) if confidential_status is not None: _query["bool"].setdefault("filter", []) _query["bool"]["filter"].append({"term": {"confidential": confidential_status}}) if organisation: _query["bool"].setdefault("filter", []) _query["bool"]["filter"].append( {"match": {"organisation": {"id": str(organisation.id)}}} ) if user_type in ("TRA", "PUB"): _query["bool"].setdefault("filter", []) _query["bool"]["filter"].append({"match": {"user_type": user_type}}) try: client = get_open_search() except OSWrapperError as e: logger.error(e) return None else: search_results = client.search( index=settings.OPENSEARCH_INDEX["document"], doc_type="document", body={"query": _query, "highlight": {"fields": {"content": {}}}}, ) return search_results
def open_search_doc(self): """ Return the OpenSearch document for this record """ try: client = get_open_search() except OSWrapperError as e: logger.error(e) else: try: return client.get( index=settings.OPENSEARCH_INDEX["document"], doc_type="document", id=self.id, ) except NotFoundError: logger.warning("Could not find document in OpenSearch index") return None
def delete_opensearch_document(self): """Delete an OpenSearch representation of this document. Using quite a broad exception handling to prevent any failure that will cause the delete to fail. """ try: client = get_open_search() except OSWrapperError as e: logger.error(e) else: try: result = client.delete( index=settings.OPENSEARCH_INDEX["document"], doc_type="document", id=str(self.id), ) return result.get("result") == "deleted" except NotFoundError as exc: # OpenSearch document not found, probably uploaded before opensearch was activated pass except Exception as exc: logger.error(f"cannot delete OpenSearch document: {self.id} - {exc}") return False
def test_get_open_search(self): """Tests that the get_open_search() function returns a working OpenSearch object""" opensearch_client = get_open_search() self.assertIsInstance(opensearch_client, OpenSearch)
def test_get_open_search_error(self): """Tests that without the correct environment variables, an OSWrapperError() is raised""" with self.assertRaises(OSWrapperError): get_open_search()
try: return client.get( index=settings.OPENSEARCH_INDEX["document"], doc_type="document", id=self.id, ) except NotFoundError: logger.warning("Could not find document in OpenSearch index") return None def open_search_index(self, submission=None, case=None, **kwargs): # noqa """ Create an OpenSearch indexed document for this record """ try: client = get_open_search() except OSWrapperError as e: logger.error(e) return None content, index_state = self.extract_content() case = get_case(case) organisation = None doc = { "id": self.id, "name": self.name, "case_id": case.id if case else None, "file_type": self.file_extension, "all_case_ids": [], "created_at": self.created_at.strftime(settings.API_DATETIME_FORMAT), "created_by": {