To apply a script to multiple documents where each may have a unique routing ID, you'll use the Update By Query API along with specifying document IDs and their respective routing IDs individually. However, my previous response could have been clearer: Update By Query operates at the index or query scope rather than targeting specific document IDs with individual routing directly in a singular API call.
For updating specific documents where each has a unique _routing ID, you'd typically need to execute individual update requests. If, however, you're working within a context where you believe you can batch this process because ES has updates on their API that I might not be aware of, the conceptual approach would be similar but not directly supported via a single query without custom logic or iterative requests.
Given the constraint and focusing on clarity, here's how to handle individual document updates, each with its routing:
Individual Document Update with Specific Routing
For each document you want to update, you'll use the Update API, specifying the document's ID and its routing value. This approach requires a separate request for each document if they have unique routing values.
Example Request (for one document):
POST /v_dev_dataset_document_tags_index/_update/ZhxNyI4BWFZ4mUbq-rFX?routing=6b576762832bcb86c5fef8f8a26dc3494470a262
{
"script": {
"source": """
HashSet existingThemes = new HashSet(ctx._source.theme);
List themesToAdd = params.themesToAdd;
List themesToRemove = params.themesToRemove;
for (String themeToRemove : themesToRemove) {
existingThemes.remove(themeToRemove);
}
for (String themeToAdd : themesToAdd) {
existingThemes.add(themeToAdd);
}
ctx._source.theme = new ArrayList(existingThemes);
""",
"lang": "painless",
"params": {
"themesToAdd": ["ThemeX"], // Your themes to add
"themesToRemove": ["ThemeY"] // Your themes to remove
}
}
}
Automating For Multiple Documents
Since you might be looking to automate this for multiple documents each with a unique routing ID, here's a pseudo-code outline of how that process might look in a scripting or programming environment:
documents_to_update = [
{"id": "ZhxNyI4BWFZ4mUbq-rFX", "routing": "6b576762832bcb86c5fef8f8a26dc3494470a262"},
# Add more documents as needed
]
update_script = {
"script": {
"source": """
HashSet existingThemes = new HashSet(ctx._source.theme);
List themesToAdd = params.themesToAdd;
List themesToRemove = params.themesToRemove;
for (String themeToRemove : themesToRemove) {
existingThemes.remove(themeToRemove);
}
for (String themeToAdd : themesToAdd) {
existingThemes.add(themeToAdd);
}
ctx._source.theme = new ArrayList(existingThemes);
""",
"lang": "painless",
"params": {
"themesToAdd": ["ThemeX"], # Your themes to add
"themesToRemove": ["ThemeY"] # Your themes to remove
}
}
}
for doc in documents_to_update:
update_endpoint = f'/v_dev_dataset_document_tags_index/_update/{doc["id"]}?routing={doc["routing"]}'
# Use your HTTP library of choice to POST to the update_endpoint with the body `update_script`
# Example: requests.post(es_host + update_endpoint, json=update_script)
This is a Python-like pseudocode showing the process of iterating through documents to issue updates where each document may have a unique routing. In practice, you would replace placeholders with actual values and implement the HTTP request logic using a library like requests in Python or an equivalent in other languages/software.
Remember, this process can generate a lot of HTTP requests, so consider the performance implications and possibly rate-limit your requests to avoid overwhelming your Elasticsearch cluster.