等待下一个秋

  • Spark
  • Flink
  • Hive
  • 数据仓库
  • ClickHouse
  • 收徒弟
  • Java
    • Spring
    • Mybatis
    • SpringBoot
    • 面试题
  • Python
    • Python基础
    • 爬虫
    • Numpy
    • matplotlib
    • Flask
  • 技术杂谈
    • Linux知识
    • Docker
    • Git教程
    • Redis教程
    • mysql
    • 前端
    • R语言
    • 机器学习
  • 关于我
  • 其它
    • 副业挣钱
    • 资料下载
    • 资料文档
专注于Hadoop/Spark/Flink/Hive/数据仓库等
关注公众号:大数据技术派,获取更多学习资料。
  1. 首页
  2. 未分类
  3. 正文

curl操作elasticsearch常用命令

2022年7月5日 1672点热度 0人点赞 0条评论

说明:这些命令针对es7.x,6 以下版本可能无效,elastic 6 / 7不同版本命令差别较大。。

创建索引

这里创建索引名为fc_search,type和data是关键字类型,也就是不进行分词,keyword_1是text类型,默认分词,并且指定分词方式为ik_smart。

curl -u elastic:changeme -X PUT  http://192.168.20.130:9200/fc_search -H 'Content-Type: application/json' -d'
{
  "settings" : {
      "number_of_shards" : 1,
      "number_of_replicas" : 1
   },
    "mappings" : {
        "properties": {
          "id": {
            "type" : "long"
          }, 
          "type": {
            "type": "long"
          }, 
          "keyword_1": {
            "type": "text",
            "analyzer" : "ik_max_word"
          }, 
          "keyword_2": {
            "type": "text",
            "analyzer" : "ik_max_word"
          },
          "keyword_3": {
            "type": "text",
            "analyzer" : "ik_max_word"
          },
          "data": {
            "type": "keyword"
          },
          "created_at": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
          }, 
          "updated_at": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
          }
      }
    }
}'

添加文档

curl -u elastic:changeme -X POST  http://192.168.20.130:9200/fc_search/_doc/1 -H 'Content-Type: application/json' -d'
{
  "id": 10000,
  "data": "data",
  "keyword_1": "蓝色渐变时尚家居通用PPT模板,弧形,家居,地板,装修,蓝色,曲线,通用,家居,时尚,其他"
}'

说明:http://192.168.20.130:9200/fc_search/_doc/1?pretty, 这里的_doc表示类型,1表示doc_id 。

查询

  1. 查询所有

    curl -u elastic:changeme -X GET  http://192.168.20.130:9200/fc_search/_search

    结果:

    {"_index":"fc_search","_type":"_doc","_id":"5_1","_version":1,"_seq_no":5,"_primary_term":1,"found":true,"_source":{"id":1,"type":5,"keyword_1":"测试1","keyword_2":"测试2","keyword_3":"测试3"}}
  2. 根据文档id查询

    curl -u elastic:changeme -X GET  http://192.168.20.130:9200/fc_search/_doc/5_1

    结果不美观,es里面查询如果想让json结果规整,可以末尾加?pretty

    curl -u elastic:changeme -X GET  http://192.168.20.130:9200/fc_search/_doc/5_1?pretty

    结果:

    {
    "_index" : "fc_search",
    "_type" : "_doc",
    "_id" : "5_1",
    "_version" : 1,
    "_seq_no" : 5,
    "_primary_term" : 1,
    "found" : true,
    "_source" : {
    "id" : 1,
    "type" : 5,
    "keyword_1" : "测试1",
    "keyword_2" : "测试2",
    "keyword_3" : "测试3"
    }
    }
  3. 根据关键字查询(模糊匹配)

    curl -u elastic:changeme -X POST  http://192.168.20.130:9200/fc_search/_search?pretty -H 'Content-Type: application/json' -d'
    {
    "query": {
        "match": {
            "keyword_1.keyword": "蓝色,简约"
        }
    }
    }'

    这种方式查询,是模糊匹配,忽略分词。

  4. 根据分词查询

    curl -u elastic:changeme -X POST  http://192.168.20.130:9200/fc_search/_search?pretty -H 'Content-Type: application/json' -d'
    {
    "from": 0,
    "size": 20,
    "query": {
        "bool": {
            "should": [{
                    "match": {
                        "keyword_1": "蓝色,简约"
                    }
                }
            ]
        }
    }
    }'

    keyword_1是被分词了的,这种方式是按照分词匹配,搜索关键词也会被分词为:

修改索引(给字段添加分词属性)

创建索引fc_search_copy

curl -u elastic:changeme -X PUT  http://192.168.20.130:9200/fc_search_copy -H 'Content-Type: application/json' -d'
{
  "settings" : {
      "number_of_shards" : 1,
      "number_of_replicas" : 1
   },
    "mappings" : {
        "properties": {
          "id": {
            "type" : "long"
          }, 
          "type": {
            "type": "long"
          }, 
          "keyword_1": {
            "type": "text",
            "analyzer" : "ik_max_word"
          }, 
          "keyword_2": {
            "type": "text",
            "analyzer" : "ik_max_word"
          },
          "keyword_3": {
            "type": "text",
            "analyzer" : "ik_max_word"
          },
          "data": {
            "type": "keyword"
          },
          "created_at": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
          }, 
          "updated_at": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
          }
      }
    }
}'

查看新建立的索引

curl -u elastic:changeme -X GET  http://192.168.20.130:9200/fc_search_copy/_mapping

同步数据到新的索引

curl -u elastic:changeme -X POST  http://192.168.20.130:9200/_reindex -H 'Content-Type: application/json' -d'
{
  "source": {
    "index": "fc_search"
  },
  "dest": {
    "index": "fc_search_copy"
  }
}'

删除旧的索引

curl -u elastic:changeme -X DELETE  http://192.168.20.130:9200/fc_search

新的索引添加别名

curl -u elastic:changeme -X POST http://192.168.20.130:9200/_aliases -H 'Content-Type: application/json' -d'
{
  "actions": [
    {
      "add": {
        "index": "fc_search_copy",
        "alias": "fc_search"
      }
    }
  ]
}'

思路是将旧索引数据复制一份到新的索引,然后删除旧索引,最后将新索引添加别名,别名就是旧索引的名字,这样不会影响原来的代码。

常用命令

  1. 获取es集群信息,包括版本和集群名称等。

    curl -u elastic:changeme -X GET  http://192.168.20.130:9200

    获取到的信息如下:

    {
    "name" : "1653018488005162032",
    "cluster_name" : "es-lp827qju",
    "cluster_uuid" : "UuxuP35SQjqInlTs0cNDkA",
    "version" : {
    "number" : "7.14.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "da023f0226db21490ed0b4bb422b13d8f1926863",
    "build_date" : "2022-04-14T08:25:18.177087482Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
    },
    "tagline" : "You Know, for Search"
    }
  2. 查看索引mapping即字段属性信息:

    http://192.168.20.130:9200/fc_search_test/_mapping

    结果:

    {
        "fc_search_test": {
            "mappings": {
                "properties": {
                    "created_at": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
                    },
                    "data": {
                        "type": "keyword"
                    },
                    "id": {
                        "type": "long"
                    },
                    "keyword_1": {
                        "type": "text",
                        "analyzer": "ik_max_word"
                    },
                    "keyword_2": {
                        "type": "text",
                        "analyzer": "ik_max_word"
                    },
                    "keyword_3": {
                        "type": "text",
                        "analyzer": "ik_max_word"
                    },
                    "type": {
                        "type": "long"
                    },
                    "updated_at": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
                    }
                }
            }
        }
    }
  3. 查询全部数据

    http://192.168.20.130:9200/fc_search_test/_search
  4. 清空索引数据

    curl -u elastic:changeme -X POST http://192.168.20.130:9200/ppt_content/_delete_by_query -H 'Content-Type: application/json' -d'
    {
    "actions": [
    {
      "query": {
            "match_all": {}
          }
    }
    ]
    }'
  5. 根据文档id查询数据

    curl -u elastic:changeme -X GET  http://192.168.20.130:9200/fc_search/_doc/5_5?pretty
  6. 给索引添加别名

    curl -u elastic:changeme -X POST http://192.168.20.130:9200/_aliases -H 'Content-Type: application/json' -d'
    {
    "actions": [
    {
      "add": {
        "index": "fc_search_copy",
        "alias": "fc_search"
      }
    }
    ]
    }'

    查询时候,索引名跟索引别名效果是一样的。

  7. 删除索引

    curl -u elastic:changeme -X DELETE  http://192.168.20.130:9200/fc_search_test
标签: Elasticsearch
最后更新:2022年7月5日

等待下一个秋

待我代码写成,便娶你为妻!专注于Hadoop/Spark/Flink/Hive/数据仓库等,关注公众号:大数据技术派,获取更多学习资料。

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

等待下一个秋

待我代码写成,便娶你为妻!专注于Hadoop/Spark/Flink/Hive/数据仓库等,关注公众号:大数据技术派,获取更多学习资料。

搜一搜
微信
最新 热点 随机
最新 热点 随机
深入理解ClickHouse跳数索引(二级索引) ClickHouse主键索引(一级稀疏索引)最佳实践 Java和Python操作Clickhouse ChatGPT 注册教程 ChatGPT可以做什么 ClickHouse 自定义分区键
SpringBoot连接Hive实现自助取数 R语言学习之矩阵 黑客与画家 14.梦寐以求的编程语言 ClickHouse主键索引(一级稀疏索引)最佳实践 mysql索引不生效 第10讲:Flink Side OutPut 分流
标签聚合
mysql Redis 挣钱 Hive 书籍 大数据 数据仓库 Flink R语言 Java Python 算法
文章归档
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2022年12月
  • 2022年11月
  • 2022年9月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年5月
  • 2020年4月
  • 2020年1月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年1月
  • 2018年12月
  • 2017年5月

©2022 ikeguang.com. 保留所有权利。

鄂ICP备2020019097号-1

鄂公网安备 42032202000160号