在Kibana中的常见命令

索引库

创建索引库

最简单的创建一个索引库,所有的配置都走默认

PUT /my_index

效果

 

查询索引库

GET /my_index

效果:

 

其中,

  • mappings 是类型映射,目前我们没有给索引库设置映射
  • settings是索引库配置,目前是默认配置

 

删除索引库

DELETE /my_index

效果:

 

类型映射

 我们可以给一个已经存在的索引库添加映射关系,也可以创建索引库的同时直接指定映射关系

索引库已经存在时添加类型映射

PUT /my_index/_mapping
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "ik_smart"
    },
    "images": {
      "type": "keyword",
      "index": false
    },
    "price": {
      "type": "float"
    }
  }
}

效果:

 

 

索引库不存在时添加类型映射

PUT /my_index
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_smart"
      },
      "images": {
        "type": "keyword",
        "index": false
      },
      "price": {
        "type": "float"
      }
    }
  }
}

效果:

 

 查看映射关系

GET /my_index/_mapping

效果:

 

文档的操作

 新增文档随机生成id

POST /my_index/_doc
{
  "title": "小米手机",
  "images": "http://image.leyou.com/12479122.jpg",
  "price": 2699.00
}

效果:

 

 

新增文档并指定id

 

POST /my_index/_doc/2
{
  "title": "华为手机",
  "images": "http://image.leyou.com/12479123.jpg",
  "price": 2899.00
}

效果:

 

查看文档

根据id查看指定文档

GET /my_index/_doc/2

效果:

 

 

 

 修改文档

修改是POST,修改是PUT,这里需要注意

  • id对应文档存在,则修改
  • id对应文档不存在,则新增
PUT /my_index/_doc/2
{
  "title": "苹果手机",
  "images": "http://image.leyou.com/12479123.jpg",
  "price": 2999.00
}

效果:

 

删除文档

DELETE /my_index/_doc/2

效果:

 

 

 

 默认映射

以上我们在新增数据时,添加的字段都是提前在类型中通过mapping定义过的,如果我们添加的字段并没有提前定义过,也是可以成功的。事实上ElasticSearch有一套默认映射规则,如果新增的字段从未定义过,那么就会按照默认映射规则来存储

POST /my_index/_doc/3
{
  "title": "三星手机",
  "images": "http://image.leyou.com/12479123.jpg",
  "price": 2999.00,
  "stock": 200,
  "saleable": true,
  "subTitle": "超级双摄,亿级像素"
}

然后,再看下索引库的映射关系

GET /my_index/_mapping

效果:

 

posted @ 2022-11-03 20:50  徐林俊  阅读(650)  评论(0编辑  收藏  举报