可以访问:http://qq2250125690.gitee.io/es-learn-note/
或新建html文件(Unicode编码)后查看,html代码如下:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
pre{
font-family: 微软雅黑;
}
div>h2{
margin-bottom: 0;
padding-bottom: 0;
}
h2:hover{
cursor: pointer;
}
</style>
</head>
<body>
<div>
<span style="position: fixed; right: 2%;cursor: pointer;"><span id="closeAllBtn">全部折叠</span> / <span id="openAllBtn">全部展开</span></span>
<h2>使用ik分词器</h2><pre style="display: '';"> //ik_smart最粗粒度,不会有重复
GET _analyze
{
"analyzer": "ik_smart",
"text": "中国人民共和国"
}
//ik_max_word最细粒度的拆分,会有重复
GET _analyze
{
"analyzer": "ik_max_word",
"text": "中国人民共和国"
}
</pre>
<h2>查看es集群状态</h2><pre style="display: '';">GET _cat/health?v
</pre>
<h2>创建索引</h2><pre style="display: '';"> //创建索引: PUT + 索引名
PUT index1
//创建索引时如果索引已存在会报错提示: 索引已存在
PUT index1
//不能用POST创建索引
POST index2
//创建索引并制定字段类型mapping
PUT user
{
"mappings": {
"properties": {
"name": {
"type": "text",
"index": true
},
"password": {
"type": "keyword",
"index": false
}
}
}
}
//查看索引的字段类型mapping
GET user/_mapping
</pre>
<h2>查看索引信息</h2><pre style="display: '';"> //查看索引信息
GET index1
//查看所有索引详细信息
GET _cat/indices?v
</pre>
<h2>删除索引</h2><pre style="display: '';"> //删除单个索引
DELETE index1
//删除全部索引
DELETE *
</pre>
<h2>添加数据(文档)</h2><pre style="display: '';"> //添加数据(文档)时不指定id,id随机生成,由于id是随机生成的,不符合幂等性,因此采用POST请求
POST index1/_doc
{
"name": "华为手机2",
"type": "华为",
"price": 5999
}
//添加数据(文档)时指定id,由于id是指定的,符合幂等性,因此采用PUT请求
PUT index1/_doc/1
{
"name": "指定了id",
"age": 18
}
//使用_create添加数据
PUT user/_create/1
{
"name": "name1",
"password": "12"
}
</pre>
<h2>查询数据(文档)</h2><pre style="display: '';"> //根据id查询
GET index1/_doc/1
//查询一个索引的全部数据
GET index1/_search
//条件查询
//简单条件查询
GET index1/_search?q=type:"华为"
GET index1/_search
{
"query": {
"match": {
"type": "小米"
}
}
}
//条件查询之精确查询(使用match_phrase则不会查询关键词分词,这意味着搜索"小华"不会拆分成"小"和"华"然后去库中检索,使用.keyword则不对数据进行分词,这意味着想搜索type="米"的数据,不会匹配到type="小米"的数据)
GET index1/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"type": "米"
}
}
]
}
}
}
//条件查询条件与(使用must)
GET index1/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"type": "小米"
}
},
{
"match": {
"name": "1"
}
}
]
}
}
}
//条件查询之条件或(使用should)
GET index1/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"type": "小米"
}
},
{
"match": {
"type": "华为"
}
}
]
}
}
}
//条件查询之范围查询
GET index1/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 28
}
}
}
}
}
}
//分页查询
GET index1/_search
{
"from": 0,
"size": 1
}
//查询部分字段
GET index1/_search
{
"_source": ["name", "type"]
}
//查询排序
GET index1/_search
{
"sort": [
{
"name.keyword": {
"order": "desc"
}
}
]
}
GET index2/_search
{
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
//查询高亮
//对每个字进行高亮
GET index1/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"type": "小米"
}
}
]
}
},
"highlight": {
"fields": {
"type": {}
}
}
}
//对整个词语进行高亮
GET index1/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"type.keyword": "小米"
}
}
]
}
},
"highlight": {
"fields": {
"type.keyword": {}
}
}
}
//自定义高亮标签
//对整个词语进行高亮
GET index1/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"type.keyword": "小米"
}
}
]
}
},
"highlight": {
"fields": {
"type.keyword": {}
},
"pre_tags": "<span class='red'>",
"post_tags": "</span>"
}
}
//聚合
//统计数量
GET index1/_search
{
"aggs": {
"name_group": {
"terms": {
"field": "name.keyword"
}
}
}
}
//求均值
GET index1/_search
{
"aggs": {
"price_avg": {
"avg": {
"field": "price"
}
}
}
}
</pre>
<h2>修改数据(文档)</h2><pre style="display: '';"> //完全覆盖修改,符合幂等性(_version每次都会增加1),采用PUT
PUT index1/_doc/3
{
"name": "华为手机1",
"type": "华为"
}
//局部修改,只有被修改的字段会发生改变,其他字段不会被改变,不符合幂等性(_version会根据情况选择是否加1,如果新的值不等于原来的值,则加1,否则_version不变)
POST index1/_update/1
{
"doc": {
"name": "局部修改"
}
}
</pre>
<h2>删除数据(文档)</h2><pre style="display: '';">DELETE index1/_doc/1</pre>
</pre>
<script>
let h2s = document.getElementsByTagName("h2");
let pres = document.getElementsByTagName("pre");
let pre = pres[0];
for(let h2 of h2s){
h2.addEventListener("click", e => {
let clickNode = e.target;
let nextNode = clickNode.nextElementSibling;
nextNode.style.display = nextNode.style.display === "none" ? "" : "none";
})
}
let closeAllBtn = document.getElementById("closeAllBtn");
closeAllBtn.addEventListener("click", e => {
for(let pre of pres) {
pre.style.display = "none";
}
})
let openAllBtn = document.getElementById("openAllBtn");
openAllBtn.addEventListener("click", e => {
for(let pre of pres) {
pre.style.display = "";
}
})
</script>
</body>
</html>