Elasticsearch索引自動刪除

2022-02-21 23:07:40 字數 2157 閱讀 5775

指令碼分2部分,1部分查詢符合條件的索引名,2指令碼呼叫1指令碼,進行刪除操作
查詢符合條件的,預設大於30天

# coding:utf-8

__author__ = 'jipu fang'

from elasticsearch import elasticsearch

import re

import time

import datetime

now = time.localtime()

data1 = datetime.datetime(now[0], now[1], now[2])

es=elasticsearch("")

res = es.cat.indices()

l = res.strip().split()

def dindex(day=30):

index =

for i in l:

if re.search('\d+\.\d+\.\d+$', i):

itime = time.strptime(re.findall('\d+\.\d+\.\d+$', i)[0], "%y.%m.%d")

data2 = datetime.datetime(itime[0], itime[1], itime[2])

d = (data1-data2).days

if int(d) > int(day):

return index

if __name__ == '__main__':

print dindex()

對符合條件的索引,進行刪除操作

# coding:utf-8

__author__ = 'jipu fang'

import requests

import json

import time

from multiprocessing.dummy import pool as threadpool

import re

import indexs

''''''

# request api

class es_api:

def __init__(self, url, data, headers):

self.url=url

self.data=data

self.headers=headers

def delete(self):

r = requests.delete(url=self.url, data=json.dumps(self.data), headers=self.headers)

v=r.text

print(v)

def post(self):

r = requests.post(url=self.url, data=json.dumps(self.data), headers=self.headers)

v=r.text

print(v)

# 刪除索引,day保留多少天

def delete_index(day):

for i in indexs.dindex(day):

url = r"/%s" %(i)

data = }}

c=es_api(url, data, headers)

c.delete()

time.sleep(3)

return "delete indexs ok!"

# 關閉索引,day保留多少天,當索引處於關閉狀態,資源占用比較少

def close_index(day):

for i in indexs.dindex(day):

url = r"/%s/_close?pretty" %(i)

data = {}

c=es_api(url, data, headers)

c.post()

time.sleep(3)

return "index status close ok!"

delete_index(30)

time.sleep(60)

close_index(15)

Elasticsearch索引建議

背景 最近在做日誌收集,用到elasticsearch作為儲存層,因為日誌量比較大,一天近2t,所以每時每刻都會有大量的插入操作。又由於是給開發人員查日誌使用,所以查詢的量比較小。受限於儲存空間,目前僅保留7天的日誌,多數為乙個業務模組乙個索引,按天切分索引。關於索引和型別的取捨,建立多個索引還是乙...

elasticsearch 建立索引

介紹 分詞器 analyzer 包括乙個分解器 tokenizer 和多個詞元過濾器 filter 詞元過濾器的作是對分詞器提取的詞元進一步處理,比如轉成小寫,使用edge ngram,同義詞等,處理之後成為索引詞 term 文件正包含了幾個這樣的term成為frequency 詞頻 分解器 tok...

Elasticsearch 索引例項

elasticsearch包含了一系列的感念,比如索引 indexing 搜尋 search 以及聚合 aggregations 現在我們主要介紹indexing。在elasticsearch中,文件歸屬於一種型別 type 而這些型別存在於索引 index 中,我們可以畫一些簡單的對比圖來模擬傳統...