elasticsearch 学习笔记之二 常见接口

一、索引操作类

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/仓库名称)

 http://10.19.214.3:9200/_snapshot/all/

 

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
                }
            }
        }
    }
}

 

posted @ 2024-12-04 11:16  夏之夜  阅读(10)  评论(0编辑  收藏  举报