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

2019-02-14  本文已影响0人  smallAttr

[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

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
'
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
  }
}
'

公共选项

index API

自动创建index

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

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

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

version

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

curl -X DELETE "localhost:9200/twitter/_doc/1"
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"
    }
}
'

Reindex API

赋值文档到新索引里面去

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

猜你喜欢

热点阅读