Loading...
Loading...
OpenSearch development best practices for indexing, querying, search optimization, vector search, and cluster management
npx skill4agent add bigdataboutique/skills opensearch-best-practices{
"mappings": {
"properties": {
"product_id": {
"type": "keyword"
},
"name": {
"type": "text",
"analyzer": "standard",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"description": {
"type": "text",
"analyzer": "english"
},
"price": {
"type": "scaled_float",
"scaling_factor": 100
},
"category": {
"type": "keyword"
},
"tags": {
"type": "keyword"
},
"created_at": {
"type": "date"
},
"metadata": {
"type": "object",
"enabled": false
},
"location": {
"type": "geo_point"
}
}
}
}keywordtextdatenumeric typesbooleangeo_pointnestedknn_vector{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"refresh_interval": "30s",
"analysis": {
"analyzer": {
"custom_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "asciifolding", "synonym_filter"]
}
},
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms": ["laptop, notebook", "phone, mobile, smartphone"]
}
}
}
}
}replication.type: SEGMENT{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}PUT _plugins/_ism/policies/<policy_id>{
"policy": {
"description": "Hot-warm-delete lifecycle",
"default_state": "hot",
"ism_template": [
{
"index_patterns": ["logs-*"],
"priority": 100
}
],
"states": [
{
"name": "hot",
"actions": [
{
"rollover": {
"min_size": "50gb",
"min_index_age": "7d"
}
}
],
"transitions": [
{
"state_name": "warm",
"conditions": {
"min_index_age": "30d"
}
}
]
},
{
"name": "warm",
"actions": [
{
"replica_count": {
"number_of_replicas": 1
}
},
{
"force_merge": {
"max_num_segments": 1
}
}
],
"transitions": [
{
"state_name": "delete",
"conditions": {
"min_index_age": "90d"
}
}
]
},
{
"name": "delete",
"actions": [
{
"delete": {}
}
],
"transitions": []
}
]
}
}ism_template_plugins/_ism/policies/<id>_ilm/policy/<id>min_index_agemin_doc_countmin_sizecronrolloverforce_mergeshrinkdeleteread_onlyread_writereplica_countindex_prioritycloseopensnapshotallocationnotificationPUT _plugins/_ism/policies/<policy_id> # Create/update policy
GET _plugins/_ism/policies/<policy_id> # Get policy
DELETE _plugins/_ism/policies/<policy_id> # Delete policy
POST _plugins/_ism/add/<index> # Attach policy to existing index
POST _plugins/_ism/remove/<index> # Detach policy
GET _plugins/_ism/explain/<index> # Get ISM status for an index
POST _plugins/_ism/retry/<index> # Retry failed action{
"query": {
"match": {
"description": {
"query": "wireless bluetooth headphones",
"operator": "and",
"fuzziness": "AUTO"
}
}
}
}{
"query": {
"term": {
"status": "active"
}
}
}{
"query": {
"bool": {
"must": [
{ "match": { "name": "laptop" } }
],
"filter": [
{ "term": { "category": "electronics" } },
{ "range": { "price": { "gte": 500, "lte": 2000 } } }
],
"should": [
{ "term": { "brand": "apple" } }
],
"must_not": [
{ "term": { "status": "discontinued" } }
]
}
}
}filtermustkeywordsize{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "search terms",
"fields": ["name^3", "description", "tags^2"],
"type": "best_fields"
}
},
"filter": [
{ "term": { "active": true } },
{ "range": { "created_at": { "gte": "now-30d" } } }
]
}
},
"size": 20,
"from": 0,
"_source": ["name", "price", "category"]
}PUT /my-vector-index
{
"settings": {
"index": {
"knn": true,
"knn.algo_param.ef_search": 100
}
},
"mappings": {
"properties": {
"my_vector": {
"type": "knn_vector",
"dimension": 768,
"method": {
"name": "hnsw",
"space_type": "l2",
"engine": "faiss",
"parameters": {
"ef_construction": 256,
"m": 16
}
}
},
"title": { "type": "text" },
"category": { "type": "keyword" }
}
}
}l2cosinesimilinnerproductl1linfGET /my-vector-index/_search
{
"size": 10,
"query": {
"knn": {
"my_vector": {
"vector": [0.1, 0.2, 0.3],
"k": 10
}
}
}
}GET /my-vector-index/_search
{
"size": 10,
"query": {
"knn": {
"my_vector": {
"vector": [0.1, 0.2, 0.3],
"k": 10,
"filter": {
"term": { "category": "electronics" }
}
}
}
}
}k"knn": trueef_searchef_constructionmknn.memory.circuit_breaker.limitGET /_plugins/_knn/warmup/{index}innerproduct_sourcedoc_valuescode_size=8mon_diskPUT /my-vector-index
{
"settings": {
"index": {
"knn": true
}
},
"mappings": {
"properties": {
"my_vector": {
"type": "knn_vector",
"dimension": 768,
"mode": "on_disk"
}
}
}
}PUT _cluster/settings
{
"persistent": {
"search.concurrent_segment_search.enabled": true
}
}PUT /_ingest/pipeline/neural-search-pipeline
{
"description": "Pipeline for neural search",
"processors": [
{
"text_embedding": {
"model_id": "<model_id>",
"field_map": {
"title": "title_embedding"
}
}
}
]
}GET /my-index/_search
{
"query": {
"neural": {
"title_embedding": {
"query_text": "comfortable running shoes",
"model_id": "<model_id>",
"k": 10
}
}
}
}PUT /_search/pipeline/hybrid-pipeline
{
"description": "Hybrid search pipeline",
"phase_results_processors": [
{
"normalization-processor": {
"normalization": {
"technique": "min_max"
},
"combination": {
"technique": "arithmetic_mean",
"parameters": {
"weights": [0.3, 0.7]
}
}
}
}
]
}GET /my-index/_search?search_pipeline=hybrid-pipeline
{
"query": {
"hybrid": {
"queries": [
{
"match": {
"title": "wireless headphones"
}
},
{
"knn": {
"my_vector": {
"vector": [0.1, 0.2, 0.3],
"k": 10
}
}
}
]
}
}
}plugins.ml_commons.only_run_on_ml_node: truemodel_groupPOST /_plugins/_ml/connectors/_create
{
"name": "Amazon Bedrock Connector",
"description": "Connector for Titan Embeddings",
"version": 1,
"protocol": "aws_sigv4",
"parameters": {
"region": "us-east-1",
"service_name": "bedrock"
},
"actions": [
{
"action_type": "predict",
"method": "POST",
"url": "https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.titan-embed-text-v1/invoke",
"headers": { "content-type": "application/json" },
"request_body": "{ \"inputText\": \"${parameters.inputText}\" }"
}
]
}{
"size": 0,
"aggs": {
"categories": {
"terms": {
"field": "category",
"size": 10
},
"aggs": {
"avg_price": {
"avg": { "field": "price" }
}
}
},
"price_ranges": {
"range": {
"field": "price",
"ranges": [
{ "to": 100 },
{ "from": 100, "to": 500 },
{ "from": 500 }
]
}
},
"date_histogram": {
"date_histogram": {
"field": "created_at",
"calendar_interval": "month"
}
}
}
}size: 0shard_sizeaggsPOST _bulk
{ "index": { "_index": "products", "_id": "1" } }
{ "name": "Product 1", "price": 99.99 }
{ "index": { "_index": "products", "_id": "2" } }
{ "name": "Product 2", "price": 149.99 }PUT /products/_settings
{
"refresh_interval": "-1"
}PUT /products/_settings
{
"refresh_interval": "1s"
}
POST /products/_refreshPOST /products/_update/1
{
"doc": {
"price": 89.99,
"updated_at": "2024-01-15T10:30:00Z"
}
}POST /products/_update_by_query
{
"query": {
"term": { "category": "electronics" }
},
"script": {
"source": "ctx._source.on_sale = true"
}
}{
"settings": {
"analysis": {
"analyzer": {
"product_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding",
"english_stop",
"english_stemmer"
]
},
"autocomplete_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"edge_ngram_filter"
]
}
},
"filter": {
"english_stop": {
"type": "stop",
"stopwords": "_english_"
},
"english_stemmer": {
"type": "stemmer",
"language": "english"
},
"edge_ngram_filter": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 15
}
}
}
}
}POST /products/_analyze
{
"analyzer": "product_analyzer",
"text": "Wireless Bluetooth Headphones"
}{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"suggest": {
"type": "completion"
}
}
}
}
}
}{
"suggest": {
"product-suggest": {
"prefix": "wire",
"completion": {
"field": "name.suggest",
"size": 5,
"fuzzy": {
"fuzziness": "AUTO"
}
}
}
}
}{
"query": {
"match": { "description": "wireless" }
},
"highlight": {
"fields": {
"description": {
"pre_tags": ["<em>"],
"post_tags": ["</em>"],
"fragment_size": 150
}
}
}
}POST /_plugins/_sql
{
"query": "SELECT category, COUNT(*) as cnt, AVG(price) as avg_price FROM products GROUP BY category HAVING cnt > 10 ORDER BY avg_price DESC"
}POST /_plugins/_ppl
{
"query": "source=server-logs | where response_code >= 500 | stats count() as error_count by host | where error_count > 100 | sort - error_count"
}"format": "jdbc""format": "csv"LIMITfiltersearch_after_sourcedoc_values{
"query": { "match_all": {} },
"size": 20,
"search_after": [1705329600000, "product_123"],
"sort": [
{ "created_at": "desc" },
{ "_id": "asc" }
]
}PUT /my-index
{
"settings": {
"index": {
"replication.type": "SEGMENT",
"number_of_replicas": 1
}
}
}PUT /my-remote-index
{
"settings": {
"index": {
"replication.type": "SEGMENT",
"remote_store.enabled": true,
"remote_store.segment.repository": "my-s3-repo",
"remote_store.translog.repository": "my-s3-repo"
}
}
}GET _cluster/health
GET _cat/indices?v
GET _cat/shards?v
GET _nodes/statsGET localhost:9600/_plugins/_performanceanalyzer/metrics?metrics=Latency,CPU_Utilization&agg=avg&dim=OperationPOST /products/_forcemerge?max_num_segments=1
POST /products/_cache/clear
POST /products/_refreshPUT /products/_settings
{
"index.search.slowlog.threshold.query.warn": "10s",
"index.search.slowlog.threshold.query.info": "5s",
"index.search.slowlog.threshold.fetch.warn": "1s"
}POST /_plugins/_anomaly_detection/detectors
{
"name": "cpu-anomaly-detector",
"description": "Detect CPU usage anomalies",
"time_field": "@timestamp",
"indices": ["server-metrics-*"],
"feature_attributes": [
{
"feature_name": "cpu_usage",
"feature_enabled": true,
"aggregation_query": {
"cpu_avg": {
"avg": { "field": "cpu.usage" }
}
}
}
],
"detection_interval": { "period": { "interval": 5, "unit": "Minutes" } },
"window_delay": { "period": { "interval": 1, "unit": "Minutes" } }
}window_delay_plugins/_security/api/PUT _plugins/_security/api/roles/products_reader
{
"cluster_permissions": [
"cluster_monitor"
],
"index_permissions": [
{
"index_patterns": ["products*"],
"allowed_actions": ["read", "search"]
}
]
}PUT _plugins/_security/api/roles/limited_access
{
"index_permissions": [
{
"index_patterns": ["users"],
"allowed_actions": ["read"],
"fls": ["name", "email", "created_at"],
"dls": "{\"bool\": {\"must\": [{\"term\": {\"department\": \"engineering\"}}]}}",
"masked_fields": ["email"]
}
]
}fls~dlsmasked_fieldsPUT _plugins/_security/api/rolesmapping/products_reader
{
"users": ["analyst_jane"],
"backend_roles": ["analysts"],
"hosts": []
}_plugins/_security/api/_security/allowed_actionsprivilegesflsdlsmasked_fieldsfield_security.grant/excepttenant_permissionssecurityadmin.shreadwritesearchcrudmanagecreate_indexindices_allcluster_monitorcluster_allPOST _aliases
{
"actions": [
{ "add": { "index": "products_v2", "alias": "products" } },
{ "remove": { "index": "products_v1", "alias": "products" } }
]
}POST _reindex
{
"source": {
"index": "products_v1"
},
"dest": {
"index": "products_v2"
},
"script": {
"source": "ctx._source.migrated_at = new Date().toString()"
}
}POST /_plugins/_notifications/configs
{
"config": {
"name": "ops-slack",
"description": "Slack channel for ops alerts",
"config_type": "slack",
"is_enabled": true,
"slack": {
"url": "https://hooks.slack.com/services/xxx/yyy/zzz"
}
}
}PUT /_plugins/_replication/follower-index/_start
{
"leader_alias": "leader-cluster",
"leader_index": "my-index",
"use_roles": {
"leader_cluster_role": "cross_cluster_replication_leader_full_access",
"follower_cluster_role": "cross_cluster_replication_follower_full_access"
}
}GET /_plugins/_replication/<index>/_status_plugins/| Feature | API Prefix |
|---|---|
| Security | |
| ISM | |
| Alerting | |
| Anomaly Detection | |
| k-NN | |
| ML Commons | |
| SQL | |
| PPL | |
| Notifications | |
| Replication | |
| Observability | |
| Rollups | |
| Transforms | |
_opendistro/_plugins/