Skip to content

Commit 177edf7

Browse files
authored
(fix) Minor - KeyError in wr.opensearch.seach && cleanup tests (#1879)
* Fix KeyError in wr.opensearch.seach && cleanup tests * Fix flake8
1 parent 7da7214 commit 177edf7

File tree

2 files changed

+9
-28
lines changed

2 files changed

+9
-28
lines changed

awswrangler/opensearch/_read.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _hit_to_row(hit: Mapping[str, Any]) -> Mapping[str, Any]:
3333

3434

3535
def _search_response_to_documents(response: Mapping[str, Any]) -> List[Mapping[str, Any]]:
36-
return [_hit_to_row(hit) for hit in response["hits"]["hits"]]
36+
return [_hit_to_row(hit) for hit in response.get("hits", {}).get("hits", [])]
3737

3838

3939
def _search_response_to_df(response: Union[Mapping[str, Any], Any]) -> pd.DataFrame:

tests/test_opensearch.py

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ def test_delete_index(client):
201201
index = "test_delete_index"
202202
wr.opensearch.create_index(client, index=index)
203203
response = wr.opensearch.delete_index(client, index=index)
204-
print(response)
205204
assert response.get("acknowledged", False) is True
206205

207206

@@ -211,7 +210,6 @@ def test_index_df(client):
211210
df=pd.DataFrame([{"_id": "1", "name": "John"}, {"_id": "2", "name": "George"}, {"_id": "3", "name": "Julia"}]),
212211
index="test_index_df1",
213212
)
214-
print(response)
215213
assert response.get("success", 0) == 3
216214

217215

@@ -223,7 +221,6 @@ def test_index_df_with_array(client):
223221
),
224222
index="test_index_df1",
225223
)
226-
print(response)
227224
assert response.get("success", 0) == 2
228225

229226

@@ -233,22 +230,17 @@ def test_index_documents(client):
233230
documents=[{"_id": "1", "name": "John"}, {"_id": "2", "name": "George"}, {"_id": "3", "name": "Julia"}],
234231
index="test_index_documents1",
235232
)
236-
print(response)
237233
assert response.get("success", 0) == 3
238234

239235

240236
def test_index_documents_id_keys(client):
241-
response = wr.opensearch.index_documents(
237+
wr.opensearch.index_documents(
242238
client, documents=inspections_documents, index="test_index_documents_id_keys", id_keys=["inspection_id"]
243239
)
244-
print(response)
245240

246241

247242
def test_index_documents_no_id_keys(client):
248-
response = wr.opensearch.index_documents(
249-
client, documents=inspections_documents, index="test_index_documents_no_id_keys"
250-
)
251-
print(response)
243+
wr.opensearch.index_documents(client, documents=inspections_documents, index="test_index_documents_no_id_keys")
252244

253245

254246
def test_search(client):
@@ -262,10 +254,13 @@ def test_search(client):
262254
search_body={"query": {"match": {"business_name": "soup"}}},
263255
_source=["inspection_id", "business_name", "business_location"],
264256
)
265-
266-
print("")
267-
print(df.to_string())
268257
assert df.shape[0] == 3
258+
df = wr.opensearch.search(
259+
client,
260+
index=index,
261+
search_body={"query": {"match": {"business_name": "message"}}},
262+
)
263+
assert df.shape == (0, 0)
269264

270265

271266
def test_search_filter_path(client):
@@ -280,9 +275,6 @@ def test_search_filter_path(client):
280275
_source=["inspection_id", "business_name", "business_location"],
281276
filter_path=["hits.hits._source"],
282277
)
283-
284-
print("")
285-
print(df.to_string())
286278
assert df.shape[0] == 3
287279

288280

@@ -294,9 +286,6 @@ def test_search_scroll(client):
294286
df = wr.opensearch.search(
295287
client, index=index, is_scroll=True, _source=["inspection_id", "business_name", "business_location"]
296288
)
297-
298-
print("")
299-
print(df.to_string())
300289
assert df.shape[0] == 5
301290

302291

@@ -306,9 +295,6 @@ def test_search_sql(client):
306295
client, documents=inspections_documents, index=index, id_keys=["inspection_id"], refresh="wait_for"
307296
)
308297
df = wr.opensearch.search_by_sql(client, sql_query=f"select * from {index}")
309-
310-
print("")
311-
print(df.to_string())
312298
assert df.shape[0] == 5
313299

314300

@@ -318,7 +304,6 @@ def test_index_json_local(client):
318304
for doc in inspections_documents:
319305
filehandle.write("%s\n" % json.dumps(doc))
320306
response = wr.opensearch.index_json(client, index="test_index_json_local", path=file_path)
321-
print(response)
322307
assert response.get("success", 0) == 6
323308

324309

@@ -332,7 +317,6 @@ def test_index_json_s3(client, path):
332317
bucket, key = wr._utils.parse_path(path)
333318
s3.upload_file(file_path, bucket, key)
334319
response = wr.opensearch.index_json(client, index="test_index_json_s3", path=path)
335-
print(response)
336320
assert response.get("success", 0) == 6
337321

338322

@@ -342,7 +326,6 @@ def test_index_csv_local(client):
342326
df = pd.DataFrame(inspections_documents)
343327
df.to_csv(file_path, index=False)
344328
response = wr.opensearch.index_csv(client, path=file_path, index=index)
345-
print(response)
346329
assert response.get("success", 0) == 6
347330

348331

@@ -356,7 +339,6 @@ def test_index_csv_s3(client, path):
356339
bucket, key = wr._utils.parse_path(path)
357340
s3.upload_file(file_path, bucket, key)
358341
response = wr.opensearch.index_csv(client, path=path, index=index)
359-
print(response)
360342
assert response.get("success", 0) == 6
361343

362344

@@ -366,5 +348,4 @@ def test_index_json_s3_large_file(client):
366348
response = wr.opensearch.index_json(
367349
client, index="test_index_json_s3_large_file", path=path, json_path="Filings2011", id_keys=["EIN"], bulk_size=20
368350
)
369-
print(response)
370351
assert response.get("success", 0) > 0

0 commit comments

Comments
 (0)