elasticsearch 學習筆記之二 常見介面

夏之夜發表於2024-12-04

一、索引操作類

1、建立索引(PUT /索引名稱)

PUT /my-index-000001

請求body

{
  "settings": {
    "index": {
      "number_of_shards": 3,   //建立3個主片
      "number_of_replicas": 2   //每個主片都會有兩個副本
    }
  }
}

未指定mapping的情況下,使用預設的

2、刪除索引(DELETE /索引) (會刪除索引的資料及其配置) DELETE /iotrm_event_event_acs* 表示刪除 iotrm_event_event_acs 開頭的索引

注意:使用 Delete Index API 是不可逆的操作,一旦執行,索引及其所有資料都將被永久刪除

http://10.19.214.13:9200/data_iotrm_event_event_acs%240x00030403_2024-10-29?pretty --- 刪除索引data_iotrm_event_event_acs$0x00030403_2024-10-29

3、修改索引

4、檢視某個索引下的文件資料總量(GET /索引名稱/_count)

http://10.19.223.119:9200/iotrm_event_cl2event_2024-10-29/_count

{
    "count": 155701,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    }
}

5、檢視所有索引資訊(GET _cat/indices?v&format) --- 包括資料量、佔用大小

http://10.19.223.119:9200/_cat/indices?v

列說明:

  • pri:主片數量
  • rep:副本數量
  • store.size:總共儲存大小(主片+副本)
  • pri.store.size:主片大小

6、檢視某個索引資訊(GET _cat/indices/索引名稱?v&format) --- 包括資料量、佔用大小

http://10.19.223.119:9200/_cat/indices/iotrm_event_cl2telemeter_2024-09-20?v
query引數:bytes="b", format="json"

7、關閉所有索引(POST /*/_close) --- 關閉某個索引 將*替換成具體的索引名稱即可,多個明確的索引要關閉的話可以使用逗號隔開

http://10.19.214.13:9200/*/_close

8、開啟所有索引(POST /*/_open) --- 開啟某個索引 將*替換成具體的索引名稱即可,多個明確的索引要開啟的話可以使用逗號隔開

http://10.19.214.13:9200/*/_open

9、檢視一個索引資料實際分佈到各個節點機器的不同分片的情況(GET /_cat/shards/索引名稱)

http://10.19.223.119:9200/_cat/shards/iotrm_event_event_towercrane$0x00054001_2024-10-29?v

圖上可以看到這個索引副本都沒有建立

10、檢視一個索引定義資訊,包括 aliases、mappings、settings(GET /索引名稱)

http://10.19.223.119:9200/iotrm_event_temperaturetelemeter_2024-10-29

{
    "iotrm_event_temperaturetelemeter_2024-10-29": {
        "aliases": {},
        "mappings": {
            "properties": {
                "IOTProtocolType": {
                    "type": "long"
                },
                "basic": {
                    "properties": {
                        "IOTProtocolType": {
                            "type": "long"
                        },
                        "UUID": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "channelIndexCode": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "dateTime": {
                            "type": "date"
                        },
                        "domain": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "eventType": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "imageServerCode": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "indexCode": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "ipV4Address": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "ipV6Address": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "localIndex": {
                            "type": "long"
                        },
                        "macAddress": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "resourceCategory": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "sendTime": {
                            "type": "date"
                        }
                    }
                },
                "dataType": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "eventType": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "targetAttrs": {
                    "properties": {
                        "cameraIndexCode": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "customCategory": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "deviceCategory": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "deviceIndexCode": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "deviceName": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "deviceType": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "extendDeviceCode": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "modelId": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "orgPath": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "tenantId": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "telemeter": {
                    "properties": {
                        "tag": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "test": {
                            "type": "long"
                        },
                        "value": {
                            "type": "float"
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "3",
                "provided_name": "iotrm_event_temperaturetelemeter_2024-10-29",
                "creation_date": "1730365148113",
                "number_of_replicas": "1",
                "uuid": "XAQiTzvoTwGDYi2DMGeRJA",
                "version": {
                    "created": "7100299"
                }
            }
        }
    }
}

11、檢視某個索引的 primary 儲存資訊(GET /索引名稱/_stats/store)

http://10.19.223.119:9200/iotrm_event_event_towercrane%240x00054001_2024-10-29/_stats/store

{
    "_shards": {
        "total": 6,
        "successful": 3,
        "failed": 0
    },
    "_all": {
        "primaries": {
            "store": {
                "size_in_bytes": 13069189,
                "reserved_in_bytes": 0
            }
        },
        "total": {
            "store": {
                "size_in_bytes": 13069189,
                "reserved_in_bytes": 0
            }
        }
    },
    "indices": {
        "iotrm_event_event_towercrane$0x00054001_2024-10-29": {
            "uuid": "LUuow0N6TCOEvIDnYu1eUQ",
            "primaries": {
                "store": {
                    "size_in_bytes": 13069189,
                    "reserved_in_bytes": 0
                }
            },
            "total": {
                "store": {
                    "size_in_bytes": 13069189,
                    "reserved_in_bytes": 0
                }
            }
        }
    }
}

12、檢視指定索引的統計資訊(GET /索引名稱/_stats)

http://10.19.223.119:9200/iotrm_event_event_towercrane%240x00054001_2024-10-29/_stats

{
    "_shards": {
        "total": 6,
        "successful": 3,
        "failed": 0
    },
    "_all": {
        "primaries": {
            "docs": {
                "count": 36927,
                "deleted": 5767
            },
            "store": {
                "size_in_bytes": 13069189,
                "reserved_in_bytes": 0
            },
            "indexing": {
                "index_total": 0,
                "index_time_in_millis": 0,
                "index_current": 0,
                "index_failed": 0,
                "delete_total": 1444,
                "delete_time_in_millis": 98,
                "delete_current": 0,
                "noop_update_total": 0,
                "is_throttled": false,
                "throttle_time_in_millis": 0
            },
            "get": {
                "total": 0,
                "time_in_millis": 0,
                "exists_total": 0,
                "exists_time_in_millis": 0,
                "missing_total": 0,
                "missing_time_in_millis": 0,
                "current": 0
            },
            "search": {
                "open_contexts": 0,
                "query_total": 1328,
                "query_time_in_millis": 1884,
                "query_current": 0,
                "fetch_total": 6,
                "fetch_time_in_millis": 131,
                "fetch_current": 0,
                "scroll_total": 3,
                "scroll_time_in_millis": 1412,
                "scroll_current": 0,
                "suggest_total": 0,
                "suggest_time_in_millis": 0,
                "suggest_current": 0
            },
            "merges": {
                "current": 0,
                "current_docs": 0,
                "current_size_in_bytes": 0,
                "total": 0,
                "total_time_in_millis": 0,
                "total_docs": 0,
                "total_size_in_bytes": 0,
                "total_stopped_time_in_millis": 0,
                "total_throttled_time_in_millis": 0,
                "total_auto_throttle_in_bytes": 62914560
            },
            "refresh": {
                "total": 15,
                "total_time_in_millis": 176,
                "external_total": 12,
                "external_total_time_in_millis": 186,
                "listeners": 0
            },
            "flush": {
                "total": 6,
                "periodic": 0,
                "total_time_in_millis": 4303
            },
            "warmer": {
                "current": 0,
                "total": 9,
                "total_time_in_millis": 0
            },
            "query_cache": {
                "memory_size_in_bytes": 0,
                "total_count": 0,
                "hit_count": 0,
                "miss_count": 0,
                "cache_size": 0,
                "cache_count": 0,
                "evictions": 0
            },
            "fielddata": {
                "memory_size_in_bytes": 1456,
                "evictions": 0
            },
            "completion": {
                "size_in_bytes": 0
            },
            "segments": {
                "count": 9,
                "memory_in_bytes": 92804,
                "terms_memory_in_bytes": 73152,
                "stored_fields_memory_in_bytes": 4584,
                "term_vectors_memory_in_bytes": 0,
                "norms_memory_in_bytes": 10304,
                "points_memory_in_bytes": 0,
                "doc_values_memory_in_bytes": 4764,
                "index_writer_memory_in_bytes": 0,
                "version_map_memory_in_bytes": 0,
                "fixed_bit_set_memory_in_bytes": 0,
                "max_unsafe_auto_id_timestamp": -1,
                "file_sizes": {}
            },
            "translog": {
                "operations": 0,
                "size_in_bytes": 165,
                "uncommitted_operations": 0,
                "uncommitted_size_in_bytes": 165,
                "earliest_last_modified_age": 24819785
            },
            "request_cache": {
                "memory_size_in_bytes": 348918,
                "evictions": 0,
                "hit_count": 174,
                "miss_count": 114
            },
            "recovery": {
                "current_as_source": 0,
                "current_as_target": 0,
                "throttle_time_in_millis": 0
            }
        },
        "total": {
            "docs": {
                "count": 36927,
                "deleted": 5767
            },
            "store": {
                "size_in_bytes": 13069189,
                "reserved_in_bytes": 0
            },
            "indexing": {
                "index_total": 0,
                "index_time_in_millis": 0,
                "index_current": 0,
                "index_failed": 0,
                "delete_total": 1444,
                "delete_time_in_millis": 98,
                "delete_current": 0,
                "noop_update_total": 0,
                "is_throttled": false,
                "throttle_time_in_millis": 0
            },
            "get": {
                "total": 0,
                "time_in_millis": 0,
                "exists_total": 0,
                "exists_time_in_millis": 0,
                "missing_total": 0,
                "missing_time_in_millis": 0,
                "current": 0
            },
            "search": {
                "open_contexts": 0,
                "query_total": 1328,
                "query_time_in_millis": 1884,
                "query_current": 0,
                "fetch_total": 6,
                "fetch_time_in_millis": 131,
                "fetch_current": 0,
                "scroll_total": 3,
                "scroll_time_in_millis": 1412,
                "scroll_current": 0,
                "suggest_total": 0,
                "suggest_time_in_millis": 0,
                "suggest_current": 0
            },
            "merges": {
                "current": 0,
                "current_docs": 0,
                "current_size_in_bytes": 0,
                "total": 0,
                "total_time_in_millis": 0,
                "total_docs": 0,
                "total_size_in_bytes": 0,
                "total_stopped_time_in_millis": 0,
                "total_throttled_time_in_millis": 0,
                "total_auto_throttle_in_bytes": 62914560
            },
            "refresh": {
                "total": 15,
                "total_time_in_millis": 176,
                "external_total": 12,
                "external_total_time_in_millis": 186,
                "listeners": 0
            },
            "flush": {
                "total": 6,
                "periodic": 0,
                "total_time_in_millis": 4303
            },
            "warmer": {
                "current": 0,
                "total": 9,
                "total_time_in_millis": 0
            },
            "query_cache": {
                "memory_size_in_bytes": 0,
                "total_count": 0,
                "hit_count": 0,
                "miss_count": 0,
                "cache_size": 0,
                "cache_count": 0,
                "evictions": 0
            },
            "fielddata": {
                "memory_size_in_bytes": 1456,
                "evictions": 0
            },
            "completion": {
                "size_in_bytes": 0
            },
            "segments": {
                "count": 9,
                "memory_in_bytes": 92804,
                "terms_memory_in_bytes": 73152,
                "stored_fields_memory_in_bytes": 4584,
                "term_vectors_memory_in_bytes": 0,
                "norms_memory_in_bytes": 10304,
                "points_memory_in_bytes": 0,
                "doc_values_memory_in_bytes": 4764,
                "index_writer_memory_in_bytes": 0,
                "version_map_memory_in_bytes": 0,
                "fixed_bit_set_memory_in_bytes": 0,
                "max_unsafe_auto_id_timestamp": -1,
                "file_sizes": {}
            },
            "translog": {
                "operations": 0,
                "size_in_bytes": 165,
                "uncommitted_operations": 0,
                "uncommitted_size_in_bytes": 165,
                "earliest_last_modified_age": 24819785
            },
            "request_cache": {
                "memory_size_in_bytes": 348918,
                "evictions": 0,
                "hit_count": 174,
                "miss_count": 114
            },
            "recovery": {
                "current_as_source": 0,
                "current_as_target": 0,
                "throttle_time_in_millis": 0
            }
        }
    },
    "indices": {
        "iotrm_event_event_towercrane$0x00054001_2024-10-29": {
            "uuid": "LUuow0N6TCOEvIDnYu1eUQ",
            "primaries": {
                "docs": {
                    "count": 36927,
                    "deleted": 5767
                },
                "store": {
                    "size_in_bytes": 13069189,
                    "reserved_in_bytes": 0
                },
                "indexing": {
                    "index_total": 0,
                    "index_time_in_millis": 0,
                    "index_current": 0,
                    "index_failed": 0,
                    "delete_total": 1444,
                    "delete_time_in_millis": 98,
                    "delete_current": 0,
                    "noop_update_total": 0,
                    "is_throttled": false,
                    "throttle_time_in_millis": 0
                },
                "get": {
                    "total": 0,
                    "time_in_millis": 0,
                    "exists_total": 0,
                    "exists_time_in_millis": 0,
                    "missing_total": 0,
                    "missing_time_in_millis": 0,
                    "current": 0
                },
                "search": {
                    "open_contexts": 0,
                    "query_total": 1328,
                    "query_time_in_millis": 1884,
                    "query_current": 0,
                    "fetch_total": 6,
                    "fetch_time_in_millis": 131,
                    "fetch_current": 0,
                    "scroll_total": 3,
                    "scroll_time_in_millis": 1412,
                    "scroll_current": 0,
                    "suggest_total": 0,
                    "suggest_time_in_millis": 0,
                    "suggest_current": 0
                },
                "merges": {
                    "current": 0,
                    "current_docs": 0,
                    "current_size_in_bytes": 0,
                    "total": 0,
                    "total_time_in_millis": 0,
                    "total_docs": 0,
                    "total_size_in_bytes": 0,
                    "total_stopped_time_in_millis": 0,
                    "total_throttled_time_in_millis": 0,
                    "total_auto_throttle_in_bytes": 62914560
                },
                "refresh": {
                    "total": 15,
                    "total_time_in_millis": 176,
                    "external_total": 12,
                    "external_total_time_in_millis": 186,
                    "listeners": 0
                },
                "flush": {
                    "total": 6,
                    "periodic": 0,
                    "total_time_in_millis": 4303
                },
                "warmer": {
                    "current": 0,
                    "total": 9,
                    "total_time_in_millis": 0
                },
                "query_cache": {
                    "memory_size_in_bytes": 0,
                    "total_count": 0,
                    "hit_count": 0,
                    "miss_count": 0,
                    "cache_size": 0,
                    "cache_count": 0,
                    "evictions": 0
                },
                "fielddata": {
                    "memory_size_in_bytes": 1456,
                    "evictions": 0
                },
                "completion": {
                    "size_in_bytes": 0
                },
                "segments": {
                    "count": 9,
                    "memory_in_bytes": 92804,
                    "terms_memory_in_bytes": 73152,
                    "stored_fields_memory_in_bytes": 4584,
                    "term_vectors_memory_in_bytes": 0,
                    "norms_memory_in_bytes": 10304,
                    "points_memory_in_bytes": 0,
                    "doc_values_memory_in_bytes": 4764,
                    "index_writer_memory_in_bytes": 0,
                    "version_map_memory_in_bytes": 0,
                    "fixed_bit_set_memory_in_bytes": 0,
                    "max_unsafe_auto_id_timestamp": -1,
                    "file_sizes": {}
                },
                "translog": {
                    "operations": 0,
                    "size_in_bytes": 165,
                    "uncommitted_operations": 0,
                    "uncommitted_size_in_bytes": 165,
                    "earliest_last_modified_age": 24819785
                },
                "request_cache": {
                    "memory_size_in_bytes": 348918,
                    "evictions": 0,
                    "hit_count": 174,
                    "miss_count": 114
                },
                "recovery": {
                    "current_as_source": 0,
                    "current_as_target": 0,
                    "throttle_time_in_millis": 0
                }
            },
            "total": {
                "docs": {
                    "count": 36927,
                    "deleted": 5767
                },
                "store": {
                    "size_in_bytes": 13069189,
                    "reserved_in_bytes": 0
                },
                "indexing": {
                    "index_total": 0,
                    "index_time_in_millis": 0,
                    "index_current": 0,
                    "index_failed": 0,
                    "delete_total": 1444,
                    "delete_time_in_millis": 98,
                    "delete_current": 0,
                    "noop_update_total": 0,
                    "is_throttled": false,
                    "throttle_time_in_millis": 0
                },
                "get": {
                    "total": 0,
                    "time_in_millis": 0,
                    "exists_total": 0,
                    "exists_time_in_millis": 0,
                    "missing_total": 0,
                    "missing_time_in_millis": 0,
                    "current": 0
                },
                "search": {
                    "open_contexts": 0,
                    "query_total": 1328,
                    "query_time_in_millis": 1884,
                    "query_current": 0,
                    "fetch_total": 6,
                    "fetch_time_in_millis": 131,
                    "fetch_current": 0,
                    "scroll_total": 3,
                    "scroll_time_in_millis": 1412,
                    "scroll_current": 0,
                    "suggest_total": 0,
                    "suggest_time_in_millis": 0,
                    "suggest_current": 0
                },
                "merges": {
                    "current": 0,
                    "current_docs": 0,
                    "current_size_in_bytes": 0,
                    "total": 0,
                    "total_time_in_millis": 0,
                    "total_docs": 0,
                    "total_size_in_bytes": 0,
                    "total_stopped_time_in_millis": 0,
                    "total_throttled_time_in_millis": 0,
                    "total_auto_throttle_in_bytes": 62914560
                },
                "refresh": {
                    "total": 15,
                    "total_time_in_millis": 176,
                    "external_total": 12,
                    "external_total_time_in_millis": 186,
                    "listeners": 0
                },
                "flush": {
                    "total": 6,
                    "periodic": 0,
                    "total_time_in_millis": 4303
                },
                "warmer": {
                    "current": 0,
                    "total": 9,
                    "total_time_in_millis": 0
                },
                "query_cache": {
                    "memory_size_in_bytes": 0,
                    "total_count": 0,
                    "hit_count": 0,
                    "miss_count": 0,
                    "cache_size": 0,
                    "cache_count": 0,
                    "evictions": 0
                },
                "fielddata": {
                    "memory_size_in_bytes": 1456,
                    "evictions": 0
                },
                "completion": {
                    "size_in_bytes": 0
                },
                "segments": {
                    "count": 9,
                    "memory_in_bytes": 92804,
                    "terms_memory_in_bytes": 73152,
                    "stored_fields_memory_in_bytes": 4584,
                    "term_vectors_memory_in_bytes": 0,
                    "norms_memory_in_bytes": 10304,
                    "points_memory_in_bytes": 0,
                    "doc_values_memory_in_bytes": 4764,
                    "index_writer_memory_in_bytes": 0,
                    "version_map_memory_in_bytes": 0,
                    "fixed_bit_set_memory_in_bytes": 0,
                    "max_unsafe_auto_id_timestamp": -1,
                    "file_sizes": {}
                },
                "translog": {
                    "operations": 0,
                    "size_in_bytes": 165,
                    "uncommitted_operations": 0,
                    "uncommitted_size_in_bytes": 165,
                    "earliest_last_modified_age": 24819785
                },
                "request_cache": {
                    "memory_size_in_bytes": 348918,
                    "evictions": 0,
                    "hit_count": 174,
                    "miss_count": 114
                },
                "recovery": {
                    "current_as_source": 0,
                    "current_as_target": 0,
                    "throttle_time_in_millis": 0
                }
            }
        }
    }
}

二、資料操作類

資料在索引下稱之為文件,以下也稱之為文件

1、查詢某個索引下的所有文件資料(GET /索引/_search)

localhost:9200/my_index/_search?pretty

2、查詢某個索引下的某個文件ID對應的資料(GET /索引/_doc/文件ID/ )

http://10.19.214.13:9200/索引/_doc/文件ID

3、修改指定索引下的某個文件(POST 根據文件ID找到對應文件進行修改)

http://10.19.214.13:9200/索引/_doc/文件ID/_update

Postman 呼叫介面更改資料

透過 http://10.19.214.13:9200/data_iotrm_event_event_acs%240x00030403_2024-10-29/_doc/oxcqTJMBXFMQjG9sdTdP/ 介面 檢視該文件對應的資料

4、刪除指定索引下的某個文件(DELETE /索引/_doc/文件ID)

http://10.19.214.13:9200/data_iotrm_event_event_acs%240x00030403_2024-10-29/_doc/SBcoTJMBXFMQjG9sdTZM/

再次根據這個文件ID 檢視是否已經被刪除

5、某個索引下新增一個文件(POST/索引/_doc/docID)

http://10.19.214.13:9200/data_iotrm_event_event_acs%240x00030403_2024-10-29/_doc/1 在索引 data_iotrm_event_event_acs$0x00030403_2024-10-29 下建立一個ID為1的文件資料

檢視新增的資料

6、刪除指定索引下的所有文件(POST /索引/_delete_by_query?pretty)當索引為* 表示刪除所有索引下的所有文件資料

request body

{
"query": {
"match_all": {}
}
}

示例

elastic head 透過谷歌瀏覽器已經連線上es,可以看到目標索引下的資訊

可以看到索引“iotrm_event_event_acs$0x00030403_2024-10-29” 下有36700個文件

當下透過API將刪除該索引下的所有資料

開啟 Postman 輸入 http://10.19.214.13:9200/iotrm_event_event_acs%240x00030403_2024-10-29/_delete_by_query?pretty (注意:iotrm_event_event_acs$240x00030403_2024-10-29 索引ID,由於其中含有$,需要將這個字元變成%24 )

request body

{
"query": {
"match_all": {}
}
}

由上圖可知刪除了 36700 條資料

重新整理 elastichead 可以看到該索引下的資料

8、統計文件總數(GET _cat/indices)

curl -u elastic:elasticsearch_422 http://10.19.223.119:9200/_cat/indices |awk 'BEGIN{total=0}{total+=$7}END{print total}'

三、快照倉庫操作類

1、倉庫操作類

1)檢視當前所有的倉庫(GET _snapshot)

http://10.19.214.13:9200/_snapshot

2)註冊倉庫(PUT _snapshot)

{
  "type": "fs",
  "settings": {
    "location": "/opt/es/backup/iotrm"
  }
}

如果location 的地址在 es 的配置檔案 elasticsearch.yml 中沒有配置,那麼會報下面這個錯誤

[iotrm] location [/opt/es/backup/iotrm] doesn't match any of the locations specified by path.repo

配置

  • elasticsearch.yml 中倉庫配置如下:
path.repo: /opt/xx/web/xx/elasticsearch.1/data/backup,/opt/es/backup/iotrm

多個倉庫地址用逗號隔開

  • 每個倉庫地址必須和elasticsearch 啟動使用者是同一個,如es 啟動賬號是 es ,分組也是 es,則 這裡 /opt/es/backup/iotrm 需要屬於es,可以透過命令 chown -R es:es /opt/es/backup 設定
  • 重啟es服務

如果不將倉庫地址設定成 es 啟動使用者一致,則註冊倉庫會報如下錯誤

註冊

註冊成功的返回資訊如下:

3)刪除已經註冊的快照倉庫(DELETE /_snapshot/倉庫名稱)

4)審查快照倉庫並刪除未被快照引用的過時資料(POST /_snapshot/倉庫名稱/_cleanup)

5)手動校驗快照倉庫是否可用(POST _snapshot/倉庫名稱/_verify)

2、快照操作類

1)檢視某個倉庫下所有快照(GET _snapshot/倉庫名稱/*)

http://10.19.223.119:9200/_snapshot/iotrm/*

2)在某個倉庫下建立快照(PUT _snapshot/倉庫名/快照名稱?wait_for_completion=true)

http://10.19.223.119:9200/_snapshot/iotrm/%3Ciotrm-%7Bnow%7ByyyyMMddHHmmss%7D%7D%3E?wait_for_completion=true 快照名稱可以配置時間格式,%3Ciotrm-%7Bnow%7ByyyyMMddHHmmss%7D%7D%3E 實際是:<iotrm-{now{yyyyMMddHHmmss}}>

請求body

{
  "include_global_state": false,
  "indices":["iotrm*"],
  "metadata": {
    "taken_by": "wangfang5",
    "taken_because": "iotrm,backup test"
  }
}

表示將 iotrm 開頭的所有索引進行備份

返回內容

    "snapshot": {
        "snapshot": "iotrm-20241203072538",
        "uuid": "idfOxhbCR961R6o3IiKpKQ",
        "version_id": 7100299,
        "version": "7.10.2",
        "indices": [
            "iotrm_xx_2024-09-12",
            "iotrm_xx",
            "iotrm_yy",
            "iotrm_xx$0x00070101_2024-11-30",
            "iotrm_yy_2024-09-12"
        ],
        "data_streams": [],
        "include_global_state": false,
        "metadata": {
            "taken_by": "wangfang5",
            "taken_because": "iotrm,backup test"
        },
        "state": "SUCCESS",
        "start_time": "2024-12-03T07:25:38.909Z",
        "start_time_in_millis": 1733210738909,
        "end_time": "2024-12-03T07:29:24.148Z",
        "end_time_in_millis": 1733210964148,
        "duration_in_millis": 225239,
        "failures": [],
        "shards": {
            "total": 5,
            "failed": 0,
            "successful": 5
        }
    }
}

3)還原某個倉庫下的快照(POST _snapshot/倉庫名稱/快照名稱/_restore?wait_for_completion=true)

http://10.19.214.13:9200/_snapshot/iotrm/iotrm-20241203072538/_restore?wait_for_completion=true

響應內容

{
    "snapshot": {
        "snapshot": "iotrm-20241203072538",
        "indices": [

"iotrm_xx_2024-09-12",
            "iotrm_xx",
            "iotrm_yy",
            "iotrm_xx$0x00070101_2024-11-30",
            "iotrm_yy_2024-09-12"
 ], "shards": { "total": 5, "failed": 0, "successful": 5} } }

4) 從某個倉庫的某個快照進行還原,但是指定還原的索引(POST _snapshot/倉庫名稱/快照名稱/_restore?wait_for_completion=true

http://10.19.214.13:9200/_snapshot/total/all-20241203124406/_restore?wait_for_completion=true

請求body

{
  "indices": "test_event_illuminancetelemeter"
}

響應body

{
    "snapshot": {
        "snapshot": "all-20241203124406",
        "indices": [
            "test_event_illuminancetelemeter"
        ],
        "shards": {
            "total": 3,
            "failed": 0,
            "successful": 3
        }
    }
}

5)在某個倉庫下刪除快照(DELETE /_snapshot/倉庫名稱/快照名稱)

http://10.19.223.119:9200/_snapshot/es_data_repository/es_data_repository-20241128000000

四、節點操作類

1、查詢所有節點的索引搜尋資訊(GET /_nodes/stats/indices/search)

2、查詢節點資源佔用情況(GET /_cat/nodes)

http://10.19.223.119:9200/_cat/nodes?format=json

也可以使用引數 v&format 控制輸出格式

3、檢視版本資訊(GET /)

http://10.19.223.119:9200/

還可以透過 elasticsearch 工具查詢:elasticsearch --version

五、叢集

1、檢視叢集健康資訊(GET /_cluster/health)

http://10.19.223.119:9200/_cluster/health

2、檢視某個索引在叢集內的分片健康狀態(GET _cluster/health/索引名稱?level=shards)

http://10.19.214.18:9200/_cluster/health/iotrm_event_event_acs%240x00030219_2024-09-12?level=shards

{
    "cluster_name": "hik-ga-es",
    "status": "yellow",
    "timed_out": false,
    "number_of_nodes": 1,
    "number_of_data_nodes": 1,
    "active_primary_shards": 3,
    "active_shards": 3,
    "relocating_shards": 0,
    "initializing_shards": 0,
    "unassigned_shards": 3,
    "delayed_unassigned_shards": 0,
    "number_of_pending_tasks": 0,
    "number_of_in_flight_fetch": 0,
    "task_max_waiting_in_queue_millis": 0,
    "active_shards_percent_as_number": 54.51327433628319,
    "indices": {
        "iotrm_event_event_acs$0x00030219_2024-09-12": {
            "status": "yellow",
            "number_of_shards": 3,    #3個主分片
            "number_of_replicas": 1,   #1個副本,即每個主片都有一個副本
            "active_primary_shards": 3,
            "active_shards": 3,
            "relocating_shards": 0,
            "initializing_shards": 0,
            "unassigned_shards": 3,   #3個分片未分配,這是因為這是單es節點,而該索引建立的時候指明瞭需要1個副本,因此這裡3表示未分配3個副本分片
            "shards": {
                "0": {
                    "status": "yellow",
                    "primary_active": true,
                    "active_shards": 1,
                    "relocating_shards": 0,
                    "initializing_shards": 0,
                    "unassigned_shards": 1
                },
                "1": {
                    "status": "yellow",
                    "primary_active": true,
                    "active_shards": 1,
                    "relocating_shards": 0,
                    "initializing_shards": 0,
                    "unassigned_shards": 1
                },
                "2": {
                    "status": "yellow",
                    "primary_active": true,
                    "active_shards": 1,
                    "relocating_shards": 0,
                    "initializing_shards": 0,
                    "unassigned_shards": 1
                }
            }
        }
    }
}

相關文章