Skip to content

Commit aa81c27

Browse files
authored
Add support for routing_keys in index_documents
Adding support for routing_keys in the index_documents API. The routing_keys is a optional argument when provided will compound the keys joining with _ and add as the routing key for indexing.
1 parent 3b94189 commit aa81c27

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

awswrangler/opensearch/_write.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def _actions_generator(
6161
doc_type: str | None,
6262
keys_to_write: list[str] | None,
6363
id_keys: list[str] | None,
64+
routing_keys: list[str] | None = None,
6465
bulk_size: int = 10000,
6566
) -> Generator[list[dict[str, Any]], None, None]:
6667
bulk_chunk_documents = []
@@ -77,6 +78,11 @@ def _actions_generator(
7778
"_source": _selected_keys(document, keys_to_write),
7879
}
7980
)
81+
82+
if routing_keys:
83+
_routing = "-".join([str(document[routing_key]) for routing_key in routing_keys])
84+
bulk_chunk_documents[-1]["_routing"] = _routing
85+
8086
if (i + 1) % bulk_size == 0:
8187
yield bulk_chunk_documents
8288
bulk_chunk_documents = []
@@ -496,6 +502,7 @@ def index_documents(
496502
doc_type: str | None = None,
497503
keys_to_write: list[str] | None = None,
498504
id_keys: list[str] | None = None,
505+
routing_keys: list[str] | None = None,
499506
ignore_status: list[Any] | tuple[Any] | None = None,
500507
bulk_size: int = 1000,
501508
chunk_size: int | None = 500,
@@ -540,6 +547,8 @@ def index_documents(
540547
id_keys
541548
list of keys that compound document unique id. If not provided will use `_id` key if exists,
542549
otherwise will generate unique identifier for each document.
550+
routing_keys
551+
list of keys that compound document routing key. Optional.
543552
ignore_status
544553
list of HTTP status codes that you want to ignore (not raising an exception)
545554
bulk_size
@@ -599,7 +608,7 @@ def index_documents(
599608
_logger.debug("indexing %s documents into %s", total_documents, index)
600609

601610
actions = _actions_generator(
602-
documents, index, doc_type, keys_to_write=keys_to_write, id_keys=id_keys, bulk_size=bulk_size
611+
documents, index, doc_type, keys_to_write=keys_to_write, id_keys=id_keys, routing_keys=routing_keys, bulk_size=bulk_size
603612
)
604613

605614
success = 0

0 commit comments

Comments
 (0)