45321 发表于 2016-5-13 11:14:20

增删改查 elasticsearch中的文档API 的使用



1
2
curl 192.168.100.10:9200?preety
curl 192.168.100.10:9200/_count?pretty





shell 中的curl的用法
-X 指定请求方法默认是-XGET
-i 返回数据的时候,也一并返回请求结果
-d 发送的数据


这个返回值意味着我们的索引请求已经被成功创建,其中还包含了_index, _type以及_id的元数据,以及一个新的元素_version
_index 名词就是相当于数据库中库
_type 相当于数据库中的表
_id 就是id(可以自己指定也以自增)

_index 和 _type 和 _id 三者组成elasticsearch存储中的数据的唯一

创建一条

1
2
3
4
5
curl -XPUT 192.168.100.10:9200/website/blog/123 -d '{
"title": "My first blog entry",
"text":"Just trying this out...",
"date":"2016/01/01"
}'






可以看到搜索到了当时创建的文档

1
2
3
4
5
6
7
8
9
10
11
12
13
# curl 192.168.100.10:9200/website/blog/123?pretty
{
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 1,
"found" : true,
"_source" : {
    "title" : "My first blog entry",
    "text" : "Just trying this out...",
    "date" : "2014/01/01"
}
}




每找到的情况:

1
2
3
4
5
6
7
# curl 192.168.100.10:9200/website/blog/1235?pretty
{
"_index" : "website",
"_type" : "blog",
"_id" : "1235",
"found" : false
}





_source 是文档的内容。可以指定值返回文档指定的字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# curl -i -XGET "192.168.100.10:9200/logstash-2016.05.12/syslog/AVSlIBy3bzztddJUaGzh?_source=file,meesage&pretty"
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 185
{
"_index" : "logstash-2016.05.12",
"_type" : "syslog",
"_id" : "AVSlIBy3bzztddJUaGzh",
"_version" : 1,
"found" : true,
"_source" : {
    "file" : "/var/log/messages"
}
}






一次获取多个文档_mget

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# curl -i -XGET 192.168.100.10:9200/logstash-2016.05.12/syslog/_mget?pretty -d '{"ids": [ "2","1"]}'
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 230
{
"docs" : [ {
    "_index" : "logstash-2016.05.12",
    "_type" : "syslog",
    "_id" : "2",
    "found" : false
}, {
    "_index" : "logstash-2016.05.12",
    "_type" : "syslog",
    "_id" : "1",
    "found" : false
} ]
}





lxh9090 发表于 2016-5-30 13:52:48

不错!!
页: [1]
查看完整版本: 增删改查 elasticsearch中的文档API 的使用