美文网首页
【V6.6】ElasticSearch-基础篇.md

【V6.6】ElasticSearch-基础篇.md

作者: smallAttr | 来源:发表于2019-02-14 11:52 被阅读0次

    [TOC]

    es基本概念

    集群:通过唯一的名称来区分集群

    节点:默认名称是通过UUID来生成的,当然如果想加入到某个集群里面,需要修改成对应的集群名称

    索引:名称必须小写

    类型:将在V7.0以后移除

    文档:

    主分片:

    • 允许你水平拆分/扩展存储空间
    • 允许你跨分片分布和并行化操作以提高性能/吞吐量

    复制分片:

    • 当节点发生故障时以提供高可用性,因此,请务必注意,复制分片永远不会在从复制的原始/主分片相同的节点上分配
    • 允许你扩展搜索量/吞吐量,因为可以在所有副本上并行执行搜索

    集群健康

    curl -X GET "localhost:9200/_cat/health?v"
    

    集群节点

    curl -X GET "localhost:9200/_cat/nodes?v"
    

    索引

    查看所有索引:

    curl -X GET "localhost:9200/_cat/indices?v"
    

    创建索引:

    curl -X PUT "localhost:9200/customer?pretty"
    

    创建文档(在创建文档之前不用事先创建index)

    创建文档(明确指定ID用PUT)

    curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
    {
      "name": "John Doe"
    }
    '
    

    创建文档(未明确指定ID用POST)

    curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
    {
      "name": "Jane Doe"
    }
    '
    

    查询文档

    curl -X GET "localhost:9200/customer/_doc/1?pretty"
    

    删除索引

    curl -X DELETE "localhost:9200/customer?pretty"
    

    更新文档

    curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
    {
      "doc": { "name": "Jane Doe" }
    }
    '
    

    动态添加字段

    curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
    {
      "doc": { "name": "Jane Doe", "age": 20 }
    }
    '
    

    使用简单脚本修改文档

    curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
    {
      "script" : "ctx._source.age += 5"
    }
    '
    

    删除文档

    curl -X DELETE "localhost:9200/customer/_doc/2?pretty"
    

    批量处理(_bulk API)

    批量创建

    curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
    {"index":{"_id":"1"}}
    {"name": "John Doe" }
    {"index":{"_id":"2"}}
    {"name": "Jane Doe" }
    '
    

    批量操作

    curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
    {"update":{"_id":"1"}}
    {"doc": { "name": "John Doe becomes Jane Doe" } }
    {"delete":{"_id":"2"}}
    '
    

    加载简单数据集(注意文件名没有前面的@)

    curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
    

    搜索API(默认返回10条信息)

    rest request URI

    curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"
    

    rest request body

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": { "match_all": {} },
      "sort": [
        { "account_number": "asc" }
        { "balance": { "order": "desc" } }
      ]
    }
    '
    

    指定返回结果的fields

    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": { "match_all": {} },
      "_source": ["account_number", "balance"]
    }
    '
    

    bool query

    • should: 满足其中一个条件就行
    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "bool": {
          "should": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }
    '
    
    • must: 必须满足所有条件
    • must_not: 返回不满足当前筛选添加的结果集
    • filter:筛选结果集
    curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "bool": {
          "must": { "match_all": {} },
          "filter": {
            "range": {
              "balance": {
                "gte": 20000,
                "lte": 30000
              }
            }
          }
        }
      }
    }
    '
    

    聚合(Aggregations)

    es设置

    设置es日志级别

    curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
    {
      "transient": {
        "logger.org.elasticsearch.transport": "trace"
      }
    }
    '
    

    es监控

    curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
    {
      "persistent": {
        "xpack.monitoring.collection.enabled": true
      }
    }
    '
    

    公共选项

    • pretty=true (json格式展开)
    • human=false (以人方便阅读的方式展示)
    • filter_path=took,-hits.hits._id (-代表不展示该字段)
    • flat_settings=true (以平面格式展示)
    • error_trace=true (显示错误堆栈)

    index API

    自动创建index

    禁止自动创建index:action.auto_create_index = false(默认为true)

    可以指定pattern:action.auto_create_index = +aaa,-bbb,+ccc,- (+ 允许、-禁止)

    禁止自动创建mapping:index.mapper.dynamic = false

    version

    • 创建文档时不指定默认为1,后续修改一次+1
    • 当然可以在创建文档的时候指定version(version>=0)

    Operation Type

    方式一:
    curl -X PUT "localhost:9200/twitter/_doc/1?op_type=create" -H 'Content-Type: application/json' -d'
    {
        "user" : "kimchy",
        "post_date" : "2009-11-15T14:12:12",
        "message" : "trying out Elasticsearch"
    }
    '
    方式二:
    curl -X PUT "localhost:9200/twitter/_doc/1/_create" -H 'Content-Type: application/json' -d'
    {
        "user" : "kimchy",
        "post_date" : "2009-11-15T14:12:12",
        "message" : "trying out Elasticsearch"
    }
    '
    
    

    op_type=create or _create强制指定为创建模式,如果存在该文档就会报错

    Routing(路由)

    显示指定路由到kimchy,否则默认为文档ID的hash值

    curl -X POST "localhost:9200/twitter/_doc?routing=kimchy" -H 'Content-Type: application/json' -d'
    {
        "user" : "kimchy",
        "post_date" : "2009-11-15T14:12:12",
        "message" : "trying out Elasticsearch"
    }
    '
    

    Timeout

    curl -X PUT "localhost:9200/twitter/_doc/1?timeout=5m" -H 'Content-Type: application/json' -d'
    {
        "user" : "kimchy",
        "post_date" : "2009-11-15T14:12:12",
        "message" : "trying out Elasticsearch"
    }
    '
    

    delete API

    • delete document
    curl -X DELETE "localhost:9200/twitter/_doc/1"
    
    • 可以删除多个index、多个type
    curl -X POST "localhost:9200/twitter,blog/_docs,post/_delete_by_query" -H 'Content-Type: application/json' -d'
    {
      "query": {
        "match_all": {}
      }
    }
    '
    
    • 查看删除状态
    curl -X GET "localhost:9200/_tasks?detailed=true&actions=*/delete/byquery"
    
    • 取消删除任务
    curl -X POST "localhost:9200/_tasks/r1A2WoRbTwKZ516z6NEs5A:36619/_cancel"
    
    • 查看取消删除任务情况
    curl -X POST "localhost:9200/_delete_by_query/r1A2WoRbTwKZ516z6NEs5A:36619/_rethrottle?requests_per_second=-1"
    
    • 并行删除

    update API

    • 脚本更新
    curl -X POST "localhost:9200/test/_doc/1/_update" -H 'Content-Type: application/json' -d'
    {
        "script" : {
            "source": "ctx._source.counter += params.count",
            "lang": "painless",
            "params" : {
                "count" : 4
            }
        }
    }
    '
    
    • 局部更新
    curl -X POST "localhost:9200/test/_doc/1/_update" -H 'Content-Type: application/json' -d'
    {
        "doc" : {
            "name" : "new_name"
        }
    }
    '
    
    • upsert

    Reindex API

    赋值文档到新索引里面去

    curl -X POST "localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
    {
      "source": {
        "index": "twitter"
      },
      "dest": {
        "index": "new_twitter"
      }
    }
    '
    

    相关文章

      网友评论

          本文标题:【V6.6】ElasticSearch-基础篇.md

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