文章目录
- 语法规则
- 查看集群的健康状态
- 查看集群节点
- 列出所有的指数
- 创建一个索引
- 向这个索引中添加一个document
- 删除索引
- 修改数据
- 更新文档
- 删除文档
- 批量处理
- 更复杂的查询
- 查询API
- match_all()
- 执行搜索
- 使用match
- Bool查询
- 安装head插件
本文档主要是跟着官方文档操作的
语法规则
es提供restful API风格的查询
curl -X <动作> ‘< protocol >://:/ ?<query_string>’-d ‘ < body > ’
-X指定动作,比如PUT等,-d后面跟一个json字符串
另外,我们可以使用es-head插件或者postman会更方便
查看集群的健康状态
curl -X GET "10.10.31.8:9200/_cat/health?v&pretty"
[root@lvs ~]# curl -X GET "10.10.31.8:9200/_cat/health?v&pretty"
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1590648220 06:43:40 my-es green 3 3 0 0 0 0 0 0 - 100.0%
查看集群节点
curl -X GET "10.10.31.8:9200/_cat/nodes?pretty"
[root@lvs ~]# curl -X GET "10.10.31.8:9200/_cat/nodes?pretty"
10.10.31.8 23 91 6 0.11 0.12 0.09 mdi - node-1
10.10.31.28 9 93 0 0.00 0.02 0.05 mdi * node-2
10.10.31.26 12 96 0 0.00 0.01 0.05 mdi - node-3
列出所有的指数
curl -X GET "10.10.31.8:9200/_cat/indices?v&pretty"
[root@lvs ~]# curl -X GET "10.10.31.8:9200/_cat/indices?pretty"
[root@lvs ~]# curl -X GET "10.10.31.8:9200/_cat/indices?v&pretty"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
这表示集群中尚没有索引
创建一个索引
创建索引的语句:- X后面使用动作PUT
curl -X PUT "10.10.31.8:9200/customer?pretty&pretty"
实操:创建并查看索引
[root@lvs ~]# curl -X PUT "10.10.31.8:9200/customer?pretty&pretty"
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "customer"
}
[root@lvs ~]# curl -X GET "10.10.31.8:9200/_cat/indices?v&pretty"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open customer eNXdpm_KQdq5V1Q7gPvrBQ 5 1 0 0 1.2kb 631b
向这个索引中添加一个document
curl -X PUT "10.10.31.8:9200/customer/doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
[root@lvs ~]# curl -X PUT "10.10.31.8:9200/customer/doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 2
}
从上面可以看到,在customer索引中成功创建了一个新的客户文档。该文档的内部ID为1,这是我们在索引时指定的。
重要的是要注意,Elasticsearch不需要您首先显式创建索引,然后才能将文档建立索引。在上一个示例中,Elasticsearch将自动创建事先不存在的客户索引。
现在,让我们检索刚刚索引的文档:
DSL语句:
curl -X GET "localhost:9200/customer/doc/1?pretty&pretty"
[root@lvs ~]# curl -X GET "localhost:9200/customer/doc/1?pretty&pretty"
{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 2,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}
删除索引
现在,我们删除索引,然后再次列出所有的索引
curl -X DELETE "localhost:9200/customer?pretty&pretty"
curl -X GET "localhost:9200/_cat/indices?v&pretty"
[root@lvs ~]# curl -X DELETE "localhost:9200/customer?pretty&pretty"
{
"acknowledged" : true
}
[root@lvs ~]# curl -X GET "localhost:9200/_cat/indices?v&pretty"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
修改数据
Elasticsearch提供近乎实时的数据处理和搜索功能。默认情况下,从索引/更新/删除数据到显示在搜索结果中的时间可能会有一秒钟的延迟(刷新间隔)。这是与其他平台(如SQL)的重要区别,在其他平台上,数据在事务完成后立即可用。
前面我们已经看到了如何为单个文档建立索引。让我们再次调用该命令:
curl -X PUT "localhost:9200/customer/doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
效果:
[root@lvs ~]# curl -X PUT "localhost:9200/customer/doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
> {
> "name": "John Doe"
> }
> '
{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
上面的代码会将指定文档的ID索引到客户索引中,其ID为1。如果我们再对另一个(或相同)文档执行上述命令,Elasticsearch将替换(即重新索引)一个新文档。
我这里对相同的文档执行命令,把John Doe改成我的名字
curl -X PUT "localhost:9200/customer/doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "suzijian"
}
'
效果
[root@lvs ~]# curl -X PUT "localhost:9200/customer/doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "suzijian"
}
'
{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
如果我们使用新的id,那么将创建一个新的文档,这里我创建一个叫lanxinyu的文档
curl -X PUT "localhost:9200/customer/doc/2?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "lanxinyu"
}
'
更新文档
尽管Elasticsearch实际上并未在后台进行就地更新。每当我们进行更新时,Elasticsearch都会删除旧文档,然后在一个快照中将应用了更新的新文档编入索引。
示例一: 更新 id 为1文档的名字
curl -X POST "localhost:9200/customer/doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe" }
}
'
示例二:
名称字段更改为“ Jane Doe”并同时向其添加年龄字段来更新我们之前的文档(ID为1):
curl -X POST "localhost:9200/customer/doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}
'
实操:
[root@lvs ~]# curl -X POST "localhost:9200/customer/doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'
> {
> "doc": { "name": "Jane Doe", "age": 20 }
> }
> '
{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 4,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
还可以使用简单的脚本对数据进行修改
curl -X POST "localhost:9200/customer/doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.age += 5"
}
'
[root@lvs ~]# curl -X POST "localhost:9200/customer/doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'
> {
> "script" : "ctx._source.age += 5"
> }
> '
{
"_index" : "customer",
"_type" : "doc",
"_id" : "1",
"_version" : 5,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
删除文档
删除文档非常简单。此示例显示了如何删除ID为2的先前客户:
curl -X DELETE "localhost:9200/customer/doc/2?pretty&pretty"
效果
[root@lvs ~]# curl -X DELETE "localhost:9200/customer/doc/2?pretty&pretty"
{
"_index" : "customer",
"_type" : "doc",
"_id" : "2",
"_version" : 5,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
批量处理
批量操作使用_bulk
API
以下调用在一个批量操作中为两个文档(ID 1-John Doe和ID 2-Jane Doe)建立了索引
curl -X POST "localhost:9200/customer/doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
本示例在一个批量操作中更新第一个文档(ID为1),然后删除第二个文档(ID为2):
curl -X POST "localhost:9200/customer/doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
上面请注意,对于删除操作,在其后没有相应的源文档,因为删除仅需要删除文档的ID。
批量API不会因其中一项操作失败而失败。如果单个操作由于任何原因而失败,它将继续处理其后的其余操作。批量API返回时,它将为每个操作提供状态(以发送顺序相同),以便您可以检查特定操作是否失败。
更复杂的查询
先把数据导下来,跟着官方文档走
复制这里的连接地址,把数据导入
wget https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json?raw=true
如果在GitHub上吧数据下载下来后,不是account.json,需要重命令一下
mv accounts.json\?raw\=true accounts.json
然后创建索引并导入数据
curl -H "Content-Type: application/json" -XPOST '10.10.31.8:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
查询API
调用_search
查询接口,一般以key,value形式查询,这里q=*表示查询所有
curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty"
部分数据如下
{
"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : null,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "0",
"sort": [0],
"_score" : null,
"_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
}, {
"_index" : "bank",
"_type" : "account",
"_id" : "1",
"sort": [1],
"_score" : null,
"_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
}, ...
]
}
}
官方文档解释
match_all()
使用 json 风格的查询API,match_all
查询出所有,并使用account_number
进行排序
这种json风格的查询语句,叫做DSL语法
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
'
指定查询多少条数据吗,查询一条数据,如果不指定,默认查询10条
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"size": 1
}
'
从第10条开始查,查10条
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}
'
排序查找
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}
'
执行搜索
现在,我们已经了解了一些基本的搜索参数,下面让我们进一步探讨一下Query DSL。首先让我们看一下返回的文档字段。默认情况下,完整的JSON文档将作为所有搜索的一部分返回。这称为来源(_source搜索结果中的字段)。如果我们不希望返回整个源文档,则可以只返回源中的少数几个字段。
下面查询只返回"account_number", "balance"
两个字段
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"],
}
'
使用match
match针对某个字段进行查询,返回编号为20的用户
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match": { "account_number": 20 } }
}
'
效果
[root@lvs ~]# curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
> {
> "query": { "match": { "account_number": 20 } }
> }
> '
{
"took" : 64,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "20",
"_score" : 1.0,
"_source" : {
"account_number" : 20,
"balance" : 16418,
"firstname" : "Elinor",
"lastname" : "Ratliff",
"age" : 36,
"gender" : "M",
"address" : "282 Kings Place",
"employer" : "Scentric",
"email" : "elinorratliff@scentric.com",
"city" : "Ribera",
"state" : "WA"
}
}
]
}
}
返回address中包含mail,类似于通配,有mail的就返回,而且不区分大小写
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match": { "address": "mill" } }
}
'
Bool查询
本示例组成两个match查询,并返回地址中包含“ mill”和“ lane”的所有帐户:
该bool must子句指定了将文档视为匹配项必须为真的所有查询。
must有点类似and的意思,必须所有条件都满足的都返回
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'
should关键字类似或的意思,或就是,只要其中的一个条件满足,就返回
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'
must_not
本示例组成两个match查询,并返回地址中既不包含“ mill”也不包含“ lane”的所有帐户:
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'
我们可以在查询中同时组合must,should和must_not的bool子句。此外,我们可以bool在任何这些bool子句中编写查询,以模仿任何复杂的多级布尔逻辑。
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
'
安装head插件
增加这个配置