1.查看索引以及刪除之前的測試索引
1. 查看索引以及索引數量信息
liqiang@root MINGW64 ~/Desktop $ curl -X GET http://127.0.0.1:9200/_cat/indices % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 415 100 415 0 0 8829 0 --:--:-- --:--:-- --:--:-- 8829yellow open .kibana_task_manager_1 lXR5nwrFSiCplqY52qoG5g 1 1 2 0 12.4kb 12.4kb yellow open .apm-agent-configuration bPcoddBFSEa_ZR9mTuVEYA 1 1 0 0 283b 283b yellow open orders bZ1MarlySOCNFrK5NRX-9Q 1 1 21 0 15.8kb 15.8kb yellow open accounts mqSfqnX5Rt2O-rmVbqOXyQ 1 1 2 0 9kb 9kb yellow open .kibana_1 bznW8eeKSC-kSfAhBhPD4w 1 1 12 4 39.8kb 39.8kb
2.刪除accounts和orders
1. 第一種使用kibana刪除
DELETE accounts
2. 第二種 使用curl命令刪除
liqiang@root MINGW64 ~/Desktop $ curl -X DELETE http://localhost:9200/orders % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 21 100 21 0 0 18 0 0:00:01 0:00:01 --:--:-- 18{"acknowledged":true} liqiang@root MINGW64 ~/Desktop $ curl -X GET http://127.0.0.1:9200/_cat/indices % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 249 100 249 0 0 5413 0 --:--:-- --:--:-- --:--:-- 8032yellow open .kibana_task_manager_1 lXR5nwrFSiCplqY52qoG5g 1 1 2 0 12.4kb 12.4kb yellow open .apm-agent-configuration bPcoddBFSEa_ZR9mTuVEYA 1 1 0 0 283b 283b yellow open .kibana_1 bznW8eeKSC-kSfAhBhPD4w 1 1 13 2 40.1kb 40.1kb
補充:kibana也可以查看索引信息

2. 創建新的索引
0.分片與副本
對於一個索引來說,number_of_shards只能設置一次,而number_of_replicas可以使用索引更新設置API在任何時候被增加或者減少。
分片:shard。
Elasticsearch集群允許系統存儲的數據量超過單機容量,實現這一目標引入分片策略shard。在一個索引index中,數據(document)被分片處理(sharding)到多個分片上。Elasticsearch屏蔽了管理分片的復雜性,使得多個分片呈現出一個大索引的樣子。
副本:replica
為了提升訪問壓力過大是單機無法處理所有請求的問題,Elasticsearch集群引入了副本策略replica。副本策略對index中的每個分片創建冗余的副本,處理查詢時可以把這些副本當做主分片來對待(primary shard),此外副本策略提供了高可用和數據安全的保障,當分片所在的機器宕機,Elasticsearch可以使用其副本進行恢復,從而避免數據丟失。
1.不指定分片數量、副本數量以及字段
liqiang@root MINGW64 ~/Desktop $ curl -X PUT "localhost:9200/empty?pretty" % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 81 100 81 0 0 32 0 0:00:02 0:00:02 --:--:-- 33{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "empty" }
(1)查看索引信息:
liqiang@root MINGW64 ~/Desktop $ curl -X GET http://localhost:9200/empty/_settings?pretty % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 328 100 328 0 0 10250 0 --:--:-- --:--:-- --:--:-- 320k{ "empty" : { "settings" : { "index" : { "creation_date" : "1596946640278", "number_of_shards" : "1", "number_of_replicas" : "1", "uuid" : "UVn4Da93RjK4uwkMtAkcjA", "version" : { "created" : "7060299" }, "provided_name" : "empty" } } } }
(2)查看字段映射關系
liqiang@root MINGW64 ~/Desktop $ curl -X GET http://localhost:9200/empty/_mapping?pretty=true % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 43 100 43 0 0 1387 0 --:--:-- --:--:-- --:--:-- 2687{ "empty" : { "mappings" : { } } }
2.指定分片數量、副本數量以及字段映射
(1)創建
PUT http://localhost:9200/empty2?pretty=true
body如下:
{ "settings": { "number_of_shards": 3, "number_of_replicas": 2 }, "mappings": { "properties": { "userid": { "type": "long" }, "username": { "type": "text" }, "fullname": { "type": "keyword" }, "age": { "type": "double" } } } }
我是用postman執行后返回結果如下:(當然kibana中也可以執行)
{ "acknowledged": true, "shards_acknowledged": true, "index": "empty2" }
(2)查看:
liqiang@root MINGW64 ~/Desktop $ curl -X GET http://localhost:9200/empty2/_settings?pretty % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 330 100 330 0 0 7021 0 --:--:-- --:--:-- --:--:-- 20625{ "empty2" : { "settings" : { "index" : { "creation_date" : "1596951227408", "number_of_shards" : "3", "number_of_replicas" : "2", "uuid" : "lC4z_xeqQ7uYUEJZwtXBBw", "version" : { "created" : "7060299" }, "provided_name" : "empty2" } } } } liqiang@root MINGW64 ~/Desktop $ curl -X GET http://localhost:9200/empty2/_mapping?pretty=true % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 316 100 316 0 0 10193 0 --:--:-- --:--:-- --:--:-- 308k{ "empty2" : { "mappings" : { "properties" : { "age" : { "type" : "double" }, "fullname" : { "type" : "keyword" }, "userid" : { "type" : "long" }, "username" : { "type" : "text" } } } } }
補充:在ES7中,默認的類型type是_doc。
3. 創建數據-kibana中執行
1. 在empty中創建文檔
POST /empty/_doc { "name": "zhi", "lastName": "qiao", "job": "enginee" }
結果:
{ "_index" : "empty", "_type" : "_doc", "_id" : "AJe80XMBntNcepW1OmVE", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }
查看字段映射:
liqiang@root MINGW64 ~/Desktop $ curl -X GET http://localhost:9200/empty/_mapping?pretty=true % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 683 100 683 0 0 22032 0 --:--:-- --:--:-- --:--:-- 666k{ "empty" : { "mappings" : { "properties" : { "job" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "lastName" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } } }
2. 在empty2中創建文檔
POST /empty2/_doc { "name": "zhi", "lastName": "qiao", "job": "enginee" }
結果:(添加不存在的field,ES會在原Type增加field)
{ "_index" : "empty2", "_type" : "_doc", "_id" : "AZe90XMBntNcepW1N2Vv", "_version" : 1, "result" : "created", "_shards" : { "total" : 3, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }
4.ES數據類型
1. 字段數據類型-自定義字段的屬性
Alias、Arrays、Binary、Boolean、Date、Date nanoseconds、Dense vector、Histogram、Flattened、Geo-point、Geo-shape、IP、Join、Keyword、Nested、Numeric、Object、Percolator、Range、Rank feature、Rank features、Search-as-you-type、Sparse vector、Text、Token count、Shape、Constant keyword
2. Metadata fields (元屬性)-ES生成的默認屬性
_field_names field、_ignored field、_id field、_index field、_meta field、_routing field、_source field、_type field
3. ES字符串String數據類型keyword 和 text 數據類型區別的區別
引用官網的介紹:
1. keyword
A field to index structured content such as IDs, email addresses, hostnames, status codes, zip codes or tags.
They are typically used for filtering (Find me all blog posts where status is published), for sorting, and for aggregations. Keyword fields are only searchable by their exact value.
If you need to index full text content such as email bodies or product descriptions, it is likely that you should rather use a text field.
簡單理解就是 Keyword 數據類型用來建立電子郵箱地址、姓名、郵政編碼和標簽等數據,不需要進行分詞,只能用精准搜素。可以被用來檢索過濾、排序和聚合。
2. text
A field to index full-text values, such as the body of an email or the description of a product. These fields are analyzed, that is they are passed through an analyzer to convert the string into a list of individual terms before being indexed. The analysis process allows Elasticsearch to search for individual words within each full text field. Text fields are not used for sorting and seldom used for aggregations (although the significant text aggregation is a notable exception).
簡單理解就是:Text 數據類型被用來索引長文本,比如說電子郵件的主體部分或者一款產品的介紹。這些文本會被分析,在建立索引前會將這些文本進行分詞,轉化為詞的組合,建立索引。允許 ES來檢索這些詞語。text 數據類型不能用來排序和聚合
注意: 遇到字符串類型時候的字端,系統會默認為“text”類型。檢索的時候對字符串進行分析。所以要想只通過字段本身來進行檢索,還是需要按照上面把該字段改為“keyword”類型。
例如:(kibana中執行)
1.創建一個用戶索引,如下:
put /u { "mappings": { "properties": { "full_name": { "type": "text" }, "idcard": { "type": "keyword" } } } }
結果:
{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "u" }
2.查看字段映射
GET /u/_mapping?pretty=true
結果:
{ "u" : { "mappings" : { "properties" : { "full_name" : { "type" : "text" }, "idcard" : { "type" : "keyword" } } } } }
3.創建數據如下后搜索:
POST /u/_doc { "full_name": "張三", "idcard": "zhang san" }
搜索:
(1)按關鍵字張搜索
GET /u/_search?q=張
結果:
{ "took" : 4, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.9808291, "hits" : [ { "_index" : "u", "_type" : "_doc", "_id" : "BJfc0XMBntNcepW1F2Vj", "_score" : 0.9808291, "_source" : { "full_name" : "張三", "idcard" : "zhang san" } } ] } }
(2)按關鍵字zhang搜索:
GET /u/_search?q=zhang
結果:
{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] } }
(3)按關鍵字 zhang san搜索
GET /u/_search?q=zhang san
結果:
{ "took" : 4, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.9808291, "hits" : [ { "_index" : "u", "_type" : "_doc", "_id" : "BJfc0XMBntNcepW1F2Vj", "_score" : 0.9808291, "_source" : { "full_name" : "張三", "idcard" : "zhang san" } } ] } }
說明: full_name可分詞,而idcard未分詞。
4.刪掉上面數據
DELETE /u/_doc/BJfc0XMBntNcepW1F2Vj
5.再次增加數據反向測試搜索
POST /u/_doc { "full_name": "zhang san", "idcard": "張三" }
結果:
{ "_index" : "u", "_type" : "_doc", "_id" : "BZfk0XMBntNcepW1L2Xg", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 4, "_primary_term" : 1 }
(1)按關鍵字zhang 搜索
GET /u/_search?q=zhang
結果可以搜到:
{ "took" : 4, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.9808291, "hits" : [ { "_index" : "u", "_type" : "_doc", "_id" : "BZfk0XMBntNcepW1L2Xg", "_score" : 0.9808291, "_source" : { "full_name" : "zhang san", "idcard" : "張三" } } ] } }
(2)按關鍵字張搜索
GET /u/_search?q=張
結果:未搜到。
{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] } }
