rara-tools 0.0.4__tar.gz → 0.0.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of rara-tools might be problematic. Click here for more details.
- {rara_tools-0.0.4/rara_tools.egg-info → rara_tools-0.0.8}/PKG-INFO +1 -1
- rara_tools-0.0.8/VERSION +1 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/pyproject.toml +1 -0
- rara_tools-0.0.8/rara_tools/constants/__init__.py +0 -0
- rara_tools-0.0.8/rara_tools/constants/digitizer.py +13 -0
- rara_tools-0.0.8/rara_tools/constants/general.py +10 -0
- rara_tools-0.0.8/rara_tools/elastic.py +175 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8/rara_tools.egg-info}/PKG-INFO +1 -1
- {rara_tools-0.0.4 → rara_tools-0.0.8}/rara_tools.egg-info/SOURCES.txt +3 -0
- rara_tools-0.0.8/tests/test_elastic.py +132 -0
- rara_tools-0.0.4/VERSION +0 -1
- rara_tools-0.0.4/rara_tools/elastic.py +0 -92
- rara_tools-0.0.4/tests/test_elastic.py +0 -69
- {rara_tools-0.0.4 → rara_tools-0.0.8}/LICENSE.md +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/README.md +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/rara_tools/decorators.py +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/rara_tools/exceptions.py +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/rara_tools/s3.py +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/rara_tools/task_reporter.py +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/rara_tools.egg-info/dependency_links.txt +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/rara_tools.egg-info/requires.txt +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/rara_tools.egg-info/top_level.txt +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/requirements.txt +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/setup.cfg +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/tests/test_s3_exceptions.py +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/tests/test_s3_file_operations.py +0 -0
- {rara_tools-0.0.4 → rara_tools-0.0.8}/tests/test_task_reporter.py +0 -0
rara_tools-0.0.8/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.0.8
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class StatusKeys:
|
|
2
|
+
CLEAN_UP = "digitizer_clean_up"
|
|
3
|
+
ELASTICSEARCH_UPLOAD = "digitizer_elasticsearch_upload"
|
|
4
|
+
UPLOAD = "s3_upload"
|
|
5
|
+
DOWNLOAD = "digitizer_s3_download"
|
|
6
|
+
OCR = "digitizer_ocr"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Queue:
|
|
10
|
+
IO = "io"
|
|
11
|
+
DOWNLOAD = "download"
|
|
12
|
+
FINISH = "finish"
|
|
13
|
+
OCR = "ocr"
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
from typing import Dict, Optional, Any, Iterator
|
|
2
|
+
|
|
3
|
+
import elasticsearch_dsl
|
|
4
|
+
from elastic_transport import ObjectApiResponse
|
|
5
|
+
from elasticsearch import Elasticsearch
|
|
6
|
+
from elasticsearch.helpers import bulk
|
|
7
|
+
from elasticsearch_dsl import Index
|
|
8
|
+
|
|
9
|
+
from .decorators import _elastic_connection
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class KataElastic:
|
|
13
|
+
"""A class to manage all required Elasticsearch operations for Kata.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
TYPE_MAPPING = {
|
|
17
|
+
"keyword": elasticsearch_dsl.Keyword,
|
|
18
|
+
"text": elasticsearch_dsl.Text,
|
|
19
|
+
"float": elasticsearch_dsl.Float,
|
|
20
|
+
"integer": elasticsearch_dsl.Integer,
|
|
21
|
+
"date": elasticsearch_dsl.Date,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
DEFAULT_MAPPING = {
|
|
25
|
+
"text": "keyword",
|
|
26
|
+
"parent_id": "keyword",
|
|
27
|
+
"text_quality": "float",
|
|
28
|
+
"n_chars": "integer",
|
|
29
|
+
"n_words": "integer",
|
|
30
|
+
"language": "keyword",
|
|
31
|
+
"end_page": "integer",
|
|
32
|
+
"start_page": "integer",
|
|
33
|
+
"sequence_nr": "integer",
|
|
34
|
+
"section_title": "keyword",
|
|
35
|
+
"section_type": "keyword",
|
|
36
|
+
"section_meta": "keyword",
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
def __init__(self, elasticsearch_url: str, timeout: Optional[int] = None):
|
|
40
|
+
self.timeout = timeout
|
|
41
|
+
self.elasticsearch_url = elasticsearch_url
|
|
42
|
+
self.elasticsearch = Elasticsearch(self.elasticsearch_url, request_timeout=self.timeout)
|
|
43
|
+
|
|
44
|
+
def _produce_rollover_index(self, index_prefix: str, rollover_limit: int) -> str:
|
|
45
|
+
indices = self.elasticsearch.indices.get(index=f"{index_prefix}-*", expand_wildcards="open")
|
|
46
|
+
sorted_indices = sorted([(k, v["settings"]["index"]["creation_date"]) for k, v in indices.items()], key=lambda x: x[1], reverse=True)
|
|
47
|
+
sorted_indices = [i[0] for i in sorted_indices]
|
|
48
|
+
|
|
49
|
+
# new index name if none exist
|
|
50
|
+
if not len(sorted_indices):
|
|
51
|
+
last_index_name = f"{index_prefix}-0"
|
|
52
|
+
last_index_count = 0
|
|
53
|
+
else:
|
|
54
|
+
last_index_name = sorted_indices[0]
|
|
55
|
+
last_index_count = self.elasticsearch.count(index=last_index_name)["count"]
|
|
56
|
+
# check the size of the last index of the pipeline
|
|
57
|
+
if last_index_count >= rollover_limit:
|
|
58
|
+
new_index_number = int(last_index_name[-1]) + 1
|
|
59
|
+
last_index_name = f"{index_prefix}-{new_index_number}"
|
|
60
|
+
|
|
61
|
+
return last_index_name
|
|
62
|
+
|
|
63
|
+
@_elastic_connection
|
|
64
|
+
def check(self) -> bool:
|
|
65
|
+
"""Checks Elasticsearch connection.
|
|
66
|
+
:return: bool: Elasticsearch alive or dead.
|
|
67
|
+
"""
|
|
68
|
+
if self.elasticsearch.ping():
|
|
69
|
+
return True
|
|
70
|
+
return False
|
|
71
|
+
|
|
72
|
+
def generate_mapping(self, schema: dict | None = None) -> dict:
|
|
73
|
+
mapping_dsl = elasticsearch_dsl.Mapping()
|
|
74
|
+
mapping = schema or self.DEFAULT_MAPPING
|
|
75
|
+
for field_name, field_type in mapping.items():
|
|
76
|
+
if field_type in self.TYPE_MAPPING:
|
|
77
|
+
# We instantiate the class stored in the type mapping.
|
|
78
|
+
mapping_dsl.field(field_name, self.TYPE_MAPPING[field_type]())
|
|
79
|
+
return mapping_dsl.to_dict()
|
|
80
|
+
|
|
81
|
+
@_elastic_connection
|
|
82
|
+
def add_mapping(self, index_name: str, schema: dict):
|
|
83
|
+
index = Index(name=index_name)
|
|
84
|
+
return index.put_mapping(body=schema, using=self.elasticsearch)
|
|
85
|
+
|
|
86
|
+
@_elastic_connection
|
|
87
|
+
def create_index(
|
|
88
|
+
self,
|
|
89
|
+
index: str,
|
|
90
|
+
shards: int = 3,
|
|
91
|
+
replicas: int = 1,
|
|
92
|
+
settings: Optional[dict] = None,
|
|
93
|
+
) -> Dict | None:
|
|
94
|
+
"""Creates empty index.
|
|
95
|
+
:param: index str: Name of the index to create.
|
|
96
|
+
:param: shards int: Number of shards for the index.
|
|
97
|
+
:param: replicas int: Number of replicas of the index.
|
|
98
|
+
:param: settings dict: Overwrite settings for the index.
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
index_exists = self.elasticsearch.indices.exists(index=index).body
|
|
102
|
+
if index_exists is False:
|
|
103
|
+
setting_body = settings or {
|
|
104
|
+
"number_of_shards": shards,
|
|
105
|
+
"number_of_replicas": replicas,
|
|
106
|
+
}
|
|
107
|
+
return self.elasticsearch.indices.create(index=index, settings=setting_body)
|
|
108
|
+
|
|
109
|
+
@_elastic_connection
|
|
110
|
+
def delete_index(self, index: str, ignore: Optional[bool] = True) -> Dict:
|
|
111
|
+
"""Deletes index.
|
|
112
|
+
:param: index str: Name of the index to be deleted.
|
|
113
|
+
:param: ignore bool: Ignore errors because of closed/deleted index.
|
|
114
|
+
:return: Dict of Elastic's acknowledgement of the action.
|
|
115
|
+
"""
|
|
116
|
+
response = self.elasticsearch.indices.delete(index=index, ignore_unavailable=ignore, expand_wildcards="open")
|
|
117
|
+
return response
|
|
118
|
+
|
|
119
|
+
@_elastic_connection
|
|
120
|
+
def delete_document(self, index: str, document_id: str) -> ObjectApiResponse[Any]:
|
|
121
|
+
"""Deletes document fom index.
|
|
122
|
+
:param: document_id str: ID of the document to be deleted.
|
|
123
|
+
:param: index str: Index where the document is to be found.
|
|
124
|
+
:param: ignore bool: Ignore errors because of closed/deleted index.
|
|
125
|
+
:return: Dict of Elastic's acknowledgement of the action.
|
|
126
|
+
"""
|
|
127
|
+
response = self.elasticsearch.delete(id=document_id, index=index)
|
|
128
|
+
return response
|
|
129
|
+
|
|
130
|
+
@_elastic_connection
|
|
131
|
+
def bulk_index(
|
|
132
|
+
self,
|
|
133
|
+
documents: Iterator[dict],
|
|
134
|
+
index_prefix: str,
|
|
135
|
+
rollover_limit: int,
|
|
136
|
+
refresh="false",
|
|
137
|
+
create_index: bool = True
|
|
138
|
+
) -> (int, int):
|
|
139
|
+
last_index_name = self._produce_rollover_index(index_prefix, rollover_limit)
|
|
140
|
+
if create_index:
|
|
141
|
+
response = self.create_index(index=last_index_name)
|
|
142
|
+
response = self.add_mapping(index_name=last_index_name, schema=self.generate_mapping())
|
|
143
|
+
pass
|
|
144
|
+
|
|
145
|
+
actions = [{"_index": last_index_name, "_source": document} for document in documents]
|
|
146
|
+
successful_count, error_count = bulk(actions=actions, client=self.elasticsearch, max_retries=3, refresh=refresh)
|
|
147
|
+
return successful_count, error_count
|
|
148
|
+
|
|
149
|
+
@_elastic_connection
|
|
150
|
+
def index_document(self, index: str, body: dict, document_id: Optional[str] = None) -> Dict:
|
|
151
|
+
"""Indexes document.
|
|
152
|
+
:param: index str: Index that document will be indexed into.
|
|
153
|
+
:param: body dict: Document body.
|
|
154
|
+
:param: document_id str: Optional id for the document. Is generated automatically if None.
|
|
155
|
+
:return: Dict of Elastic's acknowledgement of the action.
|
|
156
|
+
"""
|
|
157
|
+
if document_id:
|
|
158
|
+
indexed = self.elasticsearch.index(index=index, id=document_id, body=body)
|
|
159
|
+
else:
|
|
160
|
+
indexed = self.elasticsearch.index(index=index, body=body)
|
|
161
|
+
return indexed
|
|
162
|
+
|
|
163
|
+
@_elastic_connection
|
|
164
|
+
def get_documents_by_key(self, index: str, document_key: str, sort_fields=("start_page", "end_page", "sequence_nr",)):
|
|
165
|
+
index = f"{index}-*"
|
|
166
|
+
s = elasticsearch_dsl.Search(using=self.elasticsearch, index=index)
|
|
167
|
+
s = s.query("match", parent_id=document_key).sort(*sort_fields)
|
|
168
|
+
# Since scan doesn't allow for sorting, we do it manually after fetching the documents.
|
|
169
|
+
documents = sorted(
|
|
170
|
+
s.scan(), key=lambda doc: [getattr(doc, field) for field in sort_fields]
|
|
171
|
+
)
|
|
172
|
+
return documents
|
|
173
|
+
|
|
174
|
+
def __str__(self) -> str:
|
|
175
|
+
return self.elasticsearch_url
|
|
@@ -13,6 +13,9 @@ rara_tools.egg-info/SOURCES.txt
|
|
|
13
13
|
rara_tools.egg-info/dependency_links.txt
|
|
14
14
|
rara_tools.egg-info/requires.txt
|
|
15
15
|
rara_tools.egg-info/top_level.txt
|
|
16
|
+
rara_tools/constants/__init__.py
|
|
17
|
+
rara_tools/constants/digitizer.py
|
|
18
|
+
rara_tools/constants/general.py
|
|
16
19
|
tests/test_elastic.py
|
|
17
20
|
tests/test_s3_exceptions.py
|
|
18
21
|
tests/test_s3_file_operations.py
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import time
|
|
4
|
+
import uuid
|
|
5
|
+
from time import sleep
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from rara_tools.elastic import KataElastic
|
|
10
|
+
|
|
11
|
+
with open("./tests/test_data/elastic_docs.json") as fh:
|
|
12
|
+
TEST_DOCUMENTS = json.load(fh)
|
|
13
|
+
|
|
14
|
+
es_url = os.getenv("ELASTIC_TEST_URL", "http://localhost:9200")
|
|
15
|
+
ELASTIC = KataElastic(es_url)
|
|
16
|
+
ELASTIC_BAD = KataElastic("http://locallost:9012")
|
|
17
|
+
TEST_INDEX_NAME = "tools_testing_index"
|
|
18
|
+
TEST_DOCUMENT_ID = None
|
|
19
|
+
TEST_DOCUMENT_INDEX = None
|
|
20
|
+
PARENT_ID = uuid.uuid4().hex
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@pytest.mark.order(1)
|
|
24
|
+
def test_index_creation():
|
|
25
|
+
""" Tests if index created and documents indexed.
|
|
26
|
+
"""
|
|
27
|
+
# Create test index
|
|
28
|
+
created = ELASTIC.create_index(TEST_INDEX_NAME)
|
|
29
|
+
assert created["acknowledged"] is True
|
|
30
|
+
time.sleep(2)
|
|
31
|
+
|
|
32
|
+
@pytest.mark.order(2)
|
|
33
|
+
def test_check():
|
|
34
|
+
"""Tests health check method.
|
|
35
|
+
"""
|
|
36
|
+
assert ELASTIC.check() is True
|
|
37
|
+
# test bad connection
|
|
38
|
+
assert ELASTIC_BAD.check() is False
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@pytest.mark.order(2)
|
|
42
|
+
def test_creating_index_again():
|
|
43
|
+
"""
|
|
44
|
+
Test to see that running the function for index generation doesn't trigger errors
|
|
45
|
+
on duplicates.
|
|
46
|
+
"""
|
|
47
|
+
# Create test index
|
|
48
|
+
created = ELASTIC.create_index(TEST_INDEX_NAME)
|
|
49
|
+
assert created is None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@pytest.mark.order(3)
|
|
53
|
+
def test_adding_mapping_to_index():
|
|
54
|
+
"""Test adding mapping to an index"""
|
|
55
|
+
schema = ELASTIC.generate_mapping()
|
|
56
|
+
result = ELASTIC.add_mapping(TEST_INDEX_NAME, schema)
|
|
57
|
+
assert result["acknowledged"] is True
|
|
58
|
+
# Test adding the mapping again doesn't create errors.
|
|
59
|
+
result = ELASTIC.add_mapping(TEST_INDEX_NAME, schema)
|
|
60
|
+
assert result["acknowledged"] is True
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@pytest.mark.order(4)
|
|
64
|
+
def test_document_addition():
|
|
65
|
+
# Add test documents
|
|
66
|
+
for document in TEST_DOCUMENTS:
|
|
67
|
+
indexed = ELASTIC.index_document(TEST_INDEX_NAME, document)
|
|
68
|
+
assert indexed["result"] == "created"
|
|
69
|
+
# let it index
|
|
70
|
+
sleep(1)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@pytest.mark.order(5)
|
|
74
|
+
def test_bulk_indexing_documents_cause_rollover():
|
|
75
|
+
data = [{"start_page": number, "sequence_nr": 1, "end_page": number, "parent_id": PARENT_ID} for number in range(10)]
|
|
76
|
+
chunks = [data[i:i + 3] for i in range(0, len(data), 3)]
|
|
77
|
+
for chunk in chunks:
|
|
78
|
+
success, errors = ELASTIC.bulk_index(chunk, TEST_INDEX_NAME, rollover_limit=3, refresh="wait_for")
|
|
79
|
+
assert success is 3 or success is 1
|
|
80
|
+
|
|
81
|
+
created_indices = ELASTIC.elasticsearch.indices.get(index=f"{TEST_INDEX_NAME}-*", expand_wildcards="open").body
|
|
82
|
+
assert len(created_indices) == 4
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@pytest.mark.order(6)
|
|
86
|
+
def test_bulk_indexing_and_document_fetch():
|
|
87
|
+
"""
|
|
88
|
+
Test that the whole process of indexing a bunch of different texts and then the retrieval
|
|
89
|
+
of only the requested documents works as intended.
|
|
90
|
+
"""
|
|
91
|
+
success, errors = ELASTIC.bulk_index(TEST_DOCUMENTS, TEST_INDEX_NAME, rollover_limit=3, refresh="wait_for")
|
|
92
|
+
|
|
93
|
+
# Test the integrity of the limiting query.
|
|
94
|
+
result = ELASTIC.get_documents_by_key(TEST_INDEX_NAME, "foo")
|
|
95
|
+
assert len(result) == 2
|
|
96
|
+
result = ELASTIC.get_documents_by_key(TEST_INDEX_NAME, "bar")
|
|
97
|
+
global TEST_DOCUMENT_ID
|
|
98
|
+
global TEST_DOCUMENT_INDEX
|
|
99
|
+
TEST_DOCUMENT_ID = result[0].meta.id
|
|
100
|
+
TEST_DOCUMENT_INDEX = result[0].meta.index
|
|
101
|
+
assert len(result) == 1
|
|
102
|
+
result = ELASTIC.get_documents_by_key(TEST_INDEX_NAME, "loll")
|
|
103
|
+
assert len(result) == 0
|
|
104
|
+
|
|
105
|
+
# Check that sorting works as expected.
|
|
106
|
+
results = ELASTIC.get_documents_by_key(TEST_INDEX_NAME, PARENT_ID)
|
|
107
|
+
for index, document in enumerate(results):
|
|
108
|
+
assert document.start_page == index
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@pytest.mark.order(7)
|
|
112
|
+
def test_document_deleting():
|
|
113
|
+
"""
|
|
114
|
+
Tests deleting a document from index.
|
|
115
|
+
"""
|
|
116
|
+
deleted = ELASTIC.delete_document(TEST_DOCUMENT_INDEX, TEST_DOCUMENT_ID)
|
|
117
|
+
assert deleted["result"] == "deleted"
|
|
118
|
+
sleep(1)
|
|
119
|
+
# check if document was actually deleted
|
|
120
|
+
result = ELASTIC.get_documents_by_key(TEST_INDEX_NAME, "bar")
|
|
121
|
+
assert len(result) == 0
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@pytest.mark.order(8)
|
|
125
|
+
def test_index_deleting():
|
|
126
|
+
"""
|
|
127
|
+
Tests deleting index. We delete the test index now.
|
|
128
|
+
"""
|
|
129
|
+
deleted = ELASTIC.delete_index(TEST_INDEX_NAME)
|
|
130
|
+
for i in range(10):
|
|
131
|
+
ELASTIC.delete_index(f"{TEST_INDEX_NAME}-{i}")
|
|
132
|
+
assert deleted["acknowledged"] is True
|
rara_tools-0.0.4/VERSION
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
0.0.4
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
from typing import Dict, Optional, List
|
|
2
|
-
from elasticsearch import Elasticsearch
|
|
3
|
-
from elasticsearch_dsl import Search
|
|
4
|
-
|
|
5
|
-
from .decorators import _elastic_connection
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class KataElastic:
|
|
9
|
-
"""A class to manage all required Elasticsearch operations for Kata.
|
|
10
|
-
"""
|
|
11
|
-
def __init__(self, elasticsearch_url: str, timeout: Optional[int] = None):
|
|
12
|
-
self.timeout = timeout
|
|
13
|
-
self.elasticsearch_url = elasticsearch_url
|
|
14
|
-
self.elasticsearch = Elasticsearch(self.elasticsearch_url, request_timeout=self.timeout)
|
|
15
|
-
|
|
16
|
-
@_elastic_connection
|
|
17
|
-
def check(self) -> bool:
|
|
18
|
-
"""Checks Elasticsearch connection.
|
|
19
|
-
:return: bool: Elasticsearch alive or dead.
|
|
20
|
-
"""
|
|
21
|
-
if self.elasticsearch.ping():
|
|
22
|
-
return True
|
|
23
|
-
return False
|
|
24
|
-
|
|
25
|
-
@_elastic_connection
|
|
26
|
-
def create_index(
|
|
27
|
-
self,
|
|
28
|
-
index: str,
|
|
29
|
-
shards: int = 3,
|
|
30
|
-
replicas: int = 1,
|
|
31
|
-
settings: Optional[dict] = None
|
|
32
|
-
) -> Dict:
|
|
33
|
-
"""Creates empty index.
|
|
34
|
-
:param: index str: Name of the index to create.
|
|
35
|
-
:param: shards int: Number of shards for the index.
|
|
36
|
-
:param: replicas int: Number of replicas of the index.
|
|
37
|
-
:param: settings dict: Overwrite settings for the index.
|
|
38
|
-
"""
|
|
39
|
-
body = settings or {
|
|
40
|
-
"number_of_shards": shards,
|
|
41
|
-
"number_of_replicas": replicas,
|
|
42
|
-
}
|
|
43
|
-
return self.elasticsearch.indices.create(index=index, settings=body)
|
|
44
|
-
|
|
45
|
-
@_elastic_connection
|
|
46
|
-
def delete_index(self, index: str, ignore: Optional[bool] = True) -> Dict:
|
|
47
|
-
"""Deletes index.
|
|
48
|
-
:param: index str: Name of the index to be deleted.
|
|
49
|
-
:param: ignore bool: Ignore errors because of closed/deleted index.
|
|
50
|
-
:return: Dict of Elastic's acknowledgement of the action.
|
|
51
|
-
"""
|
|
52
|
-
response = self.elasticsearch.indices.delete(index=index, ignore_unavailable=ignore)
|
|
53
|
-
return response
|
|
54
|
-
|
|
55
|
-
@_elastic_connection
|
|
56
|
-
def delete_document(self, index: str, document_id: str) -> Dict:
|
|
57
|
-
"""Deletes document fom index.
|
|
58
|
-
:param: document_id str: ID of the document to be deleted.
|
|
59
|
-
:param: index str: Index where the document is to be found.
|
|
60
|
-
:param: ignore bool: Ignore errors because of closed/deleted index.
|
|
61
|
-
:return: Dict of Elastic's acknowledgement of the action.
|
|
62
|
-
"""
|
|
63
|
-
response = self.elasticsearch.delete(id=document_id, index=index)
|
|
64
|
-
return response
|
|
65
|
-
|
|
66
|
-
@_elastic_connection
|
|
67
|
-
def index_document(self, index: str, body: dict, document_id: Optional[str] = None) -> Dict:
|
|
68
|
-
"""Indexes document.
|
|
69
|
-
:param: index str: Index that document will be indexed into.
|
|
70
|
-
:param: body dict: Document body.
|
|
71
|
-
:param: document_id str: Optional id for the document. Is generated automatically if None.
|
|
72
|
-
:return: Dict of Elastic's acknowledgement of the action.
|
|
73
|
-
"""
|
|
74
|
-
if document_id:
|
|
75
|
-
indexed = self.elasticsearch.index(index=index, id=document_id, body=body)
|
|
76
|
-
else:
|
|
77
|
-
indexed = self.elasticsearch.index(index=index, body=body)
|
|
78
|
-
return indexed
|
|
79
|
-
|
|
80
|
-
@_elastic_connection
|
|
81
|
-
def get_documents_by_key(self, index: str, document_key: str) -> List:
|
|
82
|
-
"""This method is for retrieving all texts/pages of the original document.
|
|
83
|
-
:param: index str: Index to search the documents from.
|
|
84
|
-
:param: document_key str: parent_id field that connects pages of document together.
|
|
85
|
-
:return: List of matching documents.
|
|
86
|
-
"""
|
|
87
|
-
s = Search(using=self.elasticsearch, index=index)
|
|
88
|
-
docs = s.query("match", parent_id=document_key).execute()
|
|
89
|
-
return docs
|
|
90
|
-
|
|
91
|
-
def __str__(self) -> str:
|
|
92
|
-
return self.elasticsearch_url
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import pytest
|
|
2
|
-
import json
|
|
3
|
-
import os
|
|
4
|
-
from time import sleep
|
|
5
|
-
|
|
6
|
-
from rara_tools.elastic import KataElastic
|
|
7
|
-
|
|
8
|
-
with open("./tests/test_data/elastic_docs.json") as fh:
|
|
9
|
-
TEST_DOCUMENTS = json.load(fh)
|
|
10
|
-
|
|
11
|
-
es_url = os.getenv("ELASTIC_TEST_URL", "http://localhost:9200")
|
|
12
|
-
ELASTIC = KataElastic(es_url)
|
|
13
|
-
ELASTIC_BAD = KataElastic("http://locallost:9012")
|
|
14
|
-
TEST_INDEX_NAME = "tools_testing_index"
|
|
15
|
-
TEST_DOCUMENT_ID = None
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
@pytest.mark.order(1)
|
|
19
|
-
def test_index_creation_and_data_indexing():
|
|
20
|
-
""" Tests if index created and documents indexed.
|
|
21
|
-
"""
|
|
22
|
-
# Create test index
|
|
23
|
-
created = ELASTIC.create_index(TEST_INDEX_NAME)
|
|
24
|
-
assert created["acknowledged"] is True
|
|
25
|
-
# Add test documents
|
|
26
|
-
for document in TEST_DOCUMENTS:
|
|
27
|
-
indexed = ELASTIC.index_document(TEST_INDEX_NAME, document)
|
|
28
|
-
assert indexed["result"] == "created"
|
|
29
|
-
# let it index
|
|
30
|
-
sleep(1)
|
|
31
|
-
|
|
32
|
-
@pytest.mark.order(2)
|
|
33
|
-
def test_check():
|
|
34
|
-
"""Tests health check method.
|
|
35
|
-
"""
|
|
36
|
-
assert ELASTIC.check() is True
|
|
37
|
-
# test bad connection
|
|
38
|
-
assert ELASTIC_BAD.check() is False
|
|
39
|
-
|
|
40
|
-
@pytest.mark.order(3)
|
|
41
|
-
def test_get_document_by_key():
|
|
42
|
-
"""Tests if correct documents fetched.
|
|
43
|
-
"""
|
|
44
|
-
result = ELASTIC.get_documents_by_key(TEST_INDEX_NAME, "foo")
|
|
45
|
-
assert len(result) == 2
|
|
46
|
-
result = ELASTIC.get_documents_by_key(TEST_INDEX_NAME, "bar")
|
|
47
|
-
global TEST_DOCUMENT_ID
|
|
48
|
-
TEST_DOCUMENT_ID = result[0].meta.id
|
|
49
|
-
assert len(result) == 1
|
|
50
|
-
result = ELASTIC.get_documents_by_key(TEST_INDEX_NAME, "loll")
|
|
51
|
-
assert len(result) == 0
|
|
52
|
-
|
|
53
|
-
@pytest.mark.order(5)
|
|
54
|
-
def test_document_deleting():
|
|
55
|
-
""" Tests deleting a document from index.
|
|
56
|
-
"""
|
|
57
|
-
deleted = ELASTIC.delete_document(TEST_INDEX_NAME, TEST_DOCUMENT_ID)
|
|
58
|
-
assert deleted["result"] == "deleted"
|
|
59
|
-
sleep(1)
|
|
60
|
-
# check if document was actually deleted
|
|
61
|
-
result = ELASTIC.get_documents_by_key(TEST_INDEX_NAME, "bar")
|
|
62
|
-
assert len(result) == 0
|
|
63
|
-
|
|
64
|
-
@pytest.mark.order(5)
|
|
65
|
-
def test_index_deleting():
|
|
66
|
-
""" Tests deleting index. We delete the test index now.
|
|
67
|
-
"""
|
|
68
|
-
deleted = ELASTIC.delete_index(TEST_INDEX_NAME)
|
|
69
|
-
assert deleted["acknowledged"] is True
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|