1. 使用kibana创建索引
之前看到过很多次mapping关键词,但是一直都没有太在意过,看了一些文档,mapping主要是为了定义索引中的一些属性,比如一个字段是什么类型的,以及一个字段是否能被索引等等
官网介绍:https://www.elastic.co/guide/cn/elasticsearch/guide/current/_creating_an_index.html
# Click the Variables button, above, to create your own variables.
PUT /cyx_first_index
{
"settings": { #设置相关
"number_of_shards": 3, #分片数量
"number_of_replicas": 2, #副本数量
"analysis": { #解析器
"analyzer": {
"es_std": {
"type": "standard" #标准版
}
}
}
},
"mappings": {
"properties": { #摸索了好久,最后还是问了chatgpt才搞定,chatgpt yyds
"name":{"type": "text"}, # 7.0后string被废弃了,使用text 或者 keyword(不可分词)
"age":{"type": "integer"},
"createTime":{"type": "long"}
}
}
}
获取mapping
语法是get /index/_mapping
GET /cyx_first_index/_mapping
结果
{
"cyx_first_index": {
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"createTime": {
"type": "long"
},
"name": {
"type": "text"
}
}
}
}
}
2. 插入文档
POST /cyx_first_index/_doc
{
"name":"chen",
"age":27,
"createTime":801848820000
}
查找
# Click the Variables button, above, to create your own variables.
POST /cyx_first_index/_search
{
"query": {
"match": {
"name": "chen"
}
}
}
response
{
"took": 538,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "cyx_first_index",
"_id": "zcm_iocBg5Ow_pYU0yPl",
"_score": 0.2876821,
"_source": {
"name": "chen",
"age": 27,
"createTime": 801848820000
}
}
]
}
}