使用kibana实现elasticsearch的增删改查

0.添加索引|mapping

# 创建索引
PUT lagou
 {
   "settings":{
     "index":{
       "number_of_shards":5,
       "number_of_replicas":1
     }
   }

}
#number_of_shards 分片数量
#number_of_replicas 副本数量

#获取lagou的设置
 GET lagou/_settings
#获取所有的设置
 GET _all/_settings
#获取所有的设置(同上)
 GET _settings
#获取kibana和lagou的设置
 GET .kibana,lagou/_settings

在设置中分片的数量不能修改但是副本的数量可以修改

#number_of_shards 分片数量
#number_of_replicas 副本数量
#修改副本数量设置

PUT lagou/_settings
{
  "number_of_replicas":2
}

#获取索引信息
GET _all 
GET lagou

#获取库中所有的索引
GET _cat/_indices  # 无标题栏
GET _cat/indices?pretty&v  # 含标题栏

# 添加别名
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "test_index",
        "alias": "test_index_name"
      }
    }
  ]
}

maping

# 创建mapping
{
"test_type": { # 任何自命名的type类型,可以是_doc,也可是what for ever
  "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"
    }
  }
}
}


# e.g. 创建实例
PUT people
{
  "settings": { # 设置分片
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": { # 设置字段类型
    "_doc": { # _doc的type
    "properties": {
      "type": {"type": "keyword"},
      "name": {"type": "text"},
      "country": {"type": "keyword"},
      "age": {"type": "integer"},
      "date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
      }
    }}
  }
}

1.查询数据

1.1 根据文档id查询数据

#获取某个索引中某个类型某个id的数据
GET lagou/_doc/1
#或者可以这样
GET lagou/_doc/1?_source

1.2 根据query语句查询语句

此种查询方式灵活,通常开发时常用

GET lagou/_search
{
  "size": xxx, # 返回数据量
  "sort": {"字段": {"order": "asc|desc"}},
  "_source": ["字段1", "字段2", ...],
  "query": {
    "bool": {
      "filter": {"field":"value"} ,
      "must|must_not|should" : []
    }
  }
}

2.插入数据-create|insert

#接下来是保存一篇文档到索引当中去  (相当于插入一条记录到一个数据库表当中)
PUT lagou/_doc/1
{
  "title":"python分布式爬虫开发",
  "salary_min":15000,
  "city":"北京",
  "company":{
    "name":"百度",
    "company_addr":"北京市软件园"
  },
  "publish_date":"2017-4-16",
  "comments":15
}


#保存文档到索引当中,不给id
POST lagou/_doc/
{
  "title":"java架构师",
  "salary_min":30000,
  "city":"上海",
  "company":{
  "name":"美团",
  "company_addr":"北京市软件园"
  },
  "publish_date":"2017-4-16",
  "comments":20
}

3.更新修改-update

3.1 覆盖形式更新

#修改文档  (这种采用的是覆盖的方式)   相当于sql中的 update job set title=?,salary_min=?city=? (....省略...) where id = 1
PUT lagou/_doc/1
{
  "title":"linux运维工程师",
  "salary_min":10000,
  "city":"广州",
  "company":{
    "name":"腾讯",
    "company_addr":"广州软件园"
   },
  "publish_date":"2017-4-12",
  "comments":21
}

3.2 不覆盖文档更新

# update job set comments = 20 where id = 1 
POST lagou/_doc/1/_update
{
  "doc":{
    "comments":20
  }
}

# 或者,update形式更新,不覆盖原纪录
POST lagou/_doc/1
{
  字段名:字段值
  ...
}

3.3 通过查询更新-不覆盖

POST business/_update_by_query
{
  "query": {
    "term": {
      "operator_id.keyword": "operator-0" 
    }
  },
  "script": {
    "lang": "painless",
    "source": "ctx._source.operator_name=params.operator_name",
    "params": {
      "operator_name": "name_0"
    }
  }
}

4.删除-delete

  • 删除一条记录,
  • 删除一个type(表)
  • 删除整个 index (库)

4.1 删除一条记录

DELETE lagou/_doc/1

4.2 删除整个 index (库)-慎重

DELETE lagou

4.3 根据查询语句删除

DELETE /索引名/需要清空的type/_query # e.g. DELETE lagou/_doc/_query
{
  "query": {
  "match_all": {}
  }
}

4.4 通过post 查询删除

POST index/_delete_by_query
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "name.keyword": "add new function"
        }}
        ]
    }
  }
}
原文地址:https://www.cnblogs.com/davis12/p/14977858.html