码迷,mamicode.com
首页 > 其他好文 > 详细

ELK-ElasticSearch 索引查询使用指南——详细版

时间:2019-04-24 19:42:33      阅读:1371      评论:0      收藏:0      [点我收藏+]

标签:头信息   详细   last   mes   res   script   分数   put   external   

1、使用_cat API检测集群是否健康,确保9200端口号可用:
curl ‘localhost:9200/_cat/health?v‘

注意:绿色表示一切正常,黄色表示所有的数据可用但是部分副本还没有分配,红色表示部分数据因为某些原因不可用。

2、获取集群的节点列表
curl ‘localhost:9200/_cat/nodes?v‘

3、查看所有索引
curl http://localhost:9200/_cat/indices?v

4、curl用法
-X 指定http的请求方法,有HEAD GET POST PUT DELETE
-d 指定要传输的数据
-H 指定http请求头信息

5、新增索引
现在我们创建一个名为"customer"的索引,然后再查看所有的索引:
curl -XPUT ‘localhost:9200/customer?pretty‘
curl ‘localhost:9200/_cat/indices?v‘

6、插入和获取
6.1、插入数据

必须给ES指定索引的类型,如索引为customer,类型为external,ID为1,主体为JSON格式的语句:{ "name": "John Doe" }

curl -XPUT -H "Content-Type: application/json" ‘localhost:9200/customer/external/1?pretty‘ -d ‘
{
"name": "John Doe",
"age": 10
}‘

curl -XPUT -H "Content-Type: application/json" ‘localhost:9200/customer/external/3?pretty‘ -d ‘
{
"name": "AAA",
"age": 20
}‘

curl -XPUT -H "Content-Type: application/json" ‘localhost:9200/customer/external/4?pretty‘ -d ‘
{
"name": "BBB",
"age": 30
}‘

curl -XPUT -H "Content-Type: application/json" ‘localhost:9200/customer/external/5?pretty‘ -d ‘
{
"name": "EEE",
"age": 40
}‘

返回结果为:create:true 表示插入成功。

6.2、获取数据
curl -XGET ‘localhost:9200/customer/external/1?pretty‘

其中含义为:获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观。

7、删除索引
curl -XDELETE ‘localhost:9200/customer?pretty‘
curl ‘localhost:9200/_cat/indices?v‘

8、ES基本用法
通过以上命令语句的学习,我们发现索引的增删改查有一个类似的格式,总结如下:
curl -X<REST Verb> <ip>:<Port>/<Index>/<Type>/<ID>
<REST Verb>:REST风格的语法谓词
<ip>:节点ip
<port>:节点端口号,默认9200
<Index>:索引名
<Type>:索引类型
<ID>:操作对象的ID号

ES的动作是以http方法来决定的,常用的http方法: GET/PUT/POST/DELETE

9、修改数据
curl -XPUT -H "Content-Type: application/json" ‘localhost:9200/customer/external/2?pretty‘ -d ‘
{
"name": "aaa"
}‘

curl -XPUT -H "Content-Type: application/json" ‘localhost:9200/customer/external/2?pretty‘ -d ‘
{
"name": "Java"
}‘

上述命令语句是:先新增id为1,name为aaa的数据,然后将id为2的name修改为Java。

10.更新数据

10.1 这个例子展示如何将id为1文档的name字段更新为Jane Doe:

curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/1/_update?pretty‘ -d ‘
{
"doc": { "name": "Jane Doe AAA" }
}‘

10.2 这个例子展示如何将id为1数据的name字段更新为Jane Doe同时增加字段age为20:
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/1/_update?pretty‘ -d ‘
{
"doc": { "name": "Jane Doe", "age": 20 }
}‘

10.3 也可以通过一些简单的scripts来执行更新。一下语句通过使用script将年龄增加5:
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/1/_update?pretty‘ -d ‘
{
"script" : "ctx._source.age += 5"
}‘

11、删除数据
删除数据那是相当的直接. 下面的语句将执行删除Customer中ID为2的数据:
curl -XDELETE ‘localhost:9200/customer/external/2?pretty‘

12、批处理
下面语句将在一个批量操作中执行创建索引:
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_bulk?pretty‘ -d ‘
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }

查看数据
curl -XGET ‘localhost:9200/customer/external/2?pretty‘
curl -XGET ‘localhost:9200/customer/external/_search?pretty‘


下面语句批处理执行更新id为1的数据然后执行删除id为2的数据
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_bulk?pretty‘ -d ‘
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}

curl -XGET ‘localhost:9200/customer/external/_search?pretty‘

13、查询
curl ‘localhost:9200/customer/external/_search?q=*&pretty‘

上面示例返回所有customer中的索引数据。其中 q=* 表示匹配索引中所有的数据。

等价于:
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_search?pretty‘ -d ‘
{
"query": { "match_all": {} }
}‘

14、查询语言
匹配所有数据,但只返回1个:
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_search?pretty‘ -d ‘
{
"query": { "match_all": {} },
"size": 1
}‘

注意:如果siez不指定,则默认返回10条数据。

curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_search?pretty‘ -d ‘
{
"query": { "match_all": {} },
"from": 0,
"size": 10
}‘

返回从0到10的数据。(索引下标从0开始)

curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_search?pretty‘ -d ‘
{
"query": { "match_all": {} },
"sort": { "age": { "order": "desc" } }
}‘

curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_search?pretty‘ -d ‘
{
"query": { "match_all": {} },
"sort": { "age": { "order": "asc" } }
}‘
上述示例匹配所有的索引中的数据,按照age字段降序排序,并且返回前10条(如果不指定size,默认最多返回10条)。

15、执行搜索
下面例子展示如何返回两个字段(name age)
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_search?pretty‘ -d ‘
{
"query": { "match_all": {} },
"_source": ["name", "age"]
}‘

返回age为20 的数据:
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_search?pretty‘ -d ‘
{
"query": { "match": { "age": 20 } }
}‘


返回name中包含Doe的所有数据::
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_search?pretty‘ -d ‘
{
"query": { "match": { "name": "Doe" } }
}‘

返回name中包含Doe或者AAA的所有数据:
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_search?pretty‘ -d ‘
{
"query": { "match": { "name": "Doe AAA" } }
}‘

和上面匹配单个词语不同,下面这个例子是多匹配(match_phrase短语匹配),返回name字段中包含短语 “mill lane”的所有数据:
curl -XPOST -H "Content-Type: application/json" ‘localhost:9200/customer/external/_search?pretty‘ -d ‘
{
"query": { "match_phrase": { "name": "mill lane" } }
}‘

以下是布尔查询,布尔查询允许我们将多个简单的查询组合成一个更复杂的布尔逻辑查询。



ELK-ElasticSearch 索引查询使用指南——详细版

标签:头信息   详细   last   mes   res   script   分数   put   external   

原文地址:https://www.cnblogs.com/linjiqin/p/10764101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!