前面博客中创建的索引以及插入数据,都是由Elasticsearch进行自动判断类型,有些时候是需要进行明确字段类型的,否则,自动判断的类型和实际需求有时候是不相符的。
自动判断的规则如下:
JSON type | Field type |
Boolean: true or false | "boolean" |
Whole number: 123 | "long" |
Floating point: 123.45 | "double" |
String, valid date: "2014-09-15" | "date" |
String: "foo bar" | "string" |
Elasticsearch中支持的类型如下:
类型 | 表示的数据类型 |
String | string , text , keyword |
Whole number | byte , short , integer , long |
Floating point | float , double |
Boolean | boolean |
Date | date |
注意:
- PUT /study
- # 请求数据
- {
- "settings": {
- "index": {
- "number_of_shards": "2",
- "number_of_replicas": "0"
- }
- },
- "mappings": {
- "properties": {
- "name": {
- "type": "text"
- },
- "age": {
- "type": "integer"
- },
- "mail": {
- "type": "keyword"
- },
- "hobby": {
- "type": "text"
- }
- }
- }
- }
-
- # 响应数据
- {
- "acknowledged": true,
- "shards_acknowledged": true,
- "index": "study"
- }
这儿可能存在的错误,参考博文:ElasticSearch 7.x 报错:Root mapping definition has unsupported parameters_Sunny Feng的博客-CSDN博客
- GET /study/_mapping
-
- # 响应数据
- {
- "study": {
- "mappings": {
- "properties": {
- "age": {
- "type": "integer"
- },
- "hobby": {
- "type": "text"
- },
- "mail": {
- "type": "keyword"
- },
- "name": {
- "type": "text"
- }
- }
- }
- }
- }
- POST /study/_bulk
- # 请求数据
- {"index":{"_index":"study"}}
- {"name":"张三","age": 20,"mail": "111@qq.com","hobby":"羽毛球、乒乓球、足球"}
- {"index":{"_index":"study"}}
- {"name":"李四","age": 21,"mail": "222@qq.com","hobby":"羽毛球、乒乓球、足球、篮球"}
- {"index":{"_index":"study"}}
- {"name":"王五","age": 22,"mail": "333@qq.com","hobby":"羽毛球、篮球、游泳、听音乐"}
- {"index":{"_index":"study"}}
- {"name":"赵六","age": 23,"mail": "444@qq.com","hobby":"跑步、游泳"}
- {"index":{"_index":"study"}}
- {"name":"孙七","age": 24,"mail": "555@qq.com","hobby":"听音乐、看电影"}
-
-
- # 响应结果
- {
- "took": 25,
- "errors": false,
- "items": [
- {
- "index": {
- "_index": "study",
- "_type": "_doc",
- "_id": "SHjOVIIBpyNh4YQ4CVSN",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 1,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 0,
- "_primary_term": 1,
- "status": 201
- }
- },
- {
- "index": {
- "_index": "study",
- "_type": "_doc",
- "_id": "SXjOVIIBpyNh4YQ4CVSN",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 1,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 0,
- "_primary_term": 1,
- "status": 201
- }
- },
- {
- "index": {
- "_index": "study",
- "_type": "_doc",
- "_id": "SnjOVIIBpyNh4YQ4CVSN",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 1,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 1,
- "_primary_term": 1,
- "status": 201
- }
- },
- {
- "index": {
- "_index": "study",
- "_type": "_doc",
- "_id": "S3jOVIIBpyNh4YQ4CVSN",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 1,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 1,
- "_primary_term": 1,
- "status": 201
- }
- },
- {
- "index": {
- "_index": "study",
- "_type": "_doc",
- "_id": "THjOVIIBpyNh4YQ4CVSN",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 1,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 2,
- "_primary_term": 1,
- "status": 201
- }
- }
- ]
- }
注意:通过上面的数据库中数据可以看到,默认生成的是_doc类型的数据。
- POST /study/_search
-
- # 请求数据
- {
- "query": {
- "match": {
- "hobby": "音乐"
- }
- }
- }
-
- # 响应数据
- {
- "took": 2,
- "timed_out": false,
- "_shards": {
- "total": 2,
- "successful": 2,
- "skipped": 0,
- "failed": 0
- },
- "hits": {
- "total": {
- "value": 2,
- "relation": "eq"
- },
- "max_score": 1.0470967,
- "hits": [
- {
- "_index": "study",
- "_type": "_doc",
- "_id": "THjOVIIBpyNh4YQ4CVSN",
- "_score": 1.0470967,
- "_source": {
- "name": "孙七",
- "age": 24,
- "mail": "555@qq.com",
- "hobby": "听音乐、看电影"
- }
- },
- {
- "_index": "study",
- "_type": "_doc",
- "_id": "SnjOVIIBpyNh4YQ4CVSN",
- "_score": 0.8527901,
- "_source": {
- "name": "王五",
- "age": 22,
- "mail": "333@qq.com",
- "hobby": "羽毛球、篮球、游泳、听音乐"
- }
- }
- ]
- }
- }