天天看點

ES技術團隊劃重點 | ES6.X,你必須知道的API和相關技巧0. ES相關推薦1. 查詢操作2. 索引操作3. 調試和部署4.最有用的插件5.其他資訊6. 小結

0. ES相關推薦

首先,不要再使用curl,請安裝sense(kibana5.x中預設包含sense)

1)ES官方向導

https://www.elastic.co/guide/en/elasticsearch/guide/master/index.html

2)ES官方文檔(API相關)

https://www.elastic.co/guide/en/elasticsearch/reference/master/index.html

3)ES資源清單(全)

https://github.com/dzharii/awesome-elasticsearch

4)ES雲

https://www.found.no/play

5)ES官方論壇(英文)

https://discuss.elastic.co/

6)ES|stackOverflow論壇位址

https://stackoverflow.com/questions/tagged/elasticsearch

7)ES 性能測試相關(NB)

https://www.datadoghq.com/blog/monitor-elasticsearch-performance-metrics/

1. 查詢操作

基本查詢有兩種文法:左邊是一個簡單的文法,你不能使用任何選項,

右邊是一個擴充。 大多數初學者頭痛的DSL來自于:

GET _search
{
  "query": {
    "match": {
      "FIELD": "TEXT"
    }
  }
}
GET _search
{
  "query": {
    "match": {
      "FIELD": {
        "query": "TEXT",
        "OPTION": "VALUE"
      }
    }
  }
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19      

1.1 包含高亮、聚合、過濾器的完整的檢索示例

GET /_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "smith"
          }
        }
      ],
      "must_not": [
        {
          "match_phrase": {
            "title": "granny smith"
          }
        }
      ],
      "filter": [
        {
          "exists": {
            "field": "title"
          }
        }
      ]
    }
  },
  "aggs": {
    "my_agg": {
      "terms": {
        "field": "user",
        "size": 10
      }
    }
  },
  "highlight": {
    "pre_tags": [
      "<em>"
    ],
    "post_tags": [
      "</em>"
    ],
    "fields": {
      "body": {
        "number_of_fragments": 1,
        "fragment_size": 20
      },
      "title": {}
    }
  },
  "size": 20,
  "from": 100,
  "_source": [
    "title",
    "id"
  ],
  "sort": [
    {
      "_id": {
        "order": "desc"
      }
    }
  ]
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20
• 21
• 22
• 23
• 24
• 25
• 26
• 27
• 28
• 29
• 30
• 31
• 32
• 33
• 34
• 35
• 36
• 37
• 38
• 39
• 40
• 41
• 42
• 43
• 44
• 45
• 46
• 47
• 48
• 49
• 50
• 51
• 52
• 53
• 54
• 55
• 56
• 57
• 58
• 59
• 60
• 61
• 62
• 63
• 64      

1.2 普通檢索

多字段檢索

"multi_match": {
  "query": "Elastic",
  "fields": ["user.*", "title^3"],
  "type": "best_fields"
}• 1
• 2
• 3
• 4
• 5      

bool檢索

"bool": {
  "must": [],
  "must_not": [],
  "filter": [],
  "should": [],
  "minimum_should_match" : 1
}• 1
• 2
• 3
• 4
• 5
• 6
• 7      

範圍檢索

"range": {
  "age": {
    "gte": 10,
    "lte": 20,
    "boost": 2
  }
}• 1
• 2
• 3
• 4
• 5
• 6
• 7      

1.3 QueryString文法概述

1.3.1 檢索所有的_all字段

GET /_search?q=pony• 1      

1.3.2 包含運算符和包含boost精确檢索的複雜檢索

GET /_search?q=title:(joli OR code) AND author:"Damien Alexandre"^2• 1      

1.3.3 使用通配符和特殊查詢進行檢索

GET /_search?q=_exists_:title OR title:singl? noneOrAnyChar*cter• 1      

1.3.4 模糊搜素和範圍檢索

GET /_search?q=title:elastichurch~3 AND date:[2016-01-01 TO 2018-12-31]• 1      

1.3.5 使用 DSL檢索(不推薦用于使用者搜尋):

GET /_search
{
  "query": {
    "query_string": {
      "default_field": "content",
      "query": "elastic AND (title:lucene OR title:solr)"
    }
  }
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9      

2. 索引操作

2.1 建立包含設定和mapping的索引

PUT /my_index_name
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 3,
    "analysis": {},
    "refresh_interval": "1s"
  },
  "mappings": {
    "my_type_name": {
      "properties": {
        "title": {
          "type": "text",
          "analyzer": "english"
        }
      }
    }
  }
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19      

2.2 動态的更新設定

PUT /my_index_name/_settings
{
  "index": {
    "refresh_interval": "-1",
    "number_of_replicas": 0
  }
}• 1
• 2
• 3
• 4
• 5
• 6
• 7      

2.3 通過向類型添加字段更新索引

PUT /my_index_name/_mapping/my_type_name
{
  "my_type_name": {
    "properties": {
      "tag": {
        "type": "keyword"
      }
    }
  }
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10      

2.4 擷取Mapping和設定

GET /my_index_name/_mapping
GET /my_index_name/_settings• 1
• 2      

2.5 建立document

POST /my_index_name/my_type_name
{
  "title": "Elastic is funny",
  "tag": [
    "lucene"
  ]
}• 1
• 2
• 3
• 4
• 5
• 6
• 7      

2.6 建立或更新document

PUT /my_index_name/my_type_name/12abc
{
  "title": "Elastic is funny",
  "tag": [
    "lucene"
  ]
}• 1
• 2
• 3
• 4
• 5
• 6
• 7      

2.7 删除文檔

DELETE /my_index_name/my_type_name/12abc• 1      

2.8 打開或關閉索引已節約記憶體和CPU

POST /my_index_name/_close
POST /my_index_name/_open• 1
• 2      

2.9 移除和建立别名

POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "my_index_name",
        "alias": "foo"
      }
    },
    {
      "add": {
        "index": "my_index_name",
        "alias": "bar",
        "filter" : { "term" : { "user" : "damien" } }
      }
    }
  ]
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18      

2.10 列舉别名

GET /_aliases
GET /my_index_name/_alias/*
GET /*/_alias/*
GET /*/_alias/foo• 1
• 2
• 3
• 4      

2.11 索引監控和資訊

GET /my_index_name/_stats
GET /my_index_name/_segments
GET /my_index_name/_recovery?pretty&human• 1
• 2
• 3      

2.12 索引狀态和管理

POST /my_index_name/_cache/clear
POST /my_index_name/_refresh
POST /my_index_name/_flush
POST /my_index_name/_forcemerge
POST /my_index_name/_upgrade
GET /my_index_name/_upgrade?pretty&human• 1
• 2
• 3
• 4
• 5
• 6      

3. 調試和部署

3.1 檢索調試

3.1.1 擷取query操作到底做了什麼?

GET /blog/post/_validate/query?explain
{
  "query": {
    "match": {
      "title": "Smith"
    }
  }
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8      

3.1.2 擷取文檔是否比對?

GET /blog/post/1/_explain
{
  "query": {
    "match": {
      "title": "Smith"
    }
  }
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8      

3.2 分析

3.2.1 測試内容如何在文檔中被标記?

GET /blog/_analyze?field=title&text=powerful• 1      

3.2.2 測試分析器輸出?

GET /_analyze?analyzer=english&text=powerful• 1      

3.3 叢集管理和插件管理

3.3.1 叢集和節點資訊

GET /_cluster/health?pretty
GET /_cluster/health?wait_for_status=yellow&timeout=50s
GET /_cluster/state
GET /_cluster/stats?human&pretty
GET /_cluster/pending_tasks
GET /_nodes
GET /_nodes/stats
GET /_nodes/nodeId1,nodeId2/stats• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8      

3.3.2 手動移動分片

索引1的分片移動到索引2

POST /_cluster/reroute
{
  "commands": [
    {
      "move": {
        "index": "my_index_name",
        "shard": 0,
        "from_node": "node1",
        "to_node": "node2"
      }
    },
    {
      "allocate": {
        "index": "my_index_name",
        "shard": 1,
        "node": "node3"
      }
    }
  ]
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16
• 17
• 18
• 19
• 20      

3.3.3 更新設定

動态更新最小節點數。

PUT /_cluster/settings
{
  "persistent": {
    "discovery.zen.minimum_master_nodes": 3
  }
}
PUT /_cluster/settings
{
  "transient": {
    "discovery.zen.minimum_master_nodes": 2
  }
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13      

使分片失效,在rolling重新開機前有效。

PUT /_cluster/settings
{
    "transient" : {
        "cluster.routing.allocation.enable" : "none"
    }
}
PUT /_cluster/settings
{
    "transient" : {
        "cluster.routing.allocation.enable" : "all"
    }
}• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13      

4.最有用的插件

ES5.X版本已不再支援站點插件,請檢視kibana應用或類似Cerebro等其他獨立的app做管理。

Cerebro位址:

https://github.com/lmenezes/cerebro

Analysis ICU

從Unicode ICU庫中添加有用的tokenizer和令牌過濾器。

bin/elasticsearch-plugin install analysis-icu• 1      

AWS Cloud

允許在Amazon雲(EC2和S3)中發現和存儲。

bin / elasticsearch-plugin install discovery-ec2 bin / elasticsearch-plugin install repository-s3• 1      

Azure Cloud

允許在微軟雲(EC2和S3)中發現和存儲。

bin/elasticsearch-plugin install discovery-azure-classicbin/elasticsearch-plugin install repository-azure• 1      

插件管理:

bin/elasticsearch-plugin install file:///path/to/plugin
bin/elasticsearch-plugin list
bin/elasticsearch-plugin remove [pluginname]• 1
• 2
• 3      

5.其他資訊

5.1 如何發現其他插件

RPM: /usr/share/elasticsearch/bin
Debian: /usr/share/elasticsearch/bin• 1
• 2      

5.2 預設端口

Kibana: http://localhost:5601/.
Elasticsearch: http://localhost:9200/.• 1
• 2
• 3      

5.3 如何設定堆大小

單一Elasticsearch 伺服器最大可用記憶體小于總記憶體的50%,且小于32GB;

假定你使用Ubuntu/Debian 伺服器,你可以通過如下配置修改:

/etc/security/limits.conf
elasticsearch - nofile 65535
elasticsearch - memlock unlimited• 1
• 2
• 3
• 4      

對于Centos伺服器,修改配置如下:

/etc/default/elasticsearch (on CentOS/RH: /etc/sysconfig/elasticsearch)
ES_HEAP_SIZE=20g
MAX_OPEN_FILES=65535
MAX_LOCKED_MEMORY=unlimited• 1
• 2
• 3
• 4
• 5      

5.4 elasticsearch.yml中有用的需要改變的配置

cluster.name: jolicluster
node.name: ${HOSTNAME}
discovery.zen.ping.unicast.hosts: ["front01", "front02"]
discovery.zen.minimum_master_nodes: 2
network.host: _site_
network.bind_host: [_site_, _local_]
plugin.mandatory: analysis-icu
node.data: true
node.master: true
bootstrap.memory_lock: true
action.auto_create_index: +aaa*,-bbb*,+ccc*,-*• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11      

6. 小結

本文是翻譯的

http://elasticsearch-cheatsheet.jolicode.com/#es5

Cheat sheet是作弊表的意思。可見源作者對文章寄托的意義就是最核心、最關鍵的ES知識點“小抄”。

我把它了解成ES學習中待翻看的字典。

尤其第0章節的相關連結,都是上乘ES佳作,待進一步深度剖析。

繼續閱讀