Created
October 22, 2025 10:50
-
-
Save kiennt2/414092d4f38edfd42547f6f92fbe2b82 to your computer and use it in GitHub Desktop.
OpenSearch: Make Field Queryable Without Creating New Index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### 1 - Update Mapping | |
| ```shell | |
| # example of text field | |
| PUT /your_index_name/_mapping | |
| { | |
| "properties": { | |
| "your_field_path": { | |
| "type": "text", | |
| "fields": { | |
| "keyword": { | |
| "type": "keyword" | |
| } | |
| } | |
| } | |
| } | |
| } | |
| ``` | |
| Result: New documents with this field are immediately queryable. Old documents won't have it, but that's fine. | |
| ### 2 - Update existing documents | |
| ```shell | |
| POST /your_index_name/_update_by_query?wait_for_completion=false&conflicts=proceed | |
| { | |
| "query": { | |
| "match_all": {} | |
| } | |
| } | |
| ``` | |
| Optional - Use a script to populate default values: | |
| ```shell | |
| POST /your_index_name/_update_by_query | |
| { | |
| "query": { | |
| "match_all": {} | |
| }, | |
| "script": { | |
| "source": "if (ctx._source.my_field == null) { ctx._source.my_field = 'default_value' }" | |
| } | |
| } | |
| ``` | |
| ### 3 - Refresh | |
| ```shell | |
| POST /your_index_name/_refresh | |
| ``` | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
EXAMPLE to add more field to current index
Step 1: Create ingest pipeline
PUT _ingest/pipeline/discount_flag_pipeline
{
"processors": [
{
"script": {
"lang": "painless",
"source": """
def field = ctx.json_discount_application;
if (field == null) {
ctx.has_discount_application = false;
} else if (field instanceof List) {
ctx.has_discount_application = !field.isEmpty();
} else {
ctx.has_discount_application = true;
}
"""
}
}
]
}
Step 2: Add mapping for boolean field
PUT /order_logs/_mapping
{
"properties": {
"has_discount_application": {
"type": "boolean"
}
}
}
Step 3: Update EXISTING documents
POST /order_logs/_update_by_query?pipeline=discount_flag_pipeline
{
"query": {
"match_all": {}
}
}
Step 4: Set pipeline as default (for FUTURE data)
PUT /order_logs/_settings
{
"index.default_pipeline": "discount_flag_pipeline"
}