Elasticsearch字段数据范例

[复制链接]
发表于 2026-1-8 22:40:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1. 前言

ES文档的每个字段都至少有一个数据范例,此范例决定了字段值怎样被存储以及检索。比方,字符串范例可以界说为text大概keyword,前者用于全文检索,会颠末分词后索引;后者用于精准匹配,值会保持原样被索引。
ES字段范例按族分组,同一族中的范例具有完全雷同的搜刮举动,但大概具有差异的空间利用或性能特性。
2. 根本数据范例

2.1 binary

二进制数据范例,担当以Base64编码的二进制数据作为输入,默认不可索引和搜刮。
  1. // 创建索引
  2. PUT files
  3. {
  4.   "mappings": {
  5.     "properties": {
  6.       "title":{
  7.         "type": "text"
  8.       },
  9.       "blob":{
  10.         "type": "binary"
  11.       }
  12.     }
  13.   }
  14. }
  15. // 索引文档
  16. POST files/_doc
  17. {
  18.   "title":"hello.txt",
  19.   "blob":"aGVsbG8gd29ybGQ="
  20. }
  21. //获取文档
  22. GET files/_search
  23. {
  24.   "took": 30,
  25.   "timed_out": false,
  26.   "_shards": {
  27.     "total": 1,
  28.     "successful": 1,
  29.     "skipped": 0,
  30.     "failed": 0
  31.   },
  32.   "hits": {
  33.     "total": {
  34.       "value": 1,
  35.       "relation": "eq"
  36.     },
  37.     "max_score": 1,
  38.     "hits": [
  39.       {
  40.         "_index": "files",
  41.         "_id": "7-tinY4BODFb3LbQRHQD",
  42.         "_score": 1,
  43.         "_source": {
  44.           "title": "hello.txt",
  45.           "blob": "aGVsbG8gd29ybGQ="
  46.         }
  47.       }
  48.     ]
  49.   }
  50. }
复制代码
二进制数据范例字段不会被索引,固无法被搜刮,否则会报错
  1. {
  2.   "error": {
  3.     "root_cause": [
  4.       {
  5.         "type": "query_shard_exception",
  6.         "reason": "failed to create query: Binary fields do not support searching",
  7.         "index_uuid": "IM89dUaXTuqYCgnmMlb9VQ",
  8.         "index": "files"
  9.       }
  10.     ],
  11.     "type": "search_phase_execution_exception",
  12.     "reason": "all shards failed",
  13.     "phase": "query",
  14.     "grouped": true,
  15.     "failed_shards": [
  16.       {
  17.         "shard": 0,
  18.         "index": "files",
  19.         "node": "X6yldsn_RMuokM0m_Q5Z1g",
  20.         "reason": {
  21.           "type": "query_shard_exception",
  22.           "reason": "failed to create query: Binary fields do not support searching",
  23.           "index_uuid": "IM89dUaXTuqYCgnmMlb9VQ",
  24.           "index": "files",
  25.           "caused_by": {
  26.             "type": "illegal_argument_exception",
  27.             "reason": "Binary fields do not support searching"
  28.           }
  29.         }
  30.       }
  31.     ]
  32.   },
  33.   "status": 400
  34. }
复制代码
2.2 boolean

布尔范例,担当JSON的 true 或 false值,也可以担当对应字符串格式作为输入。
  1. PUT users
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "name":{
  6.         "type": "keyword"
  7.       },
  8.       "deleted":{
  9.         "type": "boolean"
  10.       }
  11.     }
  12.   }
  13. }
  14. POST users/_doc
  15. {
  16.   "name":"Lisa",
  17.   "deleted":false
  18. }
复制代码
2.3 Keywords

关键字范例族,包罗:keyword、constant_keyword和wildcard。
2.3.1 keyword

关键字范例,用于存储结构化内容,如id、电子邮件地点、主机名、状态码、邮政编码或标签。用于精准匹配、聚合、以及排序。
  1. PUT users
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "user_id":{
  6.         "type": "keyword"
  7.       }
  8.     }
  9.   }
  10. }
复制代码
2.3.2 constant_keyword

常量关键字范例,它的目标是让文档中的字段具有雷同的值,什么意思呢?
举个例子,创建一个logs索引,此中level字段界说为constant_keyword范例,值是”debug“
  1. PUT logs
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "content":{
  6.         "type": "text"
  7.       },
  8.       "level":{
  9.         "type": "constant_keyword",
  10.         "value":"debug"
  11.       }
  12.     }
  13.   }
  14. }
复制代码
下面两个索引哀求是等价的,level终极都是“debug”
  1. POST logs/_doc
  2. {
  3.   "content":"haha",
  4.   "level":"debug"
  5. }
  6. POST logs/_doc
  7. {
  8.   "content":"haha"
  9. }
复制代码
但是,假如索引一个非法的level值,就会得到一个非常
  1. POST logs/_doc
  2. {
  3.   "content":"haha",
  4.   "level":"info"
  5. }
  6. {
  7.   "error": {
  8.     "root_cause": [
  9.       {
  10.         "type": "document_parsing_exception",
  11.         "reason": "[3:11] failed to parse field [level] of type [constant_keyword] in document with id '9ut5nY4BODFb3LbQtHRG'. Preview of field's value: 'info'"
  12.       }
  13.     ],
  14.     "type": "document_parsing_exception",
  15.     "reason": "[3:11] failed to parse field [level] of type [constant_keyword] in document with id '9ut5nY4BODFb3LbQtHRG'. Preview of field's value: 'info'",
  16.     "caused_by": {
  17.       "type": "illegal_argument_exception",
  18.       "reason": "[constant_keyword] field [level] only accepts values that are equal to the value defined in the mappings [debug], but got [info]"
  19.     }
  20.   },
  21.   "status": 400
  22. }
复制代码
总结一下,constant_keyword目标是让索引内的文档字段具有雷同的值,假如映射没有界说默认值,则以第一个索引到的不为空的字段值作为默认值,假如再索引到差异的值,ES会抛出非常。
2.3.3 wildcard

通配符字段范例,可以在字符串中实现通配符的模式查找。
如下示例,创建books索引并索引文档
  1. PUT books
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "title":{
  6.         "type": "wildcard"
  7.       }
  8.     }
  9.   }
  10. }
  11. POST books/_doc
  12. {
  13.   "title":"C语言程序设计"
  14. }
复制代码
利用通配符查找书名
  1. GET books/_search
  2. {
  3.   "query": {
  4.     "wildcard": {
  5.       "title": {
  6.         "value": "*语言*设计"
  7.       }
  8.     }
  9.   }
  10. }
  11. {
  12.   "took": 11,
  13.   "timed_out": false,
  14.   "_shards": {
  15.     "total": 1,
  16.     "successful": 1,
  17.     "skipped": 0,
  18.     "failed": 0
  19.   },
  20.   "hits": {
  21.     "total": {
  22.       "value": 1,
  23.       "relation": "eq"
  24.     },
  25.     "max_score": 1,
  26.     "hits": [
  27.       {
  28.         "_index": "books",
  29.         "_id": "9-uInY4BODFb3LbQW3Sn",
  30.         "_score": 1,
  31.         "_source": {
  32.           "title": "C语言程序设计"
  33.         }
  34.       }
  35.     ]
  36.   }
  37. }
复制代码
2.4 Numbers

数字范例族,包罗:long、integer、short、byte、double、float、half_float、scaled_float、unsigned_long。
除了整型和浮点数的区别外,别的就是长度不一样导致表现的数值范围差异。在数据范例的选择上,发起在满意需求的根本上选择占用空间最小的数据范例以节省存储空间和检索性能。
2.5 Dates

日期范例,包罗:date和date_nanos。由于JSON并没有日期范例,以是ES可以吸收的日期输入是:格式化的日期字符串和时间戳。
2.5.1 date

日期范例,可以通过format指定日期的格式。如下示例,担当格式化的日期字符串或时间戳作为输入值
  1. PUT dates
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "date":{
  6.         "type": "date",
  7.         "format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
  8.       }
  9.     }
  10.   }
  11. }
复制代码
以下两种索引哀求均支持
  1. POST dates/_doc
  2. {
  3. "date":"2024-01-01 00:00:00"
  4. }
  5. POST dates/_doc
  6. {
  7. "date":1618321898000
  8. }
复制代码
2.5.2 date_nanos

纳秒级的时间范例,是对日期数据范例的增补。区别是:date利用毫秒精度存储日期、date_nanos利用纳秒精度存储日期,date_nanos日期范围被限定在约莫1970到2262之间,它存储的是自epoch依靠的纳秒时长。
如下示例,吸收格式化的日期字符串或时间戳作为输入
  1. PUT date-nanos
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "date_nanos":{
  6.         "type": "date_nanos",
  7.         "format": "strict_date_optional_time_nanos||epoch_millis"
  8.       }
  9.     }
  10.   }
  11. }
复制代码
索引文档哀求
  1. POST date-nanos/_doc
  2. {
  3. "date_nanos":"2024-01-01T12:00:00.123456789Z"
  4. }
复制代码
2.6 alias

别名范例,它可以用来给索引中的字段界说别名。
如下示例,我们给field_a界说一个别名字段
  1. PUT alias-index
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "field_a":{
  6.         "type": "keyword"
  7.       },
  8.       "field_a_alias":{
  9.         "type": "alias",
  10.         "path":"field_a"
  11.       }
  12.     }
  13.   }
  14. }
复制代码
云云一来,下面两个搜刮哀求是等价的:
  1. GET alias-index/_search
  2. {
  3.   "query": {
  4.     "term": {
  5.       "field_a": {
  6.         "value": "haha"
  7.       }
  8.     }
  9.   }
  10. }
  11. GET alias-index/_search
  12. {
  13.   "query": {
  14.     "term": {
  15.       "field_a_alias": {
  16.         "value": "haha"
  17.       }
  18.     }
  19.   }
  20. }
复制代码
3. 对象和关系范例

3.1 object

JSON文档是分层的,文档大概还会包罗内部对象,如下索引文档哀求示例:
  1. POST users/_doc
  2. {
  3.   "name":"张三",
  4.   "address":{
  5.     "province":"浙江省",
  6.     "city":"杭州市"
  7.   }
  8. }
复制代码
在ES内部,该文档会被索引为简单的键值对列表:
  1. {
  2.   "name":"张三",
  3.   "address.province":"浙江省",
  4.   "address.city":"杭州市"
  5. }
复制代码
索引映射看起来是下面如许的,address包罗province和city两个子字段,address并没有显式的指定type=object,这是默认值。
  1. {
  2.   "users": {
  3.     "mappings": {
  4.       "properties": {
  5.         "address": {
  6.           "properties": {
  7.             "city": {
  8.               "type": "text",
  9.               "fields": {
  10.                 "keyword": {
  11.                   "type": "keyword",
  12.                   "ignore_above": 256
  13.                 }
  14.               }
  15.             },
  16.             "province": {
  17.               "type": "text",
  18.               "fields": {
  19.                 "keyword": {
  20.                   "type": "keyword",
  21.                   "ignore_above": 256
  22.                 }
  23.               }
  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. }
复制代码
3.2 flattened

默认情况下,ES的设置dynamic:true,即答应动态映射添加新字段,假如不加束缚,大概会导致索引映射字段激增,终极高出索引字段数限定index.mapping.total_fields.limit:1000 ,这种非预期的字段数激增被称作”字段膨胀“。
尤其是索引结构复杂的文档,如下示例,会导致索引的字段结构变得紊乱:
  1. POST logs/_doc
  2. {
  3.   "project":"user-server",
  4.   "content":{
  5.     "field_a":{
  6.       "a":{
  7.         "a1":{
  8.           "a1_1":{},
  9.           "a1_2":{},
  10.           "a1_3":{}
  11.         },
  12.         "a2":{},
  13.         "a3":{}
  14.       },
  15.       "b":{
  16.         
  17.       },
  18.       "c":{
  19.         
  20.       }
  21.       ...
  22.     },
  23.     "field_b":{
  24.       ......
  25.     }
  26.   }
  27. }
复制代码
在这种情况下,劈面临索引包罗大量不可猜测字段的文档时,可以将字段范例设置为”flattened“来克制字段膨胀的题目。”flattened“译为”扁平“,它会将整个嵌套的JSON对象索引为单个keyword范例,以淘汰字段总数。
如下示例,将用户地点设为flattened范例,无论address对象结构怎样复杂,索引字段数都不会变。
  1. PUT users
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "name":{
  6.         "type": "keyword"
  7.       },
  8.       "address":{
  9.         "type": "flattened"
  10.       }
  11.     }
  12.   }
  13. }
复制代码
索引如下文档哀求:
  1. POST users/_doc
  2. {
  3.   "name":"张三",
  4.   "address":{
  5.     "province":"浙江省",
  6.     "city":"杭州市",
  7.     "region":"西湖区",
  8.     "street":"文一西路",
  9.     "detail":"某某小区1幢1号"
  10.   }
  11. }
复制代码
检察索引映射,仍旧只有俩字段
  1. GET users/_mapping
  2. {
  3.   "users": {
  4.     "mappings": {
  5.       "properties": {
  6.         "address": {
  7.           "type": "flattened"
  8.         },
  9.         "name": {
  10.           "type": "keyword"
  11.         }
  12.       }
  13.     }
  14.   }
  15. }
复制代码
索引字段更新具备额外的开销,ES必须为每个字段更新集群状态,跨节点的集群状态传输是单线程利用的,必要更新的字段越多,所需的时间就越长,乃至导致整个集群宕机。
3.3 nested

嵌套数据范例,它和object类似也被用来存储JSON对象或数组,它答应以一种可以相互独立查询的方式对对象数组举行索引。什么意思呢?
看一个例子,创建users索引,并索引文档,lisa有男性Jack和女性Ruth两位朋侪。
  1. PUT users
  2. POST users/_doc
  3. {
  4.   "name":"lisa",
  5.   "friends":[{
  6.     "name":"Jack",
  7.     "gender":"男"
  8.   },{
  9.     "name":"Ruth",
  10.     "gender":"女"
  11.   }]
  12. }
复制代码
接下来,我们实行bool查询,搜刮具有女性Jack朋侪的用户,按照正常的业务逻辑不应该召回任何文档,但是结果出乎料想地返回了lisa
  1. GET users/_search
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "must": [
  6.         {
  7.           "match": {
  8.             "friends.name": "Jack"
  9.           }
  10.         },
  11.         {
  12.           "match": {
  13.             "friends.gender": "女"
  14.           }
  15.         }
  16.       ]
  17.     }
  18.   }
  19. }
  20. {
  21.   "took": 3,
  22.   "timed_out": false,
  23.   "_shards": {
  24.     "total": 1,
  25.     "successful": 1,
  26.     "skipped": 0,
  27.     "failed": 0
  28.   },
  29.   "hits": {
  30.     "total": {
  31.       "value": 1,
  32.       "relation": "eq"
  33.     },
  34.     "max_score": 0.5753642,
  35.     "hits": [
  36.       {
  37.         "_index": "users",
  38.         "_id": "COvanY4BODFb3LbQgXW-",
  39.         "_score": 0.5753642,
  40.         "_source": {
  41.           "name": "lisa",
  42.           "friends": [
  43.             {
  44.               "name": "Jack",
  45.               "gender": "男"
  46.             },
  47.             {
  48.               "name": "Ruth",
  49.               "gender": "女"
  50.             }
  51.           ]
  52.         }
  53.       }
  54.     ]
  55.   }
  56. }
复制代码
出现这种情况的缘故原由是:ES文档没有内部对象的概念,它会将对象条理结构扁平化为一个简单的键值对列表,值利用数组存储,就像如许:
  1. {
  2.     "name":"lisa",
  3.     "friends.name":["Jack","Ruth"],
  4.     "friends.gender":["男","女"]
  5. }
复制代码
单个friend的关系已经丢失,示例中的搜刮条件是:(“Jack” in friends.name) and (“女” in friends.gender) ,以是lisa被召回就可以明白了。
要办理这个题目,本质上是要索引对象数组并维护数组中每个对象的独立性,此时可以利用ES提供的“nested”数据范例,如下所示:
  1. PUT users
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "name": {
  6.         "type": "keyword"
  7.       },
  8.       "friends": {
  9.         "type": "nested",
  10.         "properties": {
  11.           "name": {
  12.             "type": "keyword"
  13.           },
  14.           "gender": {
  15.             "type": "keyword"
  16.           }
  17.         }
  18.       }
  19.     }
  20.   }
  21. }
复制代码
搜刮方式改为nested,如下所示,将不会再找回任何文档
  1. GET users/_search
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "must": [
  6.         {
  7.           "nested": {
  8.             "path": "friends",
  9.             "query": {
  10.               "bool": {
  11.                 "must": [
  12.                   {
  13.                     "match": {
  14.                       "friends.name": "Jack"
  15.                     }
  16.                   },
  17.                   {
  18.                     "match": {
  19.                       "friends.gender": "女"
  20.                     }
  21.                   }
  22.                 ]
  23.               }
  24.             }
  25.           }
  26.         }
  27.       ]
  28.     }
  29.   }
  30. }
复制代码
nested范例将嵌套对象索引为单独的隐蔽文档,使其可以独立于别的对象而查询每个对象。上述示例中,lisa.friends的存储结构就变成了下面如许,每个friend都单独存储为内部的隐蔽文档。
  1. {
  2.   {
  3.    "friends.name":["Jack"],
  4.    "friends.gender":["男"]
  5.   },
  6.   {
  7.     "friends.name":["Ruth"],
  8.    "friends.gender":["女"]
  9.   }
  10. }
复制代码
3.4 join

join数据范例用来给同一索引中的文档创建父子关系,有点类似于关系数据库中的表毗连。
如下示例,创建一个问答索引用于搜刮题目和答案:
  1. PUT question-answer-index
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "content": {
  6.         "type": "text"
  7.       },
  8.       "join_field": {
  9.         "type": "join",
  10.         "relations": {
  11.           "question": "answer"
  12.         }
  13.       }
  14.     }
  15.   }
  16. }
复制代码
分别索引父文档和子文档,此中索引子文档时路由值routing是必须的,由于ES必须包管父子文档索引在同一个分片里。
  1. // 父文档
  2. POST question-answer-index/_doc/1
  3. {
  4.   "content": "Elasticsearch是什么?",
  5.   "join_field": {
  6.     "name": "question"
  7.   }
  8. }
  9. // 子文档
  10. POST question-answer-index/_doc/2?routing=1
  11. {
  12.   "content": "Elasticsearch是位于Elastic Stack 核心的分布式搜索和分析引擎。",
  13.   "join_field": {
  14.     "name": "answer",
  15.     "parent": "1"
  16.   }
  17. }
复制代码
根据父文档搜刮子文档:
  1. GET question-answer-index/_search
  2. {
  3.   "query": {
  4.     "has_parent": {
  5.       "parent_type": "question",
  6.       "query": {
  7.         "match": {
  8.           "content": "Elasticsearch"
  9.         }
  10.       }
  11.     }
  12.   }
  13. }
  14. {
  15.   "took": 4,
  16.   "timed_out": false,
  17.   "_shards": {
  18.     "total": 1,
  19.     "successful": 1,
  20.     "skipped": 0,
  21.     "failed": 0
  22.   },
  23.   "hits": {
  24.     "total": {
  25.       "value": 1,
  26.       "relation": "eq"
  27.     },
  28.     "max_score": 1,
  29.     "hits": [
  30.       {
  31.         "_index": "question-answer-index",
  32.         "_id": "2",
  33.         "_score": 1,
  34.         "_routing": "1",
  35.         "_source": {
  36.           "content": "Elasticsearch是位于Elastic Stack 核心的分布式搜索和分析引擎。",
  37.           "join_field": {
  38.             "name": "answer",
  39.             "parent": "1"
  40.           }
  41.         }
  42.       }
  43.     ]
  44.   }
  45. }
复制代码
根据子文档搜刮父文档:
  1. GET question-answer-index/_search
  2. {
  3.   "query": {
  4.     "has_child": {
  5.       "type": "answer",
  6.       "query": {
  7.         "match": {
  8.           "content": "Elasticsearch"
  9.         }
  10.       }
  11.     }
  12.   }
  13. }
  14. {
  15.   "took": 5,
  16.   "timed_out": false,
  17.   "_shards": {
  18.     "total": 1,
  19.     "successful": 1,
  20.     "skipped": 0,
  21.     "failed": 0
  22.   },
  23.   "hits": {
  24.     "total": {
  25.       "value": 1,
  26.       "relation": "eq"
  27.     },
  28.     "max_score": 1,
  29.     "hits": [
  30.       {
  31.         "_index": "question-answer-index",
  32.         "_id": "1",
  33.         "_score": 1,
  34.         "_source": {
  35.           "content": "Elasticsearch是什么?",
  36.           "join_field": {
  37.             "name": "question"
  38.           }
  39.         }
  40.       }
  41.     ]
  42.   }
  43. }
复制代码
除了示例中的一对一的关系,join也支持界说一对多的关联关系,join本身支持纵向的层级结构,除了界说父子关系,还可以界说子孙关系,但是都必须包管有关系的文档索引在同一分片里。
join范例的一些限定必要留意:

  • 每个索引,仅答应界说一个join范例的字段
  • 父子文档必须索引在同一个分片里
  • 一个父文档可以有多个子文档,一个子文档只能有一个父文档
4. 结构化数据范例

4.1 Range

范围字段范例表现一个连续的值范围,由上界限和下界限构成。ES现在支持的范围范例有:整型范围、浮点数范围、日期范围和ip范围。
如下示例,我们创建一个meetings索引,此中集会会议的连续时间duration字段就是一个日期范围范例
  1. PUT meetings
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "title":{
  6.         "type": "text"
  7.       },
  8.       "duration":{
  9.         "type": "date_range",
  10.         "format": "yyyy-MM-dd HH:mm:ss"
  11.       }
  12.     }
  13.   }
  14. }
复制代码
接下来索引文档
  1. POST meetings/_doc
  2. {
  3.   "title":"需求评审会",
  4.   "duration":{
  5.     "gte":"2024-01-01 12:00:00",
  6.     "lte":"2024-01-01 12:30:00"
  7.   }
  8. }
复制代码
利用term查询即可,只要查询的值在给定的时间范围内就可以召回文档
  1. GET meetings/_search
  2. {
  3.   "query": {
  4.     "term": {
  5.       "duration": {
  6.         "value": "2024-01-01 12:10:00"
  7.       }
  8.     }
  9.   }
  10. }
  11. {
  12.   "took": 4,
  13.   "timed_out": false,
  14.   "_shards": {
  15.     "total": 1,
  16.     "successful": 1,
  17.     "skipped": 0,
  18.     "failed": 0
  19.   },
  20.   "hits": {
  21.     "total": {
  22.       "value": 1,
  23.       "relation": "eq"
  24.     },
  25.     "max_score": 1,
  26.     "hits": [
  27.       {
  28.         "_index": "meetings",
  29.         "_id": "CuuRoY4BODFb3LbQk3WN",
  30.         "_score": 1,
  31.         "_source": {
  32.           "title": "需求评审会",
  33.           "duration": {
  34.             "gte": "2024-01-01 12:00:00",
  35.             "lte": "2024-01-01 12:30:00"
  36.           }
  37.         }
  38.       }
  39.     ]
  40.   }
  41. }
复制代码
4.2 ip

ip范例字段可以用来存储IPv4和IPv6地点。
如下示例,创建一个computers索引
  1. PUT computers
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "name":{
  6.         "type": "keyword"
  7.       },
  8.       "ip":{
  9.         "type": "ip"
  10.       }
  11.     }
  12.   }
  13. }
复制代码
索引文档
  1. POST computers/_doc
  2. {
  3.   "name":"C01",
  4.   "ip":"192.168.0.1"
  5. }
  6. POST computers/_doc
  7. {
  8.   "name":"C02",
  9.   "ip":"192.168.0.2"
  10. }
复制代码
可以根据ip准确匹配
  1. GET computers/_search
  2. {
  3.   "query": {
  4.     "term": {
  5.       "ip": {
  6.         "value": "192.168.0.1"
  7.       }
  8.     }
  9.   }
  10. }
  11. {
  12.   "took": 0,
  13.   "timed_out": false,
  14.   "_shards": {
  15.     "total": 1,
  16.     "successful": 1,
  17.     "skipped": 0,
  18.     "failed": 0
  19.   },
  20.   "hits": {
  21.     "total": {
  22.       "value": 1,
  23.       "relation": "eq"
  24.     },
  25.     "max_score": 1,
  26.     "hits": [
  27.       {
  28.         "_index": "computers",
  29.         "_id": "DeuloY4BODFb3LbQV3UG",
  30.         "_score": 1,
  31.         "_source": {
  32.           "name": "C01",
  33.           "ip": "192.168.0.1"
  34.         }
  35.       }
  36.     ]
  37.   }
  38. }
复制代码
也可以用CIDR表现法根据前缀查询
  1. GET computers/_search
  2. {
  3.   "query": {
  4.     "term": {
  5.       "ip": {
  6.         "value": "192.168.0.0/24"
  7.       }
  8.     }
  9.   }
  10. }
  11. {
  12.   "took": 0,
  13.   "timed_out": false,
  14.   "_shards": {
  15.     "total": 1,
  16.     "successful": 1,
  17.     "skipped": 0,
  18.     "failed": 0
  19.   },
  20.   "hits": {
  21.     "total": {
  22.       "value": 2,
  23.       "relation": "eq"
  24.     },
  25.     "max_score": 1,
  26.     "hits": [
  27.       {
  28.         "_index": "computers",
  29.         "_id": "DeuloY4BODFb3LbQV3UG",
  30.         "_score": 1,
  31.         "_source": {
  32.           "name": "C01",
  33.           "ip": "192.168.0.1"
  34.         }
  35.       },
  36.       {
  37.         "_index": "computers",
  38.         "_id": "DuuloY4BODFb3LbQW3XF",
  39.         "_score": 1,
  40.         "_source": {
  41.           "name": "C02",
  42.           "ip": "192.168.0.2"
  43.         }
  44.       }
  45.     ]
  46.   }
  47. }
复制代码
5. 文本搜刮范例

5.1 Text

文本范例族,包罗text和match_only_text,颠末分析的非结构化文本数据。
5.1.1 text

索引全文值的字段,比方消息内容、商品先容等,ES在索引之前会先颠末分析器将字符串转换成单个词列表再存储,默认不存储原始全文值,以是无法通过精准匹配来检索text范例,也不可用于排序和聚合,text得当存储非结构化但人类可阅读的文本内容。
如下示例,创建一个news索引,用于索引消息数据
  1. PUT news
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "title":{
  6.         "type": "text"
  7.       },
  8.       "content":{
  9.         "type": "text"
  10.       }
  11.     }
  12.   }
  13. }
复制代码
索引两篇消息文档
  1. POST news/_doc
  2. {
  3.   "title":"美国苹果公司发布新款iPhone",
  4.   "content":"新iPhone发布!美国苹果公司震撼发布最新款iPhone。精致设计,强劲性能,和先进功能的完美结合。期待它带来的创新体验!"
  5. }
  6. POST news/_doc
  7. {
  8.   "title":"今年苹果预计将推迟上市",
  9.   "content":"据报道,今年水果市场上备受期待的苹果预计将推迟上市。这可能是由于天气等因素导致苹果的生长和成熟过程延迟。消费者需稍作等待,相信在不久的将来将能品尝到新鲜甜蜜的苹果。"
  10. }
复制代码
末了通过multi_match来检索文档,检索的字段包罗title和content,此中title具备更高的权重,文档得分会更高
  1. GET news/_search
  2. {
  3.   "query": {
  4.     "multi_match": {
  5.       "query": "苹果",
  6.       "fields": ["title^2","content"]
  7.     }
  8.   }
  9. }
  10. {
  11.   "took": 11,
  12.   "timed_out": false,
  13.   "_shards": {
  14.     "total": 1,
  15.     "successful": 1,
  16.     "skipped": 0,
  17.     "failed": 0
  18.   },
  19.   "hits": {
  20.     "total": {
  21.       "value": 2,
  22.       "relation": "eq"
  23.     },
  24.     "max_score": 0.72928625,
  25.     "hits": [
  26.       {
  27.         "_index": "news",
  28.         "_id": "meQwuI4BPIYet_3flP8X",
  29.         "_score": 0.72928625,
  30.         "_source": {
  31.           "title": "美国苹果公司发布新款iPhone",
  32.           "content": "新iPhone发布!美国苹果公司震撼发布最新款iPhone。精致设计,强劲性能,和先进功能的完美结合。期待它带来的创新体验!"
  33.         }
  34.       },
  35.       {
  36.         "_index": "news",
  37.         "_id": "muQwuI4BPIYet_3fl_9V",
  38.         "_score": 0.72928625,
  39.         "_source": {
  40.           "title": "今年苹果预计将推迟上市",
  41.           "content": "据报道,今年水果市场上备受期待的苹果预计将推迟上市。这可能是由于天气等因素导致苹果的生长和成熟过程延迟。消费者需稍作等待,相信在不久的将来将能品尝到新鲜甜蜜的苹果。"
  42.         }
  43.       }
  44.     ]
  45.   }
  46. }
复制代码
5.1.2 match_only_text

Elasticsearch7.14推出的全新文本范例,它和text的区别是:match_only_text不存储长度归一化因子、词频数据、位置数据,以是match_only_text不支持文档评分,带来的利益就是比text更节省存储空间,非常适适用于存储日志日志。
如下示例,创建logs索引,用于索引日志日志数据,此中text字段范例是match_only_text
  1. PUT logs
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "level":{
  6.         "type": "keyword"
  7.       },
  8.       "text":{
  9.         "type": "match_only_text"
  10.       }
  11.     }
  12.   }
  13. }
复制代码
索引一些日志日志
  1. POST logs/_doc
  2. {
  3.   "level":"info",
  4.   "text":"This is the first log"
  5. }
  6. POST logs/_doc
  7. {
  8.   "level":"info",
  9.   "text":"This is the second log log"
  10. }
复制代码
末了,通过match检索日志,第二条日志由于有两个”log“以是评分理论上会更高,但是match_only_text不纪录词频,以是也就不加入评分,以是两条日志的评分结果都是1
  1. GET logs/_search
  2. {
  3.   "query": {
  4.     "match": {
  5.       "text": "log"
  6.     }
  7.   }
  8. }
  9. {
  10.   "took": 12,
  11.   "timed_out": false,
  12.   "_shards": {
  13.     "total": 1,
  14.     "successful": 1,
  15.     "skipped": 0,
  16.     "failed": 0
  17.   },
  18.   "hits": {
  19.     "total": {
  20.       "value": 2,
  21.       "relation": "eq"
  22.     },
  23.     "max_score": 1,
  24.     "hits": [
  25.       {
  26.         "_index": "logs",
  27.         "_id": "m-Q5uI4BPIYet_3f4P8H",
  28.         "_score": 1,
  29.         "_source": {
  30.           "level": "info",
  31.           "text": "This is the first log"
  32.         }
  33.       },
  34.       {
  35.         "_index": "logs",
  36.         "_id": "nOQ5uI4BPIYet_3f5f8j",
  37.         "_score": 1,
  38.         "_source": {
  39.           "level": "info",
  40.           "text": "This is the second log log"
  41.         }
  42.       }
  43.     ]
  44.   }
  45. }
复制代码
5.2 completion

completion范例紧张用于搜刮建媾和主动补全,假如你要做一个类似百度搜刮的遐想提示功能,那么就可以利用ES的completion范例。completion范例的suggest性能非常高,ES利用了一种特殊的结构用于前缀搜刮,而且数据会缓存在内存中。
如下示例,创建一个webpages索引,用于索引网页,此中suggest字段利用completion范例,数据泉源是title
  1. PUT webpages
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "title": {
  6.         "type": "text",
  7.         "copy_to": "suggest"
  8.       },
  9.       "suggest": {
  10.         "type": "completion"
  11.       },
  12.       "url": {
  13.         "type": "keyword"
  14.       }
  15.     }
  16.   }
  17. }
复制代码
接下来,我们索引几篇文档
  1. POST webpages/_doc
  2. {
  3.   "title":"Java-百度百科",
  4.   "url":"https://baike.baidu.com/item/Java/85979"
  5. }
  6. POST webpages/_doc
  7. {
  8.   "title":"java能做什么",
  9.   "url":"https://baijiahao.baidu.com/s?id=1765494775400937848"
  10. }
  11. POST webpages/_doc
  12. {
  13.   "title":"python编程从入门到精通",
  14.   "url":"https://baijiahao.baidu.com/s?id=1764847625088362688"
  15. }
复制代码
末了,通过suggest检索主动补全的数据
  1. GET webpages/_search
  2. {
  3.   "suggest": {
  4.     "title-suggestion": {
  5.       "text": "java",
  6.       "completion": {
  7.         "field": "suggest"
  8.       }
  9.     }
  10.   }
  11. }
  12. {
  13.   "took": 0,
  14.   "timed_out": false,
  15.   "_shards": {
  16.     "total": 1,
  17.     "successful": 1,
  18.     "skipped": 0,
  19.     "failed": 0
  20.   },
  21.   "hits": {
  22.     "total": {
  23.       "value": 0,
  24.       "relation": "eq"
  25.     },
  26.     "max_score": null,
  27.     "hits": []
  28.   },
  29.   "suggest": {
  30.     "title-suggestion": [
  31.       {
  32.         "text": "java",
  33.         "offset": 0,
  34.         "length": 4,
  35.         "options": [
  36.           {
  37.             "text": "Java-百度百科",
  38.             "_index": "webpages",
  39.             "_id": "neRCuI4BPIYet_3f8v8w",
  40.             "_score": 1,
  41.             "_source": {
  42.               "title": "Java-百度百科",
  43.               "url": "https://baike.baidu.com/item/Java/85979"
  44.             }
  45.           },
  46.           {
  47.             "text": "java能做什么",
  48.             "_index": "webpages",
  49.             "_id": "nuRCuI4BPIYet_3f9f9k",
  50.             "_score": 1,
  51.             "_source": {
  52.               "title": "java能做什么",
  53.               "url": "https://baijiahao.baidu.com/s?id=1765494775400937848"
  54.             }
  55.           }
  56.         ]
  57.       }
  58.     ]
  59.   }
  60. }
复制代码
6. 空间数据范例

ES支持的空间数据范例比力丰富,包罗:geo_point、geo_shape、point、shape。
6.1 geo_point

geo_point范例用来存储地点位置经纬度坐标,用于地点位置的搜刮和聚合分析,比方实现:附近的人、附近的店肆等功能。
如下示例,创建hotels索引,索引旅馆信息,此中location字段利用geo_point范例
  1. PUT hotels
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "name": {
  6.         "type": "keyword"
  7.       },
  8.       "location":{
  9.         "type": "geo_point"
  10.       }
  11.     }
  12.   }
  13. }
复制代码
再索引两个文档
  1. POST hotels/_doc
  2. {
  3.   "name":"如家酒店",
  4.   "location":{
  5.     "lat":10.1,
  6.     "lon":10.1
  7.   }
  8. }
  9. POST hotels/_doc
  10. {
  11.   "name":"亚朵酒店",
  12.   "location":{
  13.     "lat":10.2,
  14.     "lon":10.2
  15.   }
  16. }
复制代码
末了,给定一个圆心坐标查询附近10km的旅馆
  1. GET hotels/_search
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "filter": [
  6.         {
  7.           "geo_distance": {
  8.             "distance": "10km",
  9.             "location": {
  10.               "lat": 10.15,
  11.               "lon": 10.15
  12.             }
  13.           }
  14.         }
  15.       ]
  16.     }
  17.   }
  18. }
  19. {
  20.   "took": 1,
  21.   "timed_out": false,
  22.   "_shards": {
  23.     "total": 1,
  24.     "successful": 1,
  25.     "skipped": 0,
  26.     "failed": 0
  27.   },
  28.   "hits": {
  29.     "total": {
  30.       "value": 2,
  31.       "relation": "eq"
  32.     },
  33.     "max_score": 0,
  34.     "hits": [
  35.       {
  36.         "_index": "hotels",
  37.         "_id": "puRxuI4BPIYet_3fT__u",
  38.         "_score": 0,
  39.         "_source": {
  40.           "name": "如家酒店",
  41.           "location": {
  42.             "lat": 10.1,
  43.             "lon": 10.1
  44.           }
  45.         }
  46.       },
  47.       {
  48.         "_index": "hotels",
  49.         "_id": "p-RxuI4BPIYet_3fUv_f",
  50.         "_score": 0,
  51.         "_source": {
  52.           "name": "亚朵酒店",
  53.           "location": {
  54.             "lat": 10.2,
  55.             "lon": 10.2
  56.           }
  57.         }
  58.       }
  59.     ]
  60.   }
  61. }
复制代码
还可以利用geo_bounding_box矩形搜刮,给定矩形的左上方和右下方坐标即可
  1. GET hotels/_search
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "filter": [
  6.         {
  7.           "geo_bounding_box": {
  8.             "location": {
  9.               "top_left": {
  10.                 "lat": 10.175,
  11.                 "lon": 10
  12.               },
  13.               "bottom_right": {
  14.                 "lat": 10,
  15.                 "lon": 10.175
  16.               }
  17.             }
  18.           }
  19.         }
  20.       ]
  21.     }
  22.   }
  23. }
  24. {
  25.   "took": 1,
  26.   "timed_out": false,
  27.   "_shards": {
  28.     "total": 1,
  29.     "successful": 1,
  30.     "skipped": 0,
  31.     "failed": 0
  32.   },
  33.   "hits": {
  34.     "total": {
  35.       "value": 1,
  36.       "relation": "eq"
  37.     },
  38.     "max_score": 0,
  39.     "hits": [
  40.       {
  41.         "_index": "hotels",
  42.         "_id": "puRxuI4BPIYet_3fT__u",
  43.         "_score": 0,
  44.         "_source": {
  45.           "name": "如家酒店",
  46.           "location": {
  47.             "lat": 10.1,
  48.             "lon": 10.1
  49.           }
  50.         }
  51.       }
  52.     ]
  53.   }
  54. }
复制代码
6.2 geo_shape

geo_point用来在空间里界说一个点,geo_shape则可以用来界说一个外形,支持:点、线、圆、矩形、多边形。比方可以用geo_shape来表现一个景点的地域、一个停车场的范围等。
如下示例,创建scenic-spots索引用于索引景点信息,此中location_shape字段利用geo_shape范例
  1. PUT scenic-spots
  2. {
  3.   "mappings": {
  4.     "properties": {
  5.       "name": {
  6.         "type": "keyword"
  7.       },
  8.       "location_shape":{
  9.         "type": "geo_shape"
  10.       }
  11.     }
  12.   }
  13. }
复制代码
我们利用polygon多边形来索引杭州西湖和西溪湿地两个景点
  1. POST scenic-spots/_doc
  2. {
  3.   "name":"杭州西湖",
  4.   "location_shape":{
  5.     "type":"polygon",
  6.     "coordinates":[[
  7.       [120.13,30.25],
  8.       [120.16,30.26],
  9.       [120.16,30.25],
  10.       [120.15,30.23],
  11.       [120.14,30.23],
  12.       [120.13,30.25]
  13.     ]]
  14.   }
  15. }
  16. POST scenic-spots/_doc
  17. {
  18.   "name":"杭州西溪湿地",
  19.   "location_shape":{
  20.     "type":"polygon",
  21.     "coordinates":[[
  22.       [120.05,30.28],
  23.       [120.09,30.28],
  24.       [120.09,30.26],
  25.       [120.04,30.25],
  26.       [120.05,30.28]
  27.     ]]
  28.   }
  29. }
复制代码
末了通过外形搜刮,我们利用within即搜刮外形必须包罗在索引外形内,我们搜刮西湖内的三潭印月地域,结果返回西湖
  1. GET scenic-spots/_search
  2. {
  3.   "query": {
  4.     "bool": {
  5.       "filter": [
  6.         {
  7.           "geo_shape": {
  8.             "location_shape": {
  9.               "shape": {
  10.                 "type": "polygon",
  11.                 "relation": "within",
  12.                 "coordinates": [[
  13.                   [120.14,30.24],
  14.                   [120.145,30.245],
  15.                   [120.135,30.235],
  16.                   [120.14,30.24]
  17.                 ]]
  18.               }
  19.             }
  20.           }
  21.         }
  22.       ]
  23.     }
  24.   }
  25. }
  26. {
  27.   "took": 6,
  28.   "timed_out": false,
  29.   "_shards": {
  30.     "total": 1,
  31.     "successful": 1,
  32.     "skipped": 0,
  33.     "failed": 0
  34.   },
  35.   "hits": {
  36.     "total": {
  37.       "value": 1,
  38.       "relation": "eq"
  39.     },
  40.     "max_score": 0,
  41.     "hits": [
  42.       {
  43.         "_index": "scenic-spots",
  44.         "_id": "xeQ7vI4BPIYet_3fHP81",
  45.         "_score": 0,
  46.         "_source": {
  47.           "name": "杭州西湖",
  48.           "location_shape": {
  49.             "type": "polygon",
  50.             "coordinates": [
  51.               [
  52.                 [
  53.                   120.13,
  54.                   30.25
  55.                 ],
  56.                 [
  57.                   120.16,
  58.                   30.26
  59.                 ],
  60.                 [
  61.                   120.16,
  62.                   30.25
  63.                 ],
  64.                 [
  65.                   120.15,
  66.                   30.23
  67.                 ],
  68.                 [
  69.                   120.14,
  70.                   30.23
  71.                 ],
  72.                 [
  73.                   120.13,
  74.                   30.25
  75.                 ]
  76.               ]
  77.             ]
  78.           }
  79.         }
  80.       }
  81.     ]
  82.   }
  83. }
复制代码
除了within,还支持disjoint 搜刮外形和索引外形不重叠、intersects 搜刮外形和索引外形有重叠部门。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表