前言:安装注意点-es与kibana版本要一致

  • 推荐使用docker安装,具体如何使用docker以及docker的容器安装请移步es安装以及配置.,后面我就说安装后es的DSL操作

1.索引(Index)的基本操作

  • 1.1 进入可视化kibana界面后,在Dev tools界面操作DSL语句:
    在这里插入图片描述
  • 1.2 DSL索引操作:
GET /_cat/indices?v 		查看索引信息
PUT /dangdang/       	  	创建索引
DELETE /dangdang			删除索引
DELETE /*					删除所有索引

2.类型(type)的基本操作

  • 索引Mapping Type有:text , keyword , date ,integer, long , double , boolean or ip
  • 1.1 索引不存在时
//创建/dangdang索引并创建(product)类型
PUT /dangdang             
{
  "mappings": {
    "product": {
      "properties": {
        	"title":    { "type": "text"  },
        	"name":     { "type": "text"  },
       		"age":      { "type": "integer" },
        	"created":  {
         		 "type":   "date",
          		 "format": "strict_date_optional_time||epoch_millis"
        		}
      		}
    	}
  	}
}
  • 1.2 索引已经存在时
POST test_index/test_type/_mapping
{
 
   "test_type": {
     "dynamic": false,
     "_all": {
       "enabled": false
     },
     "properties": {
       "wbbh": {
         "type": "keyword"
       },
       "jyxkzbh": {
         "type": "keyword"
       },
       "wbmc": {
         "type": "text",
         "analyzer": "smartcn",
         "fields": {
           "raw": {
             "type": "keyword"
           },
           "standard": {
             "type": "text",
             "analyzer": "standard"
           }
         }
       },
       "zbx": {
         "type": "keyword"
       },
       "zby": {
         "type": "keyword"
       },
       "zby_zbx": {
         "type": "keyword"
       },
       "lksj": {
         "type": "keyword"
       },
       "wbdz": {
         "type": "text",
         "analyzer": "smartcn",
         "fields": {
           "raw": {
             "type": "keyword"
           },
           "standard": {
             "type": "text",
             "analyzer": "standard"
           }
         }
       },
       "cjsj": {
         "type": "date"
       },
       "rksj": {
         "type": "date"
       },
       "gxdwmc": {
         "type": "text",
         "analyzer": "smartcn",
         "fields": {
           "raw": {
             "type": "keyword"
           },
           "standard": {
             "type": "text",
             "analyzer": "standard"
           }
         }
       },
       "wbfzr": {
         "type": "text",
         "analyzer": "smartcn",
         "fields": {
           "raw": {
             "type": "keyword"
           },
           "standard": {
             "type": "text",
             "analyzer": "standard"
           }
         }
       },
       "dt": {
         "type": "keyword"
       },
       "type": {
         "type": "keyword"
       }
     }
   }
}

3.文档(document)的基本操作

3.1添加文档
PUT /ems/emp/1   #/索引/类型/id
{
  "name":"赵小六",
  "age":23,
  "bir":"2012-12-12",
  "content":"这是一个好一点的员工"
}
3.2查询文档
GET /ems/emp/1  
返回结果:
{
  "_index": "ems",
  "_type": "emp",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "赵小六",
    "age": 23,
    "bir": "2012-12-12",
    "content": "这是一个好一点的员工"
  }
}
3.3删除文档
DELETE /ems/emp/1
{
  "_index": "ems",
  "_type": "emp",
  "_id": "1",
  "_version": 2,
  "result": "deleted", #删除成功
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}
3.4更新文档

ps:PUT也能更新文档,但是会把之前的记录覆盖掉,有的属性也会丢掉,所以使用POST会更好使用

1.第一种方式  更新原有的数据
    POST /dangdang/emp/1/_update
    {
      "doc":{
        "name":"xiaohei"
      }
    }
2.第二种方式  添加新的数据
    POST /ems/emp/1/_update
    {
      "doc":{
        "name":"xiaohei",
        "age":11,
        "dpet":"你好部门"
      }
    }
3.第三种方式 在原来数据基础上更新
	POST /ems/emp/1/_update
    {
      "script": "ctx._source.age += 5"
    }
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐