1.查看索引以及删除之前的测试索引

1. 查看索引以及索引数量信息

  1. liqiang@root MINGW64 ~/Desktop
  2. $ curl -X GET http://127.0.0.1:9200/_cat/indices
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 100 415 100 415 0 0 8829 0 --:--:-- --:--:-- --:--:-- 8829yellow open .kibana_task_manager_1 lXR5nwrFSiCplqY52qoG5g 1 1 2 0 12.4kb 12.4kb
  6. yellow open .apm-agent-configuration bPcoddBFSEa_ZR9mTuVEYA 1 1 0 0 283b 283b
  7. yellow open orders bZ1MarlySOCNFrK5NRX-9Q 1 1 21 0 15.8kb 15.8kb
  8. yellow open accounts mqSfqnX5Rt2O-rmVbqOXyQ 1 1 2 0 9kb 9kb
  9. yellow open .kibana_1 bznW8eeKSC-kSfAhBhPD4w 1 1 12 4 39.8kb 39.8kb

2.删除accounts和orders

1. 第一种使用kibana删除

  1. DELETE accounts

2. 第二种 使用curl命令删除

  1. liqiang@root MINGW64 ~/Desktop
  2. $ curl -X DELETE http://localhost:9200/orders
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 100 21 100 21 0 0 18 0 0:00:01 0:00:01 --:--:-- 18{"acknowledged":true}
  6.  
  7. liqiang@root MINGW64 ~/Desktop
  8. $ curl -X GET http://127.0.0.1:9200/_cat/indices
  9. % Total % Received % Xferd Average Speed Time Time Time Current
  10. Dload Upload Total Spent Left Speed
  11. 100 249 100 249 0 0 5413 0 --:--:-- --:--:-- --:--:-- 8032yellow open .kibana_task_manager_1 lXR5nwrFSiCplqY52qoG5g 1 1 2 0 12.4kb 12.4kb
  12. yellow open .apm-agent-configuration bPcoddBFSEa_ZR9mTuVEYA 1 1 0 0 283b 283b
  13. 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.不指定分片数量、副本数量以及字段

  1. liqiang@root MINGW64 ~/Desktop
  2. $ curl -X PUT "localhost:9200/empty?pretty"
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 100 81 100 81 0 0 32 0 0:00:02 0:00:02 --:--:-- 33{
  6. "acknowledged" : true,
  7. "shards_acknowledged" : true,
  8. "index" : "empty"
  9. }

(1)查看索引信息:

  1. liqiang@root MINGW64 ~/Desktop
  2. $ curl -X GET http://localhost:9200/empty/_settings?pretty
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 100 328 100 328 0 0 10250 0 --:--:-- --:--:-- --:--:-- 320k{
  6. "empty" : {
  7. "settings" : {
  8. "index" : {
  9. "creation_date" : "1596946640278",
  10. "number_of_shards" : "1",
  11. "number_of_replicas" : "1",
  12. "uuid" : "UVn4Da93RjK4uwkMtAkcjA",
  13. "version" : {
  14. "created" : "7060299"
  15. },
  16. "provided_name" : "empty"
  17. }
  18. }
  19. }
  20. }

(2)查看字段映射关系

  1. liqiang@root MINGW64 ~/Desktop
  2. $ curl -X GET http://localhost:9200/empty/_mapping?pretty=true
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 100 43 100 43 0 0 1387 0 --:--:-- --:--:-- --:--:-- 2687{
  6. "empty" : {
  7. "mappings" : { }
  8. }
  9. }

2.指定分片数量、副本数量以及字段映射

(1)创建

PUT http://localhost:9200/empty2?pretty=true

body如下:

  1. {
  2. "settings": {
  3. "number_of_shards": 3,
  4. "number_of_replicas": 2
  5. },
  6. "mappings": {
  7. "properties": {
  8. "userid": {
  9. "type": "long"
  10. },
  11. "username": {
  12. "type": "text"
  13. },
  14. "fullname": {
  15. "type": "keyword"
  16. },
  17. "age": {
  18. "type": "double"
  19. }
  20.  
  21. }
  22. }
  23. }

我是用postman执行后返回结果如下:(当然kibana中也可以执行)

  1. {
  2. "acknowledged": true,
  3. "shards_acknowledged": true,
  4. "index": "empty2"
  5. }

(2)查看:

  1. liqiang@root MINGW64 ~/Desktop
  2. $ curl -X GET http://localhost:9200/empty2/_settings?pretty
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 100 330 100 330 0 0 7021 0 --:--:-- --:--:-- --:--:-- 20625{
  6. "empty2" : {
  7. "settings" : {
  8. "index" : {
  9. "creation_date" : "1596951227408",
  10. "number_of_shards" : "3",
  11. "number_of_replicas" : "2",
  12. "uuid" : "lC4z_xeqQ7uYUEJZwtXBBw",
  13. "version" : {
  14. "created" : "7060299"
  15. },
  16. "provided_name" : "empty2"
  17. }
  18. }
  19. }
  20. }
  21.  
  22. liqiang@root MINGW64 ~/Desktop
  23. $ curl -X GET http://localhost:9200/empty2/_mapping?pretty=true
  24. % Total % Received % Xferd Average Speed Time Time Time Current
  25. Dload Upload Total Spent Left Speed
  26. 100 316 100 316 0 0 10193 0 --:--:-- --:--:-- --:--:-- 308k{
  27. "empty2" : {
  28. "mappings" : {
  29. "properties" : {
  30. "age" : {
  31. "type" : "double"
  32. },
  33. "fullname" : {
  34. "type" : "keyword"
  35. },
  36. "userid" : {
  37. "type" : "long"
  38. },
  39. "username" : {
  40. "type" : "text"
  41. }
  42. }
  43. }
  44. }
  45. }

补充:在ES7中,默认的类型type是_doc。

3. 创建数据-kibana中执行

1. 在empty中创建文档

  1. POST /empty/_doc
  2. {
  3. "name": "zhi",
  4. "lastName": "qiao",
  5. "job": "enginee"
  6. }

结果:

  1. {
  2. "_index" : "empty",
  3. "_type" : "_doc",
  4. "_id" : "AJe80XMBntNcepW1OmVE",
  5. "_version" : 1,
  6. "result" : "created",
  7. "_shards" : {
  8. "total" : 2,
  9. "successful" : 1,
  10. "failed" : 0
  11. },
  12. "_seq_no" : 0,
  13. "_primary_term" : 1
  14. }

查看字段映射:

  1. liqiang@root MINGW64 ~/Desktop
  2. $ curl -X GET http://localhost:9200/empty/_mapping?pretty=true
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 100 683 100 683 0 0 22032 0 --:--:-- --:--:-- --:--:-- 666k{
  6. "empty" : {
  7. "mappings" : {
  8. "properties" : {
  9. "job" : {
  10. "type" : "text",
  11. "fields" : {
  12. "keyword" : {
  13. "type" : "keyword",
  14. "ignore_above" : 256
  15. }
  16. }
  17. },
  18. "lastName" : {
  19. "type" : "text",
  20. "fields" : {
  21. "keyword" : {
  22. "type" : "keyword",
  23. "ignore_above" : 256
  24. }
  25. }
  26. },
  27. "name" : {
  28. "type" : "text",
  29. "fields" : {
  30. "keyword" : {
  31. "type" : "keyword",
  32. "ignore_above" : 256
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }

2.  在empty2中创建文档

  1. POST /empty2/_doc
  2. {
  3. "name": "zhi",
  4. "lastName": "qiao",
  5. "job": "enginee"
  6. }

结果:(添加不存在的field,ES会在原Type增加field)

  1. {
  2. "_index" : "empty2",
  3. "_type" : "_doc",
  4. "_id" : "AZe90XMBntNcepW1N2Vv",
  5. "_version" : 1,
  6. "result" : "created",
  7. "_shards" : {
  8. "total" : 3,
  9. "successful" : 1,
  10. "failed" : 0
  11. },
  12. "_seq_no" : 0,
  13. "_primary_term" : 1
  14. }

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.创建一个用户索引,如下:

  1. put /u
  2. {
  3. "mappings": {
  4. "properties": {
  5. "full_name": {
  6. "type": "text"
  7. },
  8. "idcard": {
  9. "type": "keyword"
  10. }
  11. }
  12. }
  13. }

结果:

  1. {
  2. "acknowledged" : true,
  3. "shards_acknowledged" : true,
  4. "index" : "u"
  5. }

2.查看字段映射

GET /u/_mapping?pretty=true

结果:

  1. {
  2. "u" : {
  3. "mappings" : {
  4. "properties" : {
  5. "full_name" : {
  6. "type" : "text"
  7. },
  8. "idcard" : {
  9. "type" : "keyword"
  10. }
  11. }
  12. }
  13. }
  14. }

3.创建数据如下后搜索:

  1. POST /u/_doc
  2. {
  3. "full_name": "张三",
  4. "idcard": "zhang san"
  5. }

搜索:

(1)按关键字张搜索

  1. GET /u/_search?q=张

结果:

  1. {
  2. "took" : 4,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 0.9808291,
  16. "hits" : [
  17. {
  18. "_index" : "u",
  19. "_type" : "_doc",
  20. "_id" : "BJfc0XMBntNcepW1F2Vj",
  21. "_score" : 0.9808291,
  22. "_source" : {
  23. "full_name" : "张三",
  24. "idcard" : "zhang san"
  25. }
  26. }
  27. ]
  28. }
  29. }

(2)按关键字zhang搜索:

  1. GET /u/_search?q=zhang

结果:

  1. {
  2. "took" : 5,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 0,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. }
  18. }

(3)按关键字 zhang san搜索

  1. GET /u/_search?q=zhang san

结果:

  1. {
  2. "took" : 4,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 0.9808291,
  16. "hits" : [
  17. {
  18. "_index" : "u",
  19. "_type" : "_doc",
  20. "_id" : "BJfc0XMBntNcepW1F2Vj",
  21. "_score" : 0.9808291,
  22. "_source" : {
  23. "full_name" : "张三",
  24. "idcard" : "zhang san"
  25. }
  26. }
  27. ]
  28. }
  29. }

说明: full_name可分词,而idcard未分词。

4.删掉上面数据

  1. DELETE /u/_doc/BJfc0XMBntNcepW1F2Vj

5.再次增加数据反向测试搜索

  1. POST /u/_doc
  2. {
  3. "full_name": "zhang san",
  4. "idcard": "张三"
  5. }

结果:

  1. {
  2. "_index" : "u",
  3. "_type" : "_doc",
  4. "_id" : "BZfk0XMBntNcepW1L2Xg",
  5. "_version" : 1,
  6. "result" : "created",
  7. "_shards" : {
  8. "total" : 2,
  9. "successful" : 1,
  10. "failed" : 0
  11. },
  12. "_seq_no" : 4,
  13. "_primary_term" : 1
  14. }

(1)按关键字zhang 搜索

  1. GET /u/_search?q=zhang

结果可以搜到:

  1. {
  2. "took" : 4,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 0.9808291,
  16. "hits" : [
  17. {
  18. "_index" : "u",
  19. "_type" : "_doc",
  20. "_id" : "BZfk0XMBntNcepW1L2Xg",
  21. "_score" : 0.9808291,
  22. "_source" : {
  23. "full_name" : "zhang san",
  24. "idcard" : "张三"
  25. }
  26. }
  27. ]
  28. }
  29. }

(2)按关键字张搜索

  1. GET /u/_search?q=张

结果:未搜到。

  1. {
  2. "took" : 5,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 0,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. }
  18. }

ES索引Index相关操作&ES数据类型、字符串类型text和keyword区别的更多相关文章

  1. C#路径的八种相关操作,判断字符串是否为路径等

    原文:C#路径的八种相关操作,判断字符串是否为路径等 1.判定一个给定的C#路径是否有效,合法 通过Path.GetInvalidPathChars或Path.GetInvalidFileNameCh ...

  2. 第二百九十五节,python操作redis缓存-字符串类型

    python操作redis缓存-字符串类型 首先要安装redis-py模块 python连接redis方式,有两种连接方式,一种是直接连接,一张是通过连接池连接 注意:以后我们都用的连接池方式连接,直 ...

  3. redis数据类型-字符串类型

    Redis数据类型 字符串类型 字符串类型是Redis中最基本的数据类型,它能存储任何形式的字符串,包括二进制数据.你可以用其存储用户的邮箱.JSON化的对象甚至是一张图片.一个字符串类型键允许存储的 ...

  4. ES系列十一、ES的index、store、_source、copy_to和all的区别

    1.基本概念 1.1._source 存储的原始数据._source中的内容就是搜索api返回的内容,如: { "query":{  "term":{   &q ...

  5. python学习笔记(5)-基本数据类型-字符串类型及操作

    一.字符串 字符串由一对单引号或者双引号表示,如”abc“,‘中国’,字符串是字符的有序序列,可以对其中的字符进行索引.字符串也可以用三单引号或三双引号表示,可以表示多行字符串,一对单引号或双引号仅表 ...

  6. python学习笔记(5-1)-基本数据类型-字符串类型及操作

    五.字符串处理函数  len(x):字符串x的长度.如len("12345")结果为5  str(x):任意类型x所对应的字符串形式. >>> str(123) ...

  7. Delphi 常用数据类型 字符串类型 数据类型等等

    字符串类型 Delphi有三种类型的字符: ●AnsiChar这是标准的1字节的ANSI字符,程序员都对它比较熟悉. ●WideChar这是2字节的Unicode字符. ●Char在目前Delphi早 ...

  8. python 学习笔记_1 pip安装、卸载、更新包相关操作及数据类型学习

    '''prepare_1 pip安装.卸载.更新组件type 各数据类型''' py -3 -m pip py -3 -m pip listpy -3 -m pip show nosepy -3 -m ...

  9. python数据类型——字符串类型

    字符串(string) 字符串,就是字符连成一串,是由字符组成的序列.字符串有编码问题,在之前我已经讲过.本节主要讲字符串的使用. 创建字符串,不用多说: a='123abcd' b='diamond ...

随机推荐

  1. 【葵花宝典】All-in-One模式安装KubeSphere

    1.准备 Linux 机器 2.google api受限下载 KubeKey export KKZONE=cn curl -sfL https://get-kk.kubesphere.io | VER ...

  2. Pandas应用案例-股票分析:使用tushare包获取股票的历史行情数据进行数据分析

    目标: 使用tushare包获取股票的历史行情数据 输出该股票所有收盘比开盘上涨3%以上的日期 输出该股票所有开盘比前日收盘跌幅超过2%以上的日期 假如为我们从2010年1月1日开始,每月第一个交易日 ...

  3. ldf和mdf文件怎么还原到sqlserver数据库

    1.把mdf文件和ldf文件拷贝到数据库的默认路径C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA里:2.在sq ...

  4. Flutter--Flutter开发环境搭建

    一.前言 Flutter 是 Google推出并开源的移动应用开发框架,主打跨平台.高保真.高性能.开发者可以通过 Dart语言开发 App,一套代码同时运行在 iOS 和 Android平台. Fl ...

  5. git的使用学习笔记--项目版本操作

    一.使用场景 版本回退:上线失败--需要回退到上个版本 二.操作 先编辑  vim text.txt git status git add .       这个命令能看到所有的增加操作 git com ...

  6. 支持 gRPC 长链接,深度解读 Nacos 2.0 架构设计及新模型

    支持 gRPC 长链接,深度解读 Nacos 2.0 架构设计及新模型 原创 杨翊(席翁) 阿里巴巴云原生 2020-12-28    

  7. RPC 接口必须是业务职责

    https://mp.weixin.qq.com/s/MYSF8lCF92ItG_Lc8nOspg 一个加班多新人多团队,我们的代码问题与重构 陈于喆 高可用架构 2020-10-21   微服务编码 ...

  8. luoguP6754 [BalticOI 2013 Day1] Palindrome-Free Numbers

    目录 luoguP6754 [BalticOI 2013 Day1] Palindrome-Free Numbers 简述题意: Solution: Code luoguP6754 [BalticOI ...

  9. LOJ10076

    USACO 2006 Nov. Gold 贝茜把家搬到了一个小农场,但她常常回到 FJ 的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而 ...

  10. Spring听课笔记(tg)

    0. 地址:https://www.bilibili.com/video/av21335209 1.综述,Spring主要的复习要点集中在以下几点 -- Spring的整体结构,Maven依赖(环境搭 ...