Skip to content

Instantly share code, notes, and snippets.

@kiennt2
Created October 22, 2025 10:50
Show Gist options
  • Select an option

  • Save kiennt2/414092d4f38edfd42547f6f92fbe2b82 to your computer and use it in GitHub Desktop.

Select an option

Save kiennt2/414092d4f38edfd42547f6f92fbe2b82 to your computer and use it in GitHub Desktop.
OpenSearch: Make Field Queryable Without Creating New Index
### 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
```
@kiennt2
Copy link
Copy Markdown
Author

kiennt2 commented Nov 27, 2025

# create pipeline
PUT _ingest/pipeline/core_products_default_pipeline
{
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": """
          def parts = new ArrayList();

          if (ctx.sku != null && ctx.sku.toString().trim().length() > 0) {
            parts.add(ctx.sku.toString().trim().toLowerCase());
          }

          if (ctx.name != null && ctx.name.toString().trim().length() > 0) {
            parts.add(ctx.name.toString().trim().toLowerCase());
          }

          if (ctx.barcode != null && ctx.barcode.toString().trim().length() > 0) {
            parts.add(ctx.barcode.toString().trim().toLowerCase());
          }

          if (ctx.upc != null && ctx.upc.toString().trim().length() > 0) {
            parts.add(ctx.upc.toString().trim().toLowerCase());
          }

          if (ctx.gtin != null && ctx.gtin.toString().trim().length() > 0) {
            parts.add(ctx.gtin.toString().trim().toLowerCase());
          }

          if (!parts.isEmpty()) {
            ctx.product_search_combined = String.join(' ', parts);
          } else {
            ctx.product_search_combined = '';
          }
        """
      }
    }
  ]
}

# list all pipeline
GET _ingest/pipeline

# pipeline details
GET _ingest/pipeline/core_products_default_pipeline

# update mapping -> need refresh mapping & fields
PUT core_products/_mapping
{
  "properties": {
    "product_search_combined": {
      "type": "keyword",
      "ignore_above": 32766
    }
  }
}

# set default pipeline for index, new doc will have new field
PUT core_products/_settings
{
  "index.default_pipeline": "core_products_default_pipeline"
}

# update existing docs
# NOTE: Make sure free disk > used disk
POST core_products/_update_by_query?pipeline=core_products_default_pipeline&conflicts=proceed&wait_for_completion=false
{
  "query": {
    "match_all": {}
  }
}

# view task progress, task id get from prev action
GET _tasks/1zsw2RngShWru4A4sMJ3UA:78005101

# refresh index & fields by API or manual refresh
POST core_products/_refresh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment