Elasticsearch入门

小诗欣赏:
小池
宋代杨万里

泉眼无声惜细流,树阴照水爱晴柔。
小荷才露尖尖角,早有蜻蜓立上头。

正文

配置crul环境变量:
E:\software\curl-7.60.0\I386

622142330.png

测试curl配置正确性
curl http:///www.baidu.com
返回:
22142552.png

查看所有索引

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

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   website  tIG5OrIXR4W8MrivVZ87RA   5   1          2            0     10.1kb         10.1kb
yellow open   megacorp 356W-zR3RAS9wmFGtemXOA   5   1          3            0     17.4kb         17.4kb

查看节点

curl 'localhost:9200/_cat/nodes?v'

ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           29          49   9                          mdi       *      PoIa-5T

查看集群健康程度

curl 'localhost:9200/_cat/health?v'

epoch      timestamp cluster           status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1530061690 09:08:10  bruce-application yellow          1         1     10  10    0    0       10             0                  -                 50.0%

查看版本

curl http://localhost:9200/?pretty

{
  "name" : "PoIa-5T",
  "cluster_name" : "bruce-application",
  "cluster_uuid" : "FfI8hS6cSaC7e67V2nejhA",
  "version" : {
    "number" : "6.3.0",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "424e937",
    "build_date" : "2018-06-11T23:38:03.357887Z",
    "build_snapshot" : false,
    "lucene_version" : "7.3.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

文档操作

1.新增文档:
curl -H "Content-Type: application/json" -XPOST localhost:9200/megacorp/employee/1 -d "{"first_name":"Jane","last_name":"Smith","age":25,"about":"I like to collect rock albums","interests":["music"]}"
或者
curl -H "Content-Type: application/json" -XPUT localhost:9200/megacorp/employee/1 -d "{"first_name":"Jane","last_name":"Smith","age":25,"about":"I like to collect rock albums","interests":["music"]}"

2.修改文档

  • curl -H "Content-Type: application/json" -XPUT localhost:9200/megacorp/employee/1 -d "{"first_name":"Jane","last_name":"Smith","age":25,"about":"I like to collect rock albums","interests":["music and pinpang"]}"
    如果更新想检查对应的id,然后再做操作有如下两种方法:

PUT /website/blog/123?op_type=create
{ ... }

PUT /website/blog/123/_create
{ ... }

  • 基于版本的更新

内部版本更新
PUT /website/blog/1?version=1
{
"title": "My first blog entry",
"text": "Starting to get the hang of this..."
}

外部版本更新
PUT /website/blog/2?version=5&version_type=external
{
"title": "My first external blog entry",
"text": "Starting to get the hang of this..."
}

如果更新版本比当前版本低回报错如下:

{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[blog][2]: version conflict, current version [10] is higher or equal to the one provided [1]",
                "index_uuid": "tIG5OrIXR4W8MrivVZ87RA",
                "shard": "2",
                "index": "website"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[blog][2]: version conflict, current version [10] is higher or equal to the one provided [1]",
        "index_uuid": "tIG5OrIXR4W8MrivVZ87RA",
        "shard": "2",
        "index": "website"
    },
    "status": 409
}
  • 局部更新

POST /website/blog/1/_update
{
"doc" : {
"tags" : [ "testing" ],
"views": 0
}
}
查询该文档curl localhost:9200/website/blog/2

{
    "_index": "website",
    "_type": "blog",
    "_id": "2",
    "_version": 13,
    "found": true,
    "_source": {
        "title": "My first blog entry",
        "text": "Starting to get the hang of this...",
        "views": 0,
        "tags": [
            "testing"
        ]
    }
}

4.检查文档是否存在
curl -i -XHEAD http://localhost:9200/megacorp/employee/1
返回200表示找到,404未找到

5.查询文档

{
  "_index" : "megacorp",
  "_type" : "employee",
  "_id" : "1",
  "_version" : 9,
  "found" : true,
  "_source" : {
    "first_name" : "Jane",
    "last_name" : "Smith",
    "age" : 25,
    "about" : "I like to collect rock albums",
    "interests" : [
      "music and pinpang"
    ]
  }
}
{
  "_index" : "megacorp",
  "_type" : "employee",
  "_id" : "1",
  "_version" : 9,
  "found" : true,
  "_source" : {
    "interests" : [
      "music and pinpang"
    ],
    "first_name" : "Jane"
  }
}

或者你只想得到_source字段而不要其他的元数据,你可以这样请求:
curl http://localhost:9200/megacorp/employee/1/_source?pretty

{
  "first_name" : "Jane",
  "last_name" : "Smith",
  "age" : 25,
  "about" : "I like to collect rock albums",
  "interests" : [
    "music and pinpang"
  ]
}
  • _mget查询
    POST localhost:9200/website/blog/_mget
    {
    "ids": ["1","2"]
    }
    result:
{
    "docs": [
        {
            "_index": "website",
            "_type": "blog",
            "_id": "1",
            "_version": 4,
            "found": true,
            "_source": {
                "title": "My first blog entry",
                "text": "Just trying this out..."
            }
        },
        {
            "_index": "website",
            "_type": "blog",
            "_id": "2",
            "_version": 13,
            "found": true,
            "_source": {
                "title": "My first blog entry",
                "text": "Starting to get the hang of this...",
                "views": 0,
                "tags": [
                    "testing"
                ]
            }
        }
    ]
}

POST localhost:9200/website/blog/_mget
{
"docs" : [
{ "_id" : 2 }
]
}
result:

{
    "docs": [
        {
            "_index": "website",
            "_type": "blog",
            "_id": "2",
            "_version": 13,
            "found": true,
            "_source": {
                "title": "My first blog entry",
                "text": "Starting to get the hang of this...",
                "views": 0,
                "tags": [
                    "testing"
                ]
            }
        }
    ]
}

6.删除文档
curl -XDELETE http://localhost:9200/megacorp/employee/1

参考文档:https://es.xiaoleilu.com/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 227,572评论 6 531
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 98,071评论 3 414
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 175,409评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,569评论 1 307
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,360评论 6 404
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,895评论 1 321
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,979评论 3 440
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,123评论 0 286
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,643评论 1 333
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,559评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,742评论 1 369
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,250评论 5 356
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,981评论 3 346
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,363评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,622评论 1 280
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,354评论 3 390
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,707评论 2 370

推荐阅读更多精彩内容

  • 基础概念 Elasticsearch有几个核心概念,从一开始理解这些概念会对整个学习过程有莫大的帮助。 接近实时(...
    山天大畜阅读 2,113评论 0 4
  • 介绍查询语言 Elasticsearch提供一种JSON风格的特定领域语言,利用它你可以执行查询。这杯称为查询DS...
    山天大畜阅读 3,747评论 0 1
  • elasticsearch使用--简单入门(一) 一、下载安装 安装java 下载elasticsearch相应版...
    ifeelok0319阅读 325评论 0 0
  • 水龙头生锈滴滴苍凉 牙刷并脸盆占山为王 吾乃天命之子 横刀立马望天狼 你在噩梦萦绕的大清早赤着脚笑容灿烂 我在日出...
    冰楼阅读 154评论 0 0
  • socket.io-redis 如何去使用 通过使用socket.io-redis 适配器运行socket.io ...
    ZZES_ZCDC阅读 7,544评论 0 5