|
| 1 | +from elasticsearch import Elasticsearch, helpers, NotFoundError |
| 2 | +from django.conf import settings |
| 3 | + |
| 4 | +import logging |
| 5 | + |
| 6 | + |
| 7 | +def get_elasticsearch_client(url=None, basic_auth=None, api_key=None): |
| 8 | + """ |
| 9 | + Create an Elasticsearch client instance using Django settings. |
| 10 | +
|
| 11 | + :param url: Elasticsearch URL. If None, it will be taken from Django settings. |
| 12 | + :param basic_auth: Basic authentication credentials. If None, it will be taken from Django settings. |
| 13 | + :param api_key: API key. If None, it will be taken from Django settings. |
| 14 | + """ |
| 15 | + if not url: |
| 16 | + url = getattr(settings, "ES_URL", None) |
| 17 | + |
| 18 | + if not basic_auth: |
| 19 | + basic_auth = getattr(settings, "ES_BASIC_AUTH", None) |
| 20 | + |
| 21 | + if not api_key: |
| 22 | + api_key = getattr(settings, "ES_API_KEY", None) |
| 23 | + |
| 24 | + if basic_auth: |
| 25 | + client = Elasticsearch(url, basic_auth=basic_auth) |
| 26 | + elif api_key: |
| 27 | + client = Elasticsearch(url, api_key=api_key) |
| 28 | + else: |
| 29 | + client = Elasticsearch(url) |
| 30 | + |
| 31 | + return client |
| 32 | + |
| 33 | + |
| 34 | +def create_index(index_name, mappings=None, client=None, url=None, basic_auth=None, api_key=None): |
| 35 | + """ |
| 36 | + Create an Elasticsearch index. |
| 37 | +
|
| 38 | + :param index_name: Name of the index to create. |
| 39 | + :param mappings: Mappings for the index. If None, default mappings will be used. |
| 40 | + :param client: Elasticsearch client instance. If None, a new client will be created. |
| 41 | + :param url: Elasticsearch URL. If None, it will be taken from Django settings. |
| 42 | + :param basic_auth: Basic authentication credentials. If None, it will be taken from Django settings. |
| 43 | + :param api_key: API key. If None, it will be taken from Django settings. |
| 44 | + """ |
| 45 | + if not client: |
| 46 | + client = get_elasticsearch_client(url, basic_auth, api_key) |
| 47 | + |
| 48 | + if not mappings: |
| 49 | + mappings = { |
| 50 | + "properties": { |
| 51 | + "collection": { |
| 52 | + "type": "keyword" |
| 53 | + }, |
| 54 | + "journal": { |
| 55 | + "type": "keyword" |
| 56 | + }, |
| 57 | + "pid_v2": { |
| 58 | + "type": "keyword" |
| 59 | + }, |
| 60 | + "pid_v3": { |
| 61 | + "type": "keyword" |
| 62 | + }, |
| 63 | + "pid_generic": { |
| 64 | + "type": "keyword" |
| 65 | + }, |
| 66 | + "media_language": { |
| 67 | + "type": "keyword" |
| 68 | + }, |
| 69 | + "country_code": { |
| 70 | + "type": "keyword" |
| 71 | + }, |
| 72 | + "date": { |
| 73 | + "type": "date", |
| 74 | + "format": "yyyy-MM-dd" |
| 75 | + }, |
| 76 | + "year": { |
| 77 | + "type": "integer" |
| 78 | + }, |
| 79 | + "month": { |
| 80 | + "type": "integer" |
| 81 | + }, |
| 82 | + "day": { |
| 83 | + "type": "integer" |
| 84 | + }, |
| 85 | + "total_requests": { |
| 86 | + "type": "integer" |
| 87 | + }, |
| 88 | + "total_investigations": { |
| 89 | + "type": "integer" |
| 90 | + }, |
| 91 | + "unique_requests": { |
| 92 | + "type": "integer" |
| 93 | + }, |
| 94 | + "unique_investigations": { |
| 95 | + "type": "integer" |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + resp = client.indices.create( |
| 101 | + index=index_name, |
| 102 | + mappings=mappings, |
| 103 | + ) |
| 104 | + logging.info(f"Index {index_name} created: {resp}") |
| 105 | + |
| 106 | + |
| 107 | +def delete_index(index_name, client=None, url=None, basic_auth=None, api_key=None): |
| 108 | + """ |
| 109 | + Delete an Elasticsearch index. |
| 110 | +
|
| 111 | + :param index_name: Name of the index to delete. |
| 112 | + :param client: Elasticsearch client instance. If None, a new client will be created. |
| 113 | + :param url: Elasticsearch URL. If None, it will be taken from Django settings. |
| 114 | + :param basic_auth: Basic authentication credentials. If None, it will be taken from Django settings. |
| 115 | + :param api_key: API key. If None, it will be taken from Django settings. |
| 116 | + """ |
| 117 | + if not client: |
| 118 | + client = get_elasticsearch_client(url, basic_auth, api_key) |
| 119 | + client.indices.delete(index=index_name) |
| 120 | + |
| 121 | + |
| 122 | +def index_document(index_name, doc_id, document, client=None, url=None, basic_auth=None, api_key=None): |
| 123 | + """ |
| 124 | + Index a document in Elasticsearch. |
| 125 | +
|
| 126 | + :param index_name: Name of the index. |
| 127 | + :param doc_id: ID of the document. |
| 128 | + :param document: Document to index. |
| 129 | + :param client: Elasticsearch client instance. If None, a new client will be created. |
| 130 | + :param url: Elasticsearch URL. If None, it will be taken from Django settings. |
| 131 | + :param basic_auth: Basic authentication credentials. If None, it will be taken from Django settings. |
| 132 | + :param api_key: API key. If None, it will be taken from Django settings. |
| 133 | + """ |
| 134 | + if not client: |
| 135 | + client = get_elasticsearch_client(url, basic_auth, api_key) |
| 136 | + client.index(index=index_name, id=doc_id, document=document) |
| 137 | + |
| 138 | + |
| 139 | +def index_documents(index_name, documents, client=None, url=None, basic_auth=None, api_key=None): |
| 140 | + """ |
| 141 | + Index multiple documents in Elasticsearch. |
| 142 | +
|
| 143 | + :param index_name: Name of the index. |
| 144 | + :param documents: Dictionary of documents to index, where keys are document IDs and values are the documents. |
| 145 | + :param client: Elasticsearch client instance. If None, a new client will be created. |
| 146 | + :param url: Elasticsearch URL. If None, it will be taken from Django settings. |
| 147 | + :param basic_auth: Basic authentication credentials. If None, it will be taken from Django settings. |
| 148 | + :param api_key: API key. If None, it will be taken from Django settings. |
| 149 | + """ |
| 150 | + if not client: |
| 151 | + client = get_elasticsearch_client(url, basic_auth, api_key) |
| 152 | + |
| 153 | + helpers.bulk( |
| 154 | + client, |
| 155 | + ( |
| 156 | + { |
| 157 | + "_index": index_name, |
| 158 | + "_id": doc_id, |
| 159 | + "_source": document, |
| 160 | + } |
| 161 | + for doc_id, document in documents.items() |
| 162 | + ), |
| 163 | + ) |
| 164 | + |
| 165 | + |
| 166 | +def delete_document(index_name, doc_id, client=None, url=None, basic_auth=None, api_key=None): |
| 167 | + """ |
| 168 | + Delete a document from Elasticsearch. |
| 169 | +
|
| 170 | + :param index_name: Name of the index. |
| 171 | + :param doc_id: ID of the document to delete. |
| 172 | + :param client: Elasticsearch client instance. If None, a new client will be created. |
| 173 | + :param url: Elasticsearch URL. If None, it will be taken from Django settings. |
| 174 | + :param basic_auth: Basic authentication credentials. If None, it will be taken from Django settings. |
| 175 | + :param api_key: API key. If None, it will be taken from Django settings. |
| 176 | + """ |
| 177 | + if not client: |
| 178 | + client = get_elasticsearch_client(url, basic_auth, api_key) |
| 179 | + |
| 180 | + try: |
| 181 | + client.delete(index=index_name, id=doc_id) |
| 182 | + except NotFoundError as e: |
| 183 | + logging.error(f"Failed to delete document {doc_id} from Elasticsearch: {e}") |
| 184 | + |
| 185 | + |
| 186 | +def delete_documents(index_name, doc_ids, client=None, url=None, basic_auth=None, api_key=None): |
| 187 | + """ |
| 188 | + Delete multiple documents from Elasticsearch using bulk. |
| 189 | + :param index_name: Name of the index. |
| 190 | + :param doc_ids: List of document IDs to delete. |
| 191 | + :param client: Elasticsearch client instance. If None, a new client will be created. |
| 192 | + :param url: Elasticsearch URL. If None, it will be taken from Django settings. |
| 193 | + :param basic_auth: Basic authentication credentials. If None, it will be taken from Django settings. |
| 194 | + :param api_key: API key. If None, it will be taken from Django settings. |
| 195 | + """ |
| 196 | + if not client: |
| 197 | + client = get_elasticsearch_client(url, basic_auth, api_key) |
| 198 | + |
| 199 | + actions = ( |
| 200 | + { |
| 201 | + "_op_type": "delete", |
| 202 | + "_index": index_name, |
| 203 | + "_id": doc_id, |
| 204 | + } |
| 205 | + for doc_id in doc_ids |
| 206 | + ) |
| 207 | + |
| 208 | + try: |
| 209 | + helpers.bulk(client, actions) |
| 210 | + except helpers.BulkIndexError as e: |
| 211 | + logging.error(f"BulkIndexError occurred: {e.errors}") |
| 212 | + |
| 213 | + |
| 214 | +def delete_documents_by_key(index_name, key, values, client=None, url=None, basic_auth=None, api_key=None): |
| 215 | + """ |
| 216 | + Delete multiple documents from Elasticsearch based on a specific key and its values. |
| 217 | +
|
| 218 | + :param index_name: Name of the index. |
| 219 | + :param key: Key to search for in the documents. |
| 220 | + :param values: List of values to match against the key. |
| 221 | + :param client: Elasticsearch client instance. If None, a new client will be created. |
| 222 | + :param url: Elasticsearch URL. If None, it will be taken from Django settings. |
| 223 | + :param basic_auth: Basic authentication credentials. If None, it will be taken from Django settings. |
| 224 | + :param api_key: API key. If None, it will be taken from Django settings. |
| 225 | + """ |
| 226 | + if not client: |
| 227 | + client = get_elasticsearch_client(url, basic_auth, api_key) |
| 228 | + |
| 229 | + query = { |
| 230 | + "query": { |
| 231 | + "terms": { |
| 232 | + key: values |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + try: |
| 238 | + client.delete_by_query(index=index_name, body=query) |
| 239 | + except Exception as e: |
| 240 | + logging.error(f"Failed to delete documents by key {key} with values {values}: {e}") |
0 commit comments