ES索引Index相关操作ES数据类型、字符串类型text和keyword区别
1.查看索引以及删除之前的测试索引
1. 查看索引以及索引数量信息
liqiang@root MINGW64 ~/Desktop
$ curl -X GET 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 localhost:9200/orders
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
1002110021001800:00:010:00:01 --:--:--    18{"acknowledged":true}
liqiang@root MINGW64 ~/Desktop
$ curl -X GET 127.0.0.1:9200/_cat/indices
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
1002491002490054130 --:--:-- --:--:-- --:--:--  8032yellow open .kibana_task_manager_1  lXR5nwrFSiCplqY52qoG5g 112012.4kb 12.4kb
yellow open .apm-agent-configuration bPcoddBFSEa_ZR9mTuVEYA 1100  283b  283b
yellow open .kibana_1                bznW8eeKSC-kSfAhBhPD4w 1113240.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
1008110081003200:00:020:00:02 --:--:--    33{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "empty"
}
(1)查看索引信息:
liqiang@root MINGW64 ~/Desktop
$ curl -X GET localhost:9200/empty/_settings?pretty
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
10032810032800102500 --:--:-- --:--:-- --:--:--  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 localhost:9200/empty/_mapping?pretty=true
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current
Dload  Upload  Total  Spent    Left  Speed
10043100430013870 --:--:-- --:--:-- --:--:--  2687{
"empty" : {
"mappings" : { }
}
}
2.指定分⽚数量、副本数量以及字段映射
(1)创建
PUT 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 localhost:9200/empty2/_settings?pretty
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current                                  Dload  Upload  Total  Spent    Left  Speed 1003301003300070210 --:--:-- --:--:-- --:--:-- 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 localhost:9200/empty2/_mapping?pretty=true
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current                                  Dload  Upload  Total  Spent    Left  Speed 10031610031600101930 --:--:-- --:--:-- --:--:--  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 localhost:9200/empty/_mapping?pretty=true
% Total    % Received % Xferd  Average Speed  Time    Time    Time  Current                                  Dload  Upload  Total  Spent    Left  Speed 10068310068300220320 --:--:-- --:--:-- --:--:--  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.创建数据如下后搜索:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。