ElasticSearch

1. Index 相关

(1). 创建 index (PUT)

// PUT http://127.0.0.1:9200/index-1
// GET http://127.0.0.1:9200/index-1
1
2

(2). 添加 document (PUT, POST)

//PUT http://127.0.0.1:9200/index-1/_doc/1
{
    "name": "user1",
    "address" : "china"
}

//POST http://127.0.0.1:9200/index-1/_doc/
{
    "name": "user1",
    "address" : "china"
}
1
2
3
4
5
6
7
8
9
10
11

PUT 必须在 URL 中加 idPOST 自动添加 id

(3). 搜索 document (GET)

// URL 搜索
//GET http://127.0.0.1:9200/index-1/_search?q=name:"user1"

// Body 搜索
//GET http://127.0.0.1:9200/index-1/_search
{
    "query" : {
        "term" : { "name" : "user1" }
    }
}
1
2
3
4
5
6
7
8
9
10

(4). Aliases (POST, GET)

// POST http://127.0.0.1:9200/_aliases
{
    "actions" : [
        { "remove" : { "index" : "index-1", "alias" : "aa1" } },
        { "add" : { "index" : "index-1", "alias" : "a1" } }
    ]
}
1
2
3
4
5
6
7

(5). Rollover (POST)

// POST http://127.0.0.1:9200/logs_write/_rollover
{
  "conditions" : {
    "max_age": "7d",
    "max_docs": 2,
    "max_size": "5gb"
  }
}
1
2
3
4
5
6
7
8

总结:

  • rollover 针对的是 Alias,这个 Alias 必须指向,且 只指向 最新的 Index,如: logs_write

  • 如果符合条件,则生成一个新的 Index,并将此 Alias 改为指向这个新的 Index。

  • 新 Index 的命令默认有一些规则,如以数字结尾,默认加 1。如果没有符合的规则,则须指定 Index 新名字,如: POST /logs_write/_rollover/my_new_index_name

2. 常用命令

(1). 查询

// GET /_cat/indices/butler-*?h=h,s,i,id,p,r,dc,dd,ss,creation.date.string
1

(2). 删除

// POST butler-documents-int/_delete_by_query
{
    "query": { 
    "match": {
        "_id": "document-8080-id-b0ef4245-6b89-43c5-92ea-8abe51753453"
    }
    }
}

// POST butler-documents-fat/_delete_by_query
{
    "query" : {
        "terms" : {
            "_id" : 
                [ "4894C6BF93DA41420F009B010000C60F000000000000"]
        }
    }
}
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
最近更新: 7/31/2019, 5:41:05 PM