这篇 ElasticSearch 详细使用教程,内部分享时被老大表扬了

这篇 ElasticSearch 详细使用教程,内部分享时被老大表扬了

一、快速入门

1.查看集群的健康状况

http://localhost:9200/_cat

图片

http://localhost:9200/_cat/health?v

图片

说明:v是用来要求在结果中返回表头

#状态值说明

Green - everything is good (cluster is fully functional),即最佳状态
Yellow - all data is available but some replicas are not yet allocated (cluster is fully functional),即数据和集群可用,但是集群的备份有的是坏的
Red - some data is not available for whatever reason (cluster is partially functional),即数据和集群都不可用
  • 查看集群的节点

http://localhost:9200/_cat/?v

图片

2. 查看所有索引

http://localhost:9200/_cat/indices?v

图片

3. 创建一个索引

创建一个名为 customer 的索引。pretty要求返回一个漂亮的json 结果

PUT /customer?pretty

图片

再查看一下所有索引(超详细 116 页 Elasticsearch 实战文档!高清可下载

http://localhost:9200/_cat/indices?v

图片

GET /_cat/indices?v

图片

4. 索引一个文档到customer索引中
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'
5. 从customer索引中获取指定id的文档
curl -X GET "localhost:9200/customer/_doc/1?pretty"
6. 查询所有文档
GET /customer/_search?q=*&sort=name:asc&pretty

JSON格式方式(ElasticSearch 亿级数据检索案例实战!

GET /customer/_search
{
  "query": { "match_all": {} },
  "sort": [
    {"name": "asc" }
  ]
}

二、索引管理

图片

图片

1. 创建索引

创建一个名为twitter的索引,设置索引的分片数为3,备份数为2。注意:在ES中创建一个索引类似于在数据库中建立一个数据库(ES6.0之后类似于创建一个表)

PUT twitter
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,
            "number_of_replicas" : 2
        }
    }
}

说明:

  • 默认的分片数是5到1024
  • 默认的备份数是1
  • 索引的名称必须是小写的,不可重名

创建结果:

图片

创建的命令还可以简写为

PUT twitter
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}
2. 创建mapping映射

注意:在ES中创建一个mapping映射类似于在数据库中定义表结构,即表里面有哪些字段、字段是什么类型、字段的默认值等;也类似于solr里面的模式schema的定义

PUT twitter
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,
            "number_of_replicas" : 2
        }
    },
   "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}
3. 创建索引时加入别名定义
PUT twitter
{
    "aliases" : {
        "alias_1" : {},
        "alias_2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        }
    }
}
4. 创建索引时返回的结果说明

图片

5. Get Index 查看索引的定义信息

GET /twitter,可以一次获取多个索引(以逗号间隔)获取所有索引_all或用通配符*

图片

GET /twitter/_settings

图片

GET /twitter/_mapping

图片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值