ElasticSearch+Kibana(7.13.1) 简单操作


#创建简单索引,number_of_shards分片数量,number_of_replicas副本数量
PUT /users
{
   "settings" : {
      "number_of_shards" : 3,
      "number_of_replicas" : 1
   }
}

#查看setting
GET users/_settings

#查看mapping
GET users/_mapping


#创建索引并添加数据
#也用来更新整个文档,若已有id为1的数据,es会将原文档标记为已删除状态,并新增id为1的新文档,
#原文档并不会立即消失,但是却无法访问,在后续检索操作过程中,ES会慢慢处理这些已删除数据
PUT starts/_doc/1
{
    "first_name" : "xiao",
    "last_name" :  "zhan",
    "age" :        31,
    "about" :      "A handsome boy.",
    "interests": [ "actors", "singer" ]
}

PUT starts/_doc/2
{
    "first_name" : "zhang",
    "last_name" :  "zhehan",
    "age" :        31,
    "about" :      "Another handsome boy.",
    "interests": [ "actors", "singer" ]
}

#更新部分文档
#es会先根据id为1的文档构建新JSON,然后更新整个JSON,之后会对原文档进行标记删除,并且构建新文档
POST starts/_update/1
{
  "doc":{
    "age" : 32
    
  }
}

#更新部分文档,使用脚本
POST starts/_update/1
{
  "script":"ctx._source.age=ctx._source.age+1"
}


#创建新文档,不指定ID
POST starts/_doc
{
    "first_name" : "gong",
    "last_name" :  "jun",
    "age" :        30,
    "about" :      "A very good friend of Zhang Zhehan.",
    "interests": [ "actors", "gamer" ]
}

#创建新文档,指定ID
POST starts/_doc/6
{
    "first_name" : "gong",
    "last_name" :  "jun",
    "age" :        30,
    "about" :      "A very good friend of Zhang Zhehan.",
    "interests": [ "actors", "gamer" ]
}

#删除文档
DELETE starts/_doc/6

#查看索引文档的内容
GET starts/_search
{
    "query": {
        "match_all": {}
    }
}

#查看索引文档的数量
GET starts/_count
{
    "query": {
        "match_all": {}
    }
}

#获取某一个文档信息
GET starts/_doc/1

#获取某一个文档的_source字段
GET starts/_source/1


#获取first_name为zhang的文档信息
GET starts/_search?q=first_name:zhang


#使用查询表达式获取first_name为xiao的文档信息
GET starts/_search
{
    "query" : {
        "match" : {
            "first_name" : "xiao"
        }
    }
}

#增加first_name为zhang的文档
PUT starts/_doc/3
{
    "first_name" : "zhang",
    "last_name" :  "mingyu",
    "age" :        25,
    "about" :      "My boyfriend.",
    "interests": [ "programmer", "gamer" ]
}

PUT starts/_doc/4
{
    "first_name" : "zhang",
    "last_name" :  "dejun",
    "age" :        8,
    "about" :      "The son of gongjun and zhangzhehan.",
    "interests": [ "programmer", "gamer" ]
}

#含过滤器的查询,年龄大于30的first_name为zhang的文档信息
GET starts/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "first_name" : "zhang" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}

#全文检索,分词检索,结果按相关度自动降序排列
GET starts/_search
{
    "query" : {
        "match" : {
            "about" : "a handsome boy"
        }
    }
}


#短语检索
GET starts/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "handsome boy"
        }
    }
}

#短语检索,对比全文检索
GET starts/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "a handsome boy"
        }
    }
}

#高亮检索
GET starts/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "handsome"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}

#高亮检索,自定义高亮标签
GET starts/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "handsome"
        }
    },
    "highlight": {
        "pre_tags" : ["<tag1>"],
        "post_tags" : ["tag1>"],
        "fields" : {
            "about" : {}
        }
    }
}

#聚合,被聚合的字段
GET starts/_search
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests.keyword" }
    }
  }
}

#聚合+全文检索
GET starts/_search
{
  "query": {
    "match": {
      "first_name": "zhang"
    }
  },
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests.keyword"
      }
    }
  }
}

#聚合+分级汇总
GET starts/_search
{
    "query": {
      "match": {
        "first_name": "zhang"
      }
    },
    "aggs" : {
        "all_interests" : {
            "terms" : { "field" : "interests.keyword" },
            "aggs" : {
                "avg_age" : {
                    "avg" : { "field" : "age" }
                }
            }
        }
    }
}