美文网首页
一、Elasticsearch入门-Getting starte

一、Elasticsearch入门-Getting starte

作者: ASD_92f7 | 来源:发表于2019-07-09 16:11 被阅读0次

    一、概述

    官网的例子,参考链接:
    ES版本:7.2.0
    https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-explore.html

    二、Exploring Your Cluster 探索你的集群

    1、Cluster Health 集群健康检查

    GET http://127.0.0.1:9200/_cat/health?v
    

    输出结果:


    健康检查结果.png

    状态(status)有以下三种:

    • green :完全OK可用
    • yellow: 所有数据可以使用,但是某些replicas没有分配
    • red:某些数据不可用

    2、Node Check 节点检查:

    GET http://127.0.0.1:9200/_cat/nodes?v
    

    3、List All Indices 罗列所有index

    GET http://127.0.0.1:9200/_cat/indices?v
    

    输出结果:

    罗列indexes.png
    如上图,罗列了es中的两个index,一个叫asd,一个叫qqq
    可以看到,health=yellow:
    这是因为这里用的是单机版,replica无法分配(至少需要另一个节点)

    4、Create an Index 新建索引

    PUT http://127.0.0.1:9200/mvc
    

    新建了一个叫mvc的索引

    5、Index and Query a Document 向索引添加记录并查询

    PUT http://127.0.0.1:9200/customer/_doc/1?pretty
    -----------------------------------------------------------
    body内容:
    {
      "name": "John Doe"
    }
    

    这里向customer这个索引添加了一个记录,id=1,内容是body中的
    注意,这时候如果customer这个索引不存在,es会自动创建
    检索索引:

    GET http://127.0.0.1:9200/customer/_doc/1?pretty
    

    6、删除索引

    DELETE http://127.0.0.1:9200/customer?pretty
    

    综合上面几个rest api,他们的格式都如下:

    <HTTP Verb> /<Index>/<Endpoint>/<ID>
    

    记住这个,有助于后续的学习,官网这么说的

    三、Modifying Your Data 修改数据

    • PUT的时候,如果id相同,则会进行修改
    • 如果不指定ID,需要用POST方式提交,es会自动生成一个ID

    1、Updating Documents 更新文档

    官方说明,es更新文档,其实是先根据ID删除原有记录,然后新增一个记录
    所以更新和上面的新增是一样的命令,不做累述,仅对按脚本的更新方式作出说明:

    POST http://127.0.0.1:9200/customer/_update/1?pretty
    -----------------------------------------------------------
    {
      "script" : "ctx._source.age += 5"
    }
    

    ctx._source是固定写法

    2、Deleting Documents 删除文档

    DELETE http://127.0.0.1:9200/customer/_doc/2?pretty
    

    3、Batch Processing 批量操作

    • 例子-1
    POST http://127.0.0.1:9200/customer/_bulk?pretty
    -----------------------------------------------------------
    {"index":{"_id":"1"}}
    {"name": "John Doe" }
    {"index":{"_id":"2"}}
    {"name": "Jane Doe" }
    

    上面为index=customer插入了id=1和id=2的记录

    • 例子-2
    POST http://127.0.0.1:9200/customer/_bulk?pretty
    -----------------------------------------------------------
    {"update":{"_id":"1"}}
    {"doc": { "name": "John Doe becomes Jane Doe" } }
    {"delete":{"_id":"2"}}
    

    这个例子,更新了id=1的内容,并且删除了id=2的记录
    注意,body中内容最后一行一定要有回车,否则执行命令会报错:

    The bulk request must be terminated by a newline [\\n]
    

    另外需要注意的是,bulk批量操作不会因为某一个执行失败而停止,它会等到所有的任务执行完毕后,返回执行结果,可以根据结果查看哪个操作失败了!!!

    四、Exploring Your Data 探索你的数据

    做这些例子之前需要导入一些数据,数据的地址如下:
    https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json
    命令如下:

    POST http://127.0.0.1:9200/bank/_bulk?pretty&refresh
    

    导入后,可以通过 /_cat/indices?v 看看是否导入成功(index=bank,1000条数据)

    1、The Search API 查询检索API

    有两种检索方式:
    REST request URI 和 REST request body,即URL方式和body方式

    • 例子-1 URL方式
    GET http://127.0.0.1:9200/bank/_search?q=*&sort=account_number:asc&pretty
    

    endpoint = _search
    q=* 表示匹配所有
    sort=account_number:asc表示按照account_number正序排列
    返回结果如下图(只截取了一部分):

    返回结果.png
    名词解释:
    • took:查询耗时,单位为毫秒
    • timed_out:是否超时
    • shards:本次检索一共检索了几个shard,以及每个shard的检索情况
    • hits :检索结果
      • hits.total:
        • hits.total.value:总共搜索的条数
        • hits.total.relation:如果值为eq,表示刚好等于hits.total.value的值。如果值为gte,表示大于等于hits.total.value的值(目前不太理解,后续补充)
      • hits.hits:实际搜索出的数据,默认显示10条记录
      • hits.sort:结果排序的key (如果没有指定,就按照score得分排序)
      • hits._score:得分
    • 例子-2 body方式
      其实URL方式是body方式的简写,body可以做更复杂的检索
    GET http://127.0.0.1:9200/bank/_search
    -----------------------------------------------------------
    {
      "query": { "match_all": {} },
      "sort": [
        { "account_number": "asc" }
      ]
    }
    

    这个检索,与 例子-1的效果是一致的

    2、Introducing the Query Language 介绍查询语言 DSL

    DSL: Domain-Specific Language
    其实就是JSON格式的检索语言

    • 一个小例子
    GET http://127.0.0.1:9200/bank/_search
    -----------------------------------------------------------
    {
      "query": { "match_all": {} },
      "from": 10,
      "size": 10
    }
    

    分页查询,从第十个记录开始向后查询10个记录,如果size不指定,默认是10个

    3、Executing Searches 执行查询(复杂点儿的)

    这里把ip都省略了

    • 只查询部分字段
    GET /bank/_search
    {
      "query": { "match_all": {} },
      "_source": ["account_number", "balance"]
    }
    
    • match 按照条件检索(空格表示或者的关系)
      查询 address=mill 或者 address=lane 的记录
    GET /bank/_search
    {
      "query": { "match": { "address": "mill lane"} }
    }
    
    • match_phrase 按条件查询(将字符串作为整体进行匹配,空格不算)
      查询 address = mill lane的记录,mill lane是作为一个整体的
    GET /bank/_search
    {
      "query": { "match_phrase": { "address": "mill lane" } }
    }
    
    • bool query 复杂逻辑查询
      相当于SQL中的 AND
    GET /bank/_search
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    

    另外,还有should及must_not等可选

    • should:相当于SQL的 OR
    • must_not :相当于SQL的 !=

    4、Executing Filters 执行过滤器

    • range过滤
      通常用在数字类型或者时间类型的过滤
      查询 20000 <= balance <= 30000 的数据
    GET /bank/_search
    {
      "query": {
        "bool": {
          "must": { "match_all": {} },
          "filter": {
            "range": {
              "balance": {
                "gte": 20000,
                "lte": 30000
              }
            }
          }
        }
      }
    }
    

    5、Executing Aggregations 执行聚合查询

    相当于SQL的group by

    • 来个复杂的例子
      下面例子检索了,三个年龄段人的账户的平均值(按照男女进行分组)
    {
      "size": 0,
      "aggs": {
        "group_by_age": {
          "range": {
            "field": "age",
            "ranges": [
              {
                "from": 20,
                "to": 30
              },
              {
                "from": 30,
                "to": 40
              },
              {
                "from": 40,
                "to": 50
              }
            ]
          },
          "aggs": {
            "group_by_gender": {
              "terms": {
                "field": "gender.keyword"
              },
              "aggs": {
                "average_balance": {
                  "avg": {
                    "field": "balance"
                  }
                }
              }
            }
          }
        }
      }
    }
    

    6、总结

    Elasticsearch is both a simple and complex product. We’ve so far learned the basics of what it is, how to look inside of it, and how to work with it using some of the REST APIs. Hopefully this tutorial has given you a better understanding of what Elasticsearch is and more importantly, inspired you to further experiment with the rest of its great features!

    相关文章

      网友评论

          本文标题:一、Elasticsearch入门-Getting starte

          本文链接:https://www.haomeiwen.com/subject/yodxkctx.html