给es设置连接密码
1、修改es根目录下的config/elasticsearch.yml
配置文库,写入下面配置。
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
2.启动es(切换es用户)
3.设置密码,进入bin目录下执行命令。
./elasticsearch-setup-passwords interactive
指令交互过程中需要这只很多个密码
测试访问
默认账号:elastic
浏览器访问:http://localhost:9200/
,此时浏览器弹出需要密码了。
访问控制台:http://127.0.0.1:9100/?auth_user=elastic&auth_password=密码
es基本操作命令
es 没有表的概念,每一个索引就是一个库,数据都放在库下面
创建索引
类似mysql创建表
# 创建名称为shapping的索引
PUT http://127.0.0.1:9200/shapping
返回结果:
{
"acknowledged": true, //创建成功
"shards_acknowledged": true, //示索引创建、更新或删除的操作是否已被所有相关分片确认。
"index": "shapping" //索引名为shapping
}
索引具有幂等性,重复创建会报错
查看shapping索引(查看某个索引)
get http://127.0.0.1:9200/shapping
{
"shapping": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "shapping",
"creation_date": "1692721844404",
"number_of_replicas": "1",
"uuid": "VeVq2ZAWQAyJtAcjmW6z8Q",
"version": {
"created": "8020199"
}
}
}
}
}
查看全部索引
get http://127.0.0.1:9200/_cat/indices?v
v : 显示详细信息
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open shapping VeVq2ZAWQAyJtAcjmW6z8Q 1 1 0 0 225b 225b
删除索引
delete http://127.0.0.1:9200/shapping
{
"acknowledged": true
}
创建文档(添加数据
请求方式 :post
路径 : http://127.0.0.1:9200/shapping/_doc
http://127.0.0.1:9200/shapping/_doc/id #自己指定ID
//也可以吧_doc换成_create,这样看起来更加规范 http://127.0.0.1:9200/shapping/_create/ID
//这样请求会自动创建ID,如:"_id": "4iMnHooBhKSDaY6labt-",
//自定义创建ID方式在_doc后面加上ID : http://127.0.0.1:9200/shapping/_doc/1001, 1001 是这个数据的ID
请求体:
{
"title" : "小米手机",
"img" : "http://www.test.com/1.jpg",
"price" : "3999"
}
返回结果:
{
"_index": "shapping",
"_id": "4iMnHooBhKSDaY6labt-",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
根据ID查询索引下数据
get http://127.0.0.1:9200/shapping/_doc/1000
返回:
{
"_index": "shapping",
"_id": "1000",
"_version": 1,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"title": "小米手机",
"img": "http://www.test.com/1.jpg",
"price": "3999"
}
}
查询索引下的所有数据
get http://127.0.0.1:9200/shapping/_search
{
"took": 328,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shapping",
"_id": "4iMnHooBhKSDaY6labt-",
"_score": 1.0,
"_source": {
"title": "小米手机",
"img": "http://www.test.com/1.jpg",
"price": "3999"
}
},
{
"_index": "shapping",
"_id": "1000",
"_score": 1.0,
"_source": {
"title": "小米手机",
"img": "http://www.test.com/1.jpg",
"price": "3999"
}
}
]
}
}
根据ID修改某条数据
局部数据修改(推荐)
post http://127.0.0.1:9200/shapping/_update/1000
请求json体
// 华为修改为苹果
{
"doc" :{
"title" : "苹果手机"
}
}
全量修改:
需要把json中的每个字段都写上(不推荐)
put http://127.0.0.1:9200/shapping/_doc/1000
//需要修改的数据
{
"title" : "华为手机", //小米改华为
"img" : "http://www.test.com/123.jpg",
"price" : "399999"
}
根据ID删除数据
delete http://127.0.0.1:9200/shapping/_doc/1000
给索引添加映射关系
【1】
PUT /demo/_mapping
{
"properties":{
"name":{
"type":"text", // 类型为text 【text会被分词】
"index":true //当前name建立索引【可以被索引查询】
},
"age":{
"type":"keyword", // 不会被分词
"index":true
},
"tel":{
"type":"keyword",
"index":false // 索引中不缓存tel,不能通过tel 搜索
}
}
}
多字段模式
时间格式自动识别
查看所有索引
GET _cat/indices?v
查询指定的索引信息
GET api
给索引添加数据
POST /api/_doc #给api索引添加数据,自动分配ID
#/api/_doc/1 指定ID为1
#/api/_create/100
# _create == _doc
{
"name":"123"
}
查询文档信息
GET api/_doc/1 #查询指定文档
GET /api/_search #查询所有文档
删除指定索引
DELETE /api
修改文档记录
【全部修改】[不建议使用]
PUT /api/_doc/1
#全部修改
{
"name":"66666"
}
【return】
{
"_index": "api",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 17,
"_primary_term": 1
}
【部分修改】
POST /api/_update/1000
{
"doc":{
"age":666
}
}
es查询
【url查询】
q: 查询条件 age = 1
http://127.0.0.1:9200/api/_search?q=age:1
【body请求体查询】
get /api/_search
// 查询name=a,根据age 小到大排序
{
"query" : { //query 查询
"match":{ //match 匹配查询 match_all 查询所有
"name":"a" //要查询的字段name=a
}
},
"from":0, //分页其实值
"size":6 , //查询总数
"_source":["age"] , //只显示age
"sort":{
"age":{ // 根据age循序排序
"order":"asc"
}
}
}
==================================
【return】
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 13, //返回匹配的总数
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "api",
"_id": "YkFG_oABrskZ6DWlXrba",
"_score": null,
"_source": {
"age": 1
},
"sort": [
1
]
}
]
}
}
【多条件查询】
{
"query" : {
"bool":{
"should":[ // 必须一个条件即可 or
{
"match":{
"name" :"a"
}
},
{
"match":{
"age":5
}
}
]
}
}
}
{
"query" : {
"bool":{
"must":[ // 同时满足以下条件
{
"match":{
"name" :"小米"
}
},
{
"match":{
"status":1
}
}
],
"filter":{ //二次过滤
"range": {
"money": {
"gt":1000
}
}
}
}
},
"highlight" : { //高亮显示
"fields":{
"name": { //高亮显示的字段
"pre_tags": ["<strong>"], //亮显示的字段的起始标签
"post_tags": ["</strong>"] //亮显示的字段的结束标签
}
}
}
}
聚合查询
// get /api/_search
{
"aggs":{ //聚合操作
"money_group":{ //分组名称【随便取名】
"terms":{ //分组关键词【类似mysql group关键词】
"field":"money" //分组的字段
}
},
"money_avg":{ //平均数名 随便取名
"avg":{ //平均数
"field":"money" //需要求平均数的字段
}
}
},
"size":0 //只看聚合数据内容,不查看原始数据
}
//【return】
{
"took": 52,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 64,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"money_avg": {
"value": 9680.818181818182
},
"money_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 6,
"buckets": [
{
"key": 2999,
"doc_count": 3
},
{
"key": 5999,
"doc_count": 3
},
{
"key": 1999,
"doc_count": 2
},
{
"key": 3999,
"doc_count": 2
},
{
"key": 299,
"doc_count": 1
},
{
"key": 399,
"doc_count": 1
},
{
"key": 699,
"doc_count": 1
},
{
"key": 799,
"doc_count": 1
},
{
"key": 999,
"doc_count": 1
},
{
"key": 4999,
"doc_count": 1
}
]
}
}
}
多层json映射与查询
1.创建多层json映射
PUT /test/_mapping
{
"mappings":{
"properties":{
"name":{
"type":"text",
"index":true
},
"age":{
"type":"keyword",
"index":true
},
"tel":{
"properties":{
"msg":{
"type":"keyword",
"index":true
},
"data":{
"properties":{
"resu":{
"type":"text",
"index":true
}
}
}
}
}
}
}
}
2.给映射结构添加数据
post /test/_doc/
{
"name":"小米3",
"age":3,
"tel":{
"msg": 3,
"data":{
"resu": "hello world"
}
}
}
3.查询多层json
get /test/_search
{
"query" : {
"match":{
"tel.data.resu":"hello"
}
}
}
//【return】
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.52354836,
"hits": [
{
"_index": "test",
"_id": "hkFkBIEBrskZ6DWlrbYT",
"_score": 0.52354836,
"_source": {
"name": "小米",
"age": 1,
"tel": {
"msg": 1,
"data": {
"resu": "hello"
}
}
}
},
{
"_index": "test",
"_id": "iEFlBIEBrskZ6DWlR7aJ",
"_score": 0.39019167,
"_source": {
"name": "小米3",
"age": 3,
"tel": {
"msg": 3,
"data": {
"resu": "hello world"
}
}
}
}
]
}
}