| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jqvj9967
8年前发布

Elasticsearch 2.20入门篇:基本操作

来自: http://my.oschina.net/secisland/blog/613927


    前面我们已经安装了Elasticsearch ,下一步我们要对Elasticsearch进行一些基本的操作。基本的操作主要有,建索引库,插入数据,查询数据,修改数据,删除数据,删除索引库。

    备注:如果没有特殊说明,本文章及后面所有的文章都在2.20版本中进行验证,其他版本不能确定是否可用。

    由于官方文档都是使用curl来进行实例操作,不太直观,我更喜欢用图形化界面来进行验证。在本文及以后的例子中,我都是已RESTClient3.5来作为操作的工具。下载地址为http://code.fosshub.com/WizToolsorg-RESTClient/downloads,下载的文件是restclient-ui-3.5-jar-with-dependencies.jar。

程序运行: java -jar restclient-ui-3.5-jar-with-dependencies.jar

建索引库

执行PUT localhost:9200/customer?pretty

返回表示建库成功:

{    "acknowledged" : true  }

说明:http方法PUT,url为localhost:9200/customer?pretty

查询库

执行GET http://localhost:9200/_cat/indices?v

返回:

health status   index      pri  rep       docs.count docs.deleted store.size pri.store.size 

yellow open   customer   5   1          0                 0                   795b        795b 

表示已经建成了一个索引customer,主分片是5个,健康度是黄色,状态是活动,文档数为0。

插入数据

执行 PUT localhost:9200/customer/external/1?pretty

参数:{  "name": "John Doe" }

注意:Method选择PUT,Body要设置成application/x-www-form-urlencoded; charset=UTF-8

返回值为:

{    "_index" : "customer",    "_type" : "external",    "_id" : "1",    "_version" : 1,    "_shards" : {      "total" : 2,      "successful" : 1,      "failed" : 0    },    "created" : true  }

界面如下:

我们再次执行库查询,发现文档数是1:GET http://localhost:9200/_cat/indices?v  

health status  index      pri rep        docs.count docs.deleted store.size pri.store.size 

yellow open   customer   5   1          1            0      3.5kb          3.5kb 

查询数据

执行:GET http://localhost:9200/customer/external/1?pretty

返回:

{    "_index" : "customer",    "_type" : "external",    "_id" : "1",    "_version" : 1,    "found" : true,    "_source" : {      "name" : "John Doe"    }  }

可以看到_source的内容就是我们刚才插入的数据。

本文由赛克蓝德(secisland)原创,转载请标明作者和出处。

修改数据

执行:POST localhost:9200/customer/external/1/_update?pretty

参数:

{    "doc": { "name": "secisland Doe" }  }

返回结果:

{    "_index" : "customer",    "_type" : "external",    "_id" : "1",    "_version" : 2,    "_shards" : {      "total" : 2,      "successful" : 1,      "failed" : 0    }  }

表示执行成功。

然后我们在查询一下数据

GET http://localhost:9200/customer/external/1?pretty

{    "_index" : "customer",    "_type" : "external",    "_id" : "1",    "_version" : 2,    "found" : true,    "_source" : {      "name" : "secisland Doe"    }  }

可以看出文档的内容由John Doe修改成了secisland Doe。

删除文档

执行:DELETE localhost:9200/customer/external/1?pretty

返回:

{    "found" : true,    "_index" : "customer",    "_type" : "external",    "_id" : "1",    "_version" : 3,    "_shards" : {      "total" : 2,      "successful" : 1,      "failed" : 0    }  }

然后我们查询库的状态:

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

返回:

health status index    pri rep docs.count docs.deleted store.size pri.store.size 

yellow open   customer   5   1          0            0      3.6kb          3.6kb 

从中可以看出,数据库中已经没有记录了。

删除索引库

执行:DELETE localhost:9200/customer?pretty

返回:

{    "acknowledged" : true  }

表示删除成功

然后我们查询库的状态:

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

返回:

health status index pri rep docs.count docs.deleted store.size pri.store.size 

从中可以看出已经没有任何的库了。

赛克蓝德(secisland)后续会逐步对Elasticsearch的最新版本的各项功能进行分析,近请期待。

 本文由用户 jqvj9967 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1454632849151.html
ElasticSearch 搜索引擎