当前位置:首页 > 日记本 > 正文内容

elasticsearch老数据库新建索引python代码

zhangchap1年前 (2023-04-01)日记本235
from elasticsearch import Elasticsearch

# 连接到本地Elasticsearch实例,默认端口为9200
es = Elasticsearch()

# 创建一个新的索引名
new_index_name = 'new_cars'
old_index_name = 'my_index'
# 定义映射和设置分词器
mapping = {
   "mappings": {
      "properties": {
         "title": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart"},
         "content": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart"}
      }
   }
}

# 创建新的索引,并将cars索引中的数据复制到新索引
es.indices.create(index=new_index_name, body=mapping)
es.reindex(body={"source": {"index": old_index_name}, "dest": {"index": new_index_name}})

# 删除原来的cars索引
es.indices.delete(index=old_index_name)

总结:新建个索引及分词,然后把以前的老数据复制进去,然后把老数据删除

分享给朋友:

相关文章

更换服务器需要设置的几点

此设置只针对个人习惯,因为需要开通的网站及服务器较多,做个记录!就不公开了 此设置只针对个人习惯,因为需要开通的网站及服务器较多,做个记录!就不公开了 此设置只针对个人习惯,因为需要开...

lxml win 安装方法

文档地址:https://lxml.de/installation.htmlwindows 下安装不了的都可以在这里找:https://www.lfd.uci.edu/~gohl...

python 函数 开启多线程示例

from threading import Thread def readfile(queue:Queue):    &nbs...

python jieba分词

import jieba from jieba.analyse import tfidf words = jieba.lcut('...

python读取txt文件放到Queue队列

from queue import Queue with open('kw.txt',encoding='utf-8')&nb...

python fake_useragent 模块用法

我们每次发送requests请求时通过random从中随机获取一个随机UserAgent,两行代码即可完成UserAgent的不停更换 from fake_useragent i...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。