博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
REST API
阅读量:4673 次
发布时间:2019-06-09

本文共 3472 字,大约阅读时间需要 11 分钟。

下面简单介绍下elasticsearch提供的一套api的用法:

1. 创建索引 PUT (注意,index名称里的字母必须小写)

下面创建了索引jiu3,里面配置信息重写,否则默认5个分片和一个副本,映射里写了一个type:lvh,以及两个字段。当然,大括号里这些并不是必需的。

1 curl -XPUT 192.168.100.15:9200/index
1 curl -XPUT http://192.168.100.15:9200/jiu3/ -d '{
2 "settings":{ 3 "index":{ 4 "number_of_shards":1, 5 "number_of_replicas":0 6 } 7 }, 8 "mappings":{ 9 "lvh":{10 "properties":{11 "name":{12 "type":"string"},13 "age":{14 "type":"long"}15 }16 }17 }18 }'
1 curl -XPUT 192.168.100.15:9200/index/type/_mapping -d @mapping.json 其中mapping.json的内容是以下形式:
1 { 2    "lvh":{ 3         "properties":{ 4              "name":{ 5                   "type":"string"}, 6               "age":{ 7                    "type":"long"} 8            } 9      }10 }

 

以上都是手动添加索引映射,还有另一种方法:插入数据,根据数据自动创建索引和映射:

1 #以后eshost都代替192.168.100.15:92002 curl -XPUT eshost/index/type/id -d '{
3 "name":"lvhuan",4 "age":"21"5 }'

2. 添加文档(无则添加,有则覆盖) POST

添加文档时,可以在现有type里添加,亦可以新建一个type来添加文档。唯一标识符(_id)可写可不写。

这里需要注意的是,覆盖特效。新内容覆盖对应id所有的旧内容。

1 curl -XPOST eshost/jiu3/lih/1 -d '{
2 "name":"huan"}'

批量添加(特别注意格式,尤其是每条记录之间的回车必不可少; 同样,id可以缺省)(另外,记录数可以等于一)

POST /_bulk{ "index": { "_index": "addr", "_type": "contact", "_id": 1 }}{ "name": "Fyodor Dostoevsky", "country": "RU" }{ "create": { "_index": "addr", "_type": "contact", "_id": 2 }}{ "name": "Erich Maria Remarque", "country": "DE" }{ "create": { "_index": "addr", "_type": "contact", "_id": 2 }}{ "name": "Joseph Heller", "country": "US" }{ "delete": { "_index": "addr", "_type": "contact", "_id": 4 }}{ "delete": { "_index": "addr", "_type": "contact", "_id": 1 }}
curl -XPOST 'localhost:9200/_bulk?pretty' --data-binary@documents.json

其中,json文件内容就是之前那种记录排列的格式。

3. 获取索引、文档 GET

pretty是以JSON格式显示

1 curl -XGET http://192.168.100.15:9200/jiu3?pretty
1 curl -XGET http://192.168.100.15:9200/jiu3/lih/1?pretty

4. 查询文档 GET

对于字符串匹配,可模糊查询,即匹配字符串部分内容。

查询所有信息

1 curl -XGET http://192.168.100.15:9200/jiu3/lih/_search?pretty
1 curl -XGET eshost/index/type/_search -d '{
2 "query":{3 "match_all":{}4 }5 }'

查询,匹配单字段

1 curl -XGET eshost/index/type/_search?pretty&q=name:huan
1 curl -XGET eshost/index/type/_search -d '{
2 "query":{3 "match":{4 "age":21}5 }6 }'
1 curl -XGET eshost/index/type/_search -d '{
2 "query":{3 "query_string":{4 "query":"age:21"5 }6 }7 }'

匹配多字段

1 curl -XGET eshost/index/type/_search -d '{
2 "query": { 3 "bool": { 4 "should": [ 5 { "match": { "title": "War and Peace" }}, 6 { "match": { "author": "Leo Tolstoy" }} 7 ] 8 } 9 }10 }‘

匹配单词条 这里的词条是指字段的单元,确切、未经分析的词条,即必须完全匹配。

1 curl -XGET eshost/index/type/_search -d '{
2 "query": {3 "term": {4 "tags":"novel"5 }6 }7 }‘

匹配多词条

1 curl -XGET eshost/index/type/_search -d '{
2 "query": {3 "terms": {4 "tags":["novel","book"]5 }6 }7 }‘

5.删除文档 DELETE

1 curl -XDELETE eshost/index/type/id

6.更新文档 POST

这里只举两个我常用的写法,其他可参看

若下面第一个写法报错,解决方案在上面链接里可找到。

与前面覆盖文档不同,这里只需写要更新的字段即可,其他字段不写。有则更新,无则添加。

1 curl -XPOST eshost/index/type/id/_update -d '{
2 "script":"ctx._source.name = \"lv huan\" "3 }'
1 curl -XPOST eshost/index/type/id/_update -d '{
2 "doc":{3 "name" = "lv huan"4 }5 }'

 

转载于:https://www.cnblogs.com/jiu0821/p/5635417.html

你可能感兴趣的文章
java中enum类型的使用
查看>>
枚举类型转换成字符串
查看>>
(剑指Offer)面试题46:求1+2+3+....+n
查看>>
连续子数组的最大和(基于动态规划)
查看>>
Word Search
查看>>
三:背包DP
查看>>
Nikto学习点
查看>>
OSi七成模型 tcp/ip网络模型
查看>>
初学python之路-day15
查看>>
Linux内核分析——进程的描述和进程的创建
查看>>
【C++自我精讲】基础系列三 重载
查看>>
企业级docker私有仓库的配置与使用
查看>>
ireport5.6+jasperreport6.3开发(四)--以javabean为基准的报表开发(ireport)
查看>>
Spring面试底层原理的那些问题,你是不是真的懂Spring?
查看>>
Java知识导航总图
查看>>
关于Ajax的实现
查看>>
$cast
查看>>
js 把字符串格式化成时间
查看>>
关于老师
查看>>
[Swift]LeetCode212. 单词搜索 II | Word Search II
查看>>