elasticsearch接口常用操作
es连接地址:192.168.71.134:9200
账号密码:elastic:changeme
查看查询使用的方法
curl elastic:changeme@192.168.71.134:9200/_cat
查看所有索引
curl elastic:changeme@192.168.71.134:9200/_cat/indices?v
新建索引
# -d {"settings":{"index":{"number_of_shards":"5","number_of_replicas":"1"}} 设置分片数量 不接该参数 则按照系统默认的分片设置 对于数据量大的情况合理设置分片能大幅提高性能
curl -X PUT -s elastic:changeme@192.168.71.134:9200/test_index \
> -H 'content-type: application/json' \
> -d '{"settings":{"index":{"number_of_shards":"5","number_of_replicas":"1"}}}' |jq
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test_index"
}
删除索引
# 使用 DELETE 方法完成索引删除操作
curl -X DELETE -s elastic:changeme@192.168.71.134:9200/test_index |jq
{
"acknowledged": true
}
索引别名
# 设置test_index2别名为 test_index
curl -X POST -s elastic:changeme@192.168.71.134:9200/_aliases -H 'content-type: application/json' \
> -d '{"actions":[{"add":{"index":"test_index2","alias":"test_index"}}]}' |jq
{
"acknowledged": true
}
# 删除别名
curl -X POST -s elastic:changeme@192.168.71.134:9200/_aliases -H 'content-type: application/json' \
> -d '{"actions":[{"remove":{"index":"test_index2","alias":"test_index"}}]}' |jq
{
"acknowledged": true
}
重建索引
# 用于更改主分片数的情况 如原索引设置分片数过小 可创建新的索引设置好分片后使用重建索引将原索引复制到新的索引
curl -X POST -s elastic:changeme@192.168.71.134:9200/_reindex -H 'content-type: application/json' \
-d '{"source": {"index": "ak_stock_zh_a_daily"},"dest": {"index": "ak_stock_zh_a_daily_reindex"}}' |jq
# 重建索引后则 ak_stock_zh_a_daily_reindex数据与ak_stock_zh_a_daily一致
查询索引信息
#查看所有 数据过多 不显示
curl -X GET -s elastic:changeme@192.168.71.134:9200/ak_stock_zh_a_daily_s5 |jq
# 查看 mappings (查看数据中有哪些字段)
curl -X GET -s elastic:changeme@192.168.71.134:9200/ak_stock_zh_a_daily_s5/_mappings |jq
#查询索引基础信息 分片 uuid等
curl -X GET -s elastic:changeme@192.168.71.134:9200/ak_stock_zh_a_daily_s5/_settings |jq
{
"ak_stock_zh_a_daily_s5": {
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "5",
"provided_name": "ak_stock_zh_a_daily_s5",
"creation_date": "1630991943130",
"number_of_replicas": "1",
"uuid": "b1uOQBTEQF-O4QRAsgGzMg",
"version": {
"created": "7140199"
}
}
}
}
}
查询操作
# 传入查询语句
curl -X POST -s elastic:changeme@192.168.71.134:9200/stock_basic/_search -H 'content-type: application/json' \
> -d '{"query":{"match_all":{}}}' |jq
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 739,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "stock_basic",
"_type": "record",
"_id": "U5grvnsBhXANud1Uhbbk",
"_score": 1,
"_ignored": [
"profile.keyword"
],
"_source": {
"index": 0,
"ts_code": "000001.SZ",
"symbol": "000001",
"name": "平安银行",
"area": "深圳",
"industry": "银行",
"market": "主板",
"list_date": "19910403",
"circ_mv": 31806028.6425,
"pe_ttm": 11.2808,
"ps": 2.7251,
"pb": 1.1649,
"concept": "供应链金融|MSCI|银行",
"concept_code": "TS123",
"start20190930": 4,
"start20190930k": 1,
"start20190930S": 8,
"profile": "主营业务:\n经有关监管机构批准的各项商业银行业务\n产品类型:\n商业银行业务\n产品名称:\n吸收公众存款 、 发放短期 、 中期和长期贷款 、 办理国内外结算 、 办理票据承兑与贴现 、 发行金融债券 、 代理发行 、 代理兑付 、 承销政府债券 、 买卖政府债券 、 金融债券 、 从事同业拆借 、 买卖 、 代理买卖外汇 、 从事银行卡业务 、 提供信用证服务及担保 、 代理收付款项及代理保险业务 、 提供保管箱服务 、 结汇 、 售汇业务 、 离岸银行业务 、 资产托管业务 、 办理黄金业务 、 财务顾问 、 资信调查 、 咨询 、 见证业务 、 经有关监管机构批准的其他业务\n经营范围:\n吸收公众存款;发放短期、中期和长期贷款;办理国内外结算;办理票据承兑与贴现;发行金融债券;代理发行、代理兑付、承销政府债券;买卖政府债券、金融债券;从事同业拆借;买卖、代理买卖外汇;从事银行卡业务;提供信用证服务及担保;代理收付款项及代理保险业务;提供保管箱服务;结汇、售汇业务;离岸银行业务;资产托管业务;办理黄金业务;财务顾问、资信调查、咨询、见证业务;经有关监管机构批准的其他业务。"
}
},
备份迁移工具
小数据导入导出参考:
elasticdump:https://www.jianshu.com/p/8c5faa916c08