0
点赞
收藏
分享

微信扫一扫

Jenkins生成html报告

python清除一个月以前的ES索引文档数据

 先查看一下mysql 数据,看一下那一列是日期字段
 看到是 edittime 列

在这里插入图片描述

以下是 python 脚本

vim delete_old_noticeresult.py

import datetime
from elasticsearch import Elasticsearch, RequestError
import logging

# 配置日志
logging.basicConfig(filename='/var/log/es-index/delete_old_bidnotice.log', level=logging.INFO, format='%(asctime)s - %(message)s')

def delete_old_documents():
    try:
        # 获取当前日期
        now = datetime.datetime.now()
        logging.info("Current date and time: %s", now)
        
        # 计算一个月前的日期
        one_month_ago = now - datetime.timedelta(days=30)
        logging.info("Date and time one month ago: %s", one_month_ago)
        
        # 创建 Elasticsearch 连接
        es = Elasticsearch(['http://127.0.0.1:9200'])
        logging.info("Elasticsearch client created.")
        
        # 构造删除请求
        delete_query = {
            "query": {
                "range": {
                    "edittime.raw": {
                        "lt": one_month_ago.strftime("%Y-%m-%dT%H:%M:%SZ")  # 格式化日期为Elasticsearch支持的格式
                    }
                }
            }
        }
        logging.info("Delete query constructed: %s", delete_query)
        
        # 发送删除请求,并等待完成
        response = es.delete_by_query(index='noticeresult', body=delete_query, wait_for_completion=True)
        logging.info("Delete request sent. Response: %s", response)
        
    except RequestError as e:
        logging.error("Error deleting documents: %s", e)

if __name__ == "__main__":
    delete_old_documents()
# 安装 模块
pip install elasticsearch
# 创建存放日志目录
mkdir /var/log/es-index/

创建索引命令

PUT /noticeresult
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0,
    "analysis": {
      "analyzer": {
        "htmlStripAnalyzer": {
          "filter": ["lowercase", "classic", "trim"],
          "char_filter": ["html_strip"],
          "type": "custom",
          "tokenizer": "standard"
        },
        "chinese_analyzer": {
          "type": "custom",
          "tokenizer": "ik_max_word"  // 使用 IK 分词器进行中文分词
        }
      },
      "char_filter": {
        "html_strip": {
          "type": "html_strip"
        }
      },
      "tokenizer": {
        "ik_max_word": {
          "type": "ik_max_word"
        }
      }
    }
  },
  "mappings": {
    "dynamic": "true",
    "_source": {
      "excludes": [
        "fujcontent",
        "projdetail"
      ]
    },
    "date_detection": false,
    "numeric_detection": false,  
    "properties": {
      "results_id": { 
	      "type": "integer",
        "fields": {
          "raw": {
            "type": "keyword",
            "null_value": "NULL",
            "ignore_above": 256
          }
        }
	  },
      "notice_num": {
   	    "type": "text", 
        "fields": {
          "raw": {
            "type": "keyword",
            "null_value": "NULL",
            "ignore_above": 256
          }
        }	  
	  },
      "organ": { "type": "text", "analyzer": "htmlStripAnalyzer" },
   	....
   	....
      "editip": { "type": "text", "analyzer": "htmlStripAnalyzer" },  // 使用中文分析器
      "editname": { "type": "keyword" },
      "putip": { "type": "keyword" },
      "edittime": {    
	    "type": "text",
        "fields": {
          "raw": {
            "type": "keyword",
            "null_value": "NULL",
            "ignore_above": 256
          }
        }
      },
  ....
  ....
      "urlhost": { 
       "type": "text",
        "fields": {
          "raw": {
            "type": "keyword",
            "null_value": "null",
            "ignore_above": 256
          }
        }
      },
      "attachment_info": { "type": "integer" }
    }
  }
}

创建索引时查看 edittime 字段的映射,这个字段是 text 类型,并且有一个 raw 子字段,类型是 keyword。
这意味着你可以在查询中使用 edittime.raw 来进行精确匹配查询。
对应上 上方 python 的精确匹配。

执行结果

举报

相关推荐

0 条评论