Elastic Search常用命令

本文介绍了Elasticsearch的基本操作,包括集群状态查看、索引创建、数据查询与管理等核心功能。通过具体的curl命令示例,帮助读者快速掌握Elasticsearch的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ES的基本指令:
1. 查看es的集群状态:
curl 'IP:9200/_cat/health?v'
注释:?v表示格式化输出
2. 查看节点列表
curl 'IP:9200/_cat/nodes?v'
3.查询所有索引及数据大小
curl 'IP:9200/_cat/indices?v'
 
4.创建索引(名称为studentIndex)并指定分片数和备份数
curl -XPUT http://localhost:9200/studentIndex -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}'

5.创建索引studentIndex,并添加类型student,并指定分词是studentname
curl -XPUT 'http://localhost:9200/studentIndex' -d '
{
  "mappings": {
    "student": {
      "properties": {
        "studentname": {
          "type": "string"
        }
      }
    }
  }
}'


ES和mysql的对应关系
MySql        ElasticSearch
database  -- index
table        -- type
row          -- document
cloumn    -- field
schema   -- mapping
index       -- Everything is indexed
slect * from...   --- get http://...
update talbe set... -- put http://...


6.查询索引是studentIndex,type是student,studentname字段的值是“李”的信息,默认返回第一页,10条数据

http://localhost:9200/studentIndex/student/_search?q=studentname:李


{
    "took": 32,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 645161,
        "max_score": 13.882782,
        "hits": [{
                "_index": "studentIndex",
                "_type": "student",
                "_id": "AWNIsqEX4aqkPyNgQr06",
                "_score": 13.882782,
                "_source": {
                    "studentname": "李李四"
                }
            },
            {
                "_index": "studentIndex",
                "_type": "student",
                "_id": "AWNIsqEX4aqkPyNgQr06",
                "_score": 13.23425,
                "_source": {
                    "studentname": "李五"
                }
            }
        ]
    }
}


7.使用post查询elastic search

curl -X POST \
http://1ip:9200/studentIndex/student/_search 
  -d '{
    "query": {
        "match": {
            "studentname": "李"
        }
    },
    "from": 100,  // 第几页开始
    "size": 10   // 每页的大小
}'

返回结果同上:

注释:
在url中的q=* 相等于body中的
{
    "query": { "match_all": {} }
  }


8.查询数据总数:http://localhost:9200/studentIndex/student/_count

9.
a.插入数据:
curl -X POST 'localhost:9200/studentIndex/student?pretty' -d' {"studentname": "Jane Doe" }'
b.指定数据的Id是id1
curl -X POST 'localhost:9200/studentIndex/student/id1?pretty' -d' {"studentname": "John Doe" }'
10 删除数据
a.删除studentIndex中ID为2的数据
curl -X DELETE 'localhost:9200/studentIndex/student/2?pretty'

b.根据条件删除数据
curl -X POST 'localhost:9200/studentIndex/student/_delete_by_query?pretty' -d '{
    "query": {
        "match": {
            "studentname": "John"
        }
    }
}'
11.
修改id=1的name属性,并直接增加属性和属性值
curl -X POST 'localhost:9200/studentIndex/student/1/_update?pretty' -d ' {
    "doc": {
        "studentname": "xyd" 
    }
}'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值