ELK-ElasticSearch索引详解

一、使用_cat API检测集群是否健康,确保9200端口号可用: curl 'localhost:9200/_cat/health?v' 注意:绿色表示一切正常,黄色表示全部的数据可用可是部分副本尚未分配,红色表示部分数据由于某些缘由不可用。 二、获取集群的节点列表 curl 'localhost:9200/_cat/nodes?v' 三、查看全部索引 curl http://localhost:9200/_cat/indices?v 四、curl用法 -X 指定http的请求方法,有HEAD GET POST PUT DELETE -d 指定要传输的数据 -H 指定http请求头信息 五、新增索引 如今咱们建立一个名为"customer"的索引,而后再查看全部的索引: curl -XPUT 'localhost:9200/customer?pretty' curl 'localhost:9200/_cat/indices?v' 六、插入和获取 6.一、插入数据 必须给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.二、获取数据 curl -XGET 'localhost:9200/customer/external/1?pretty' 其中含义为:获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观。 七、删除索引 curl -XDELETE 'localhost:9200/customer?pretty' curl 'localhost:9200/_cat/indices?v' 八、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 九、修改数据 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" }' 十一、删除数据 删除数据那是至关的直接. 下面的语句将执行删除Customer中ID为2的数据: curl -XDELETE 'localhost:9200/customer/external/2?pretty' 十二、批处理 下面语句将在一个批量操做中执行建立索引: 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' 1三、查询 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": {} } }' 1四、查询语言 匹配全部数据,但只返回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条)。 1五、执行搜索 下面例子展现如何返回两个字段(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" } } }' 如下是布尔查询,布尔查询容许咱们将多个简单的查询组合成一个更复杂的布尔逻辑查询。 这个例子将两个查询组合,返回name中含有AAA和Doe的全部记录数据,属于and操做: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "name": "Doe" } }, { "match": { "name": "AAA" } } ] } } }' 上述例子中,must表示全部查询必须都为真才被认为匹配。 相反, 这个例子组合两个查询,返回name中含有Doe或者Lynch的全部记录数据: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "should": [ { "match": { "name": "Doe" } }, { "match": { "name": "Lynch" } } ] } } }' 上述例子中,bool表示查询列表中只要有任何一个为真则认为匹配。 下面例子组合两个查询,返回地址中既没有mill也没有lane的全部数据: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "must_not": [ { "match": { "name": "Doe" } }, { "match": { "name": "AAA" } } ] } } }' 上述例子中,must_not表示查询列表中没有为真的(也就是全为假)时则认为匹配。 咱们能够组合must、should、must_not来实现更加复杂的多级逻辑查询。 下面这个例子返回年龄等于10岁、name不为"Jane Doe AAA"的全部数据: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "age": "10" } } ], "must_not": [ { "match": { "name": "Jane Doe AAA" } } ] } } }' 1六、过滤filter(查询条件设置) 下面这个例子使用了布尔查询返回age在10到30之间的全部数据。 curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "must": { "match_all": {} } } } }' curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "must": { "match_all": {} }, "filter": { "range": { "age": { "gte": 10, "lte": 20 } } } } } }' 1七、聚合 Aggregations 下面这个例子: 将全部的数据按照age分组(group),而后按照分组记录数从大到小排序,返回前十条(默认): curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "size": 0, "aggs": { "group_by_age": { "terms": { "field": "age" } } } }' 注意:咱们设置size=0,不显示查询hits,由于咱们只想看返回的聚合结果。 上述语句相似于如下SQL语句: select age, count(*) from customer group by age order by count(*) desc 下面这个实例按照age分组,降序排序,返回age的平均值: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "size": 0, "aggs": { "group_by_age": { "terms": { "field": "age" }, "aggs": { "average_age": { "avg": { "field": "age" } } } } } }' curl -XGET 'localhost:9200/mytest?pretty' 删除索引 curl -XDELETE 'localhost:9200/mytest?pretty' 新增一条记录,并指定为article类型,ID为1 curl -XPUT -H "Content-Type: application/json" 'localhost:9200/mytest/article/1?pretty' -d ' { "title": "小D课堂啦啦啦", "content":"xdclass.net 小D课堂成立于2016年的,专一互联网在线教育,课程范围包括前端,后端,大数据,人工智能,微信开发等" }' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/mytest/article/2?pretty' -d ' { "title": "test", "content":"testsfsdfdsfdsf", "PV":10 }' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/mytest/article/3?pretty' -d ' { "title": "test", "content":"testsfsdfdsfdsf", "PV":23 }' 搜索 curl -XGET 'http://localhost:9200/mytest/article/_search?q=title:D&pretty' curl -XGET 'http://localhost:9200/mytest/article/_search?q=title:test&pretty' 查看数据 curl http://localhost:9200/es-message-2019.04.23/_search?pretty
相关文章
相关标签/搜索