以下所用版本为Elasticsearch 7.2.0

1.安装

pip3 install elasticsearch -i https://pypi.tuna.tsinghua.edu.cn/simple

2.连接ES

es = Elasticsearch([{'host': '127.0.0.1', 'port': 9200}])

3.创建index

index = 'index_tushare'
body = {
"mappings": {
"properties": {
"ts_code": {
"type": "keyword"
},
"symbol": {
"type": "keyword"
},
"ts_name": {
"type": "keyword"
},
"fullname": {
"type": "keyword"
},
"area": {
"type": "keyword"
},
"industry": {
"type": "keyword"
},
"list_date": {
"type": "keyword"
},
"tab_name": {
"type": "keyword"
}
}
}
}
# create an index in elasticsearch, ignore status code 400 (index already exists)
es.indices.create(index=index, body=body, ignore=400)

4.创建mapping之后,添加字段

index_daily_close = 'index_daily_close'
body_daily_close = {
"mappings": {
"properties": {
"trade_date": {
"type": "keyword"
}
}
}
}
es.indices.create(index=index_daily_close, body=body_daily_close, ignore=400) properties = body_daily_close.get("mappings").get("properties") def daily_close_add_tscodes(tscode):
properties.setdefault(tscode, {"type": "keyword"})
es.indices.put_mapping(index=index_daily_close, body=body_daily_close.get("mappings"))
print(body_daily_close.get("mappings"))

添加字段的时候要注意,elasticsearch默认最大字段数为1000,超过1000就会报错

{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Limit of total fields [1000] in index [index_daily_close] has been exceeded"
}
],
"type": "illegal_argument_exception",
"reason": "Limit of total fields [1000] in index [index_daily_close] has been exceeded"
},
"status": 400
}

这里的1000是整个节点不能超过1000,我尝试了在多个索引里面添加字段,最终出现的结果是:

以上实验的方式是在第一个表中500个,然后第二个表中500个,在第998个的时候报错了。加上每个表本身有的 trade_date 字段,刚好加起来1000个。然后报错在了第1001个


Python Elasticsearch的更多相关文章

  1. Python Elasticsearch api,组合过滤器,term过滤器,正则查询 ,match查询,获取最近一小时的数据

    Python Elasticsearch api   描述:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.下 ...

  2. Python Elasticsearch api

    描述:ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.下面介绍了利用Python API接口进行数据查询,方便 ...

  3. Python Elasticsearch批量操作客户端

    基于Python实现的Elasticsearch批量操作客户端 by:授客 QQ:1033553122   1. 代码用途 1 2. 测试环境 1 3. 使用方法 1 3.1 配置ES服务器信息 1 ...

  4. python elasticsearch 批量写入数据

    from elasticsearch import Elasticsearch from elasticsearch import helpers import pymysql import time ...

  5. Elasticsearch安装配置

    文档地址: https://www.elastic.co/guide/en/elasticsearch/reference/6.5/setup.html 官方页面提供自0.9版本以来的说明文档,由于我 ...

  6. django学习系列——python和php对比

    python 和 php 我都是使用过,这里不想做一个非常理性的分析,只是根据自己的经验谈一下感想. 在web开发方面,无疑 php 更甚一筹. 从某种角度来说,php 就是专门为 web 定制的语言 ...

  7. ELK系列三:Elasticsearch的简单使用和配置文件简介

    1.定义模板创建索引: 首先定义好一个模板的例子 { "order":14, "template":"ids-1", "state ...

  8. linux 安装elasticsearch

    一.检测是否已经安装的elasticsearch ps aux|grep elasticsearch. 二.下载elasticsearch.tar.gz并上传至服务器usr/local/文件夹下 三. ...

  9. 工作笔记 之 Python应用技术

    python socket编程详细介绍 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket,建立网络通信连接至少要一对端口号(socket). Socket本质是 ...

随机推荐

  1. lvs+keepalived做高可用方案1

    本文我们主要讲解的是LVS通过keepalived来实现负载均衡和高可用,而不是我们第三篇文章介绍的通过手动的方式来进行配置.通过脚本的方式来显示RS节点的健康检查和LVS的故障切换.此文会通过一个实 ...

  2. python 获取运行脚本和模块的绝对路径

    方法一:sys.args[0] 在python的运行时,sys.argv[0],存了当前脚本的运行路径包括文件名 python test.py 则:sys.argv[0] =>test.py p ...

  3. P3474 [POI2008]KUP-Plot purchase

    思路:单调栈 提交:>5次 错因:单调栈写法有问题+前缀和写错 题解: 若有\(>=k\ \&\&\ <=2\times k\)的点,显然直接选他就行了. 否则,我们 ...

  4. fmt

    fmt.Println("hello world") fmt.Printf("%T",a)打印a 的类型 fmt.Printf("%v",a ...

  5. 【概率论】3-1:随机变量和分布(Random Variables and Discrete Distributions)

    title: [概率论]3-1:随机变量和分布(Random Variables and Discrete Distributions) categories: Mathematic Probabil ...

  6. scrapy框架之Selectors选择器

    Selectors(选择器) 当您抓取网页时,您需要执行的最常见任务是从HTML源中提取数据.有几个库可以实现这一点: BeautifulSoup是Python程序员中非常流行的网络抓取库,它基于HT ...

  7. 三十、CentOS 7之systemd

    一.系统启动流程 POST --> bootloader  --> MBR工作 --> kernel(initramfs/initrd) --> ro rootfs --> ...

  8. input上传mp3格式文件,预览并且获取时间

    <input type="file" id="file" name="file" class="upfile" o ...

  9. 谈谈Java对象的强引用,软引用,弱引用,虚引用分别是什么

    整体结构 java提供了4中引用类型,在垃圾回收的时候,都有自己的各自特点. 为什么要区分这么多引用呢,其实这和Java的GC有密切关系. 强引用(默认支持模式) 把一个对象赋给一个引用变量,这个引用 ...

  10. java并发编程--第一章并发编程的挑战

    一.java并发编程的挑战 并发编程需要注意的问题: 并发编程的目的是让程序运行的更快,然而并不是启动更多的线程就能让程序最大限度的并发执行.若希望通过多线程并发让程序执行的更快,会受到如下问题的挑战 ...