zoukankan      html  css  js  c++  java
  • ELK-ElasticSearch索引详解

    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" } }
    }'
     
    以下是布尔查询,布尔查询允许我们将多个简单的查询组合成一个更复杂的布尔逻辑查询。
    
    这个例子将两个查询组合,返回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" } }
                ]
            }
        }
    }'
    
    16、过滤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
                        }
                    }
                }
            }
        }
    }'
    
    17、聚合 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
  • 相关阅读:
    Python处理Excel文件
    WebSocket使用中Stomp Client连接报ERROR CODE 200的解决办法
    深入理解Java虚拟机——读书笔记
    主要排序算法的Java实现
    LeetCode 67 Add Binary
    LeetCode 206 单链表翻转
    POJ 2388
    POJ 1207 3N+1 Problem
    POJ 1008 Maya Calendar
    关于指针的一些基本常识
  • 原文地址:https://www.cnblogs.com/linjiqin/p/10764101.html
Copyright © 2011-2022 走看看