Kibana操作ElasticSearch

2021-10-28  本文已影响0人  王宣成

对比关系型数据库
Elasticsearch6.0以后删除了Type类型

image.png

进入开发工具

image.png
# 文档操作
GET /_cat/node  #查看所有节点
GET /_cat/health  #查看es健康状况
GET /_cat/master #查看主节点
GET /_cat/indices #查看所有索引
#保存文档
PUT index/types/_id
{
  "name":"name1"
}

#保存文档加_update
POST index/types/_id/_update
{
  "doc":{
    "name":"name2"
  }
}

#新增文档
POST index/types
{
  "name":"name1"
}
#查询文档
POST index/types/_id
{
  "name":"name1"
}
#删除文档
DELETE index/types/_id
#批量操作加_bulk
POST index/types/_bulk
{"index":{"_id":1}}
{"name":"name1"}
{"index":{"_id":2}}
{"name":"name2"}

查询

#q=* 查询所有,sort=balance 按balance排序,asc升序排序
GET index/_search?q=*&sort=balance:asc
#query查询条件,match_all查询所有,sort排序条件
GET index/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "balance": "asc"
    }
  ]
}
# 分页操作, from是从第几条数据开始,size是一页多少个,默认是十条数据, _source配置,需要过滤的字段
GET index/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "balance": "asc"
    }
  ],
  "from": 1,
  "size": 10, 
  "_source": ["name","balance"]
}
#全文检索
GET index/_search
{
  "query": {
    "match": {
      "age": 20
    }
  }
}

# 通配符的模糊检索
GET index/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "*三*"
      }
    }
  }
}

# 精确匹配 match_phrase
GET index/_search
{
  "query": {
    "match_phrase": {
      "age": 20
    }
  }
}

# 多字段匹配multi_match
GET index/_search
{
  "query": {
    "multi_match": {
      "query": "mill",
      "fields": ["address","email"]
    }
  }
}

#复合查询,must必须,must_not必须不满足,should应该满足,range是区间查询
GET index/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "gender": "F"
        }},
        {"match": {
          "address": "Mill"
        }}
      ],
      "must_not": [
        {"match": {
          "age": "38"
        }}
      ],
      "should": [
        {"match": {
          "lastname": "Long"
        }}
      ],
      "range": {
        "age": {
            "gte": 18,
            "lte": 35
        }
      }
    }
  }
}

# 区间查询
GET customer/_search
{
  "query": {
  "bool": {
    "filter": [
      {"range": {
        "age": {
          "gte": 18,
          "lte": 30
        }
      }}
    ]
  }
  }
}

#精确字段查询term
GET index/_search
{
  "query": {
    "term": {
      "age": "28"
    }
  }
}


有keyword的时候精确查找,没有keyword的时候这个值会当成一个关键字
GET index/_search
{
  "query": {"match": {
    "address.keyword": "789 Madison"
  }}
}

GET index/_search
{
  "query": {"match_phrase": {
    "address": "789 Madison"
  }}
}

聚合查询

# 搜索address中包含mill的所有人的年龄分布以及平均年龄,但不显示这些人的详情
# aggs代表使用聚合函数,terms为结果种类求和,avg为平均值,size为0则不显示详细信息
GET index/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  },
  "aggs": {
    "ageagg": {
      "terms": {
        "field": "age",
        "size": 10
      }
    },
    "ageavg":{
      "avg": {
        "field": "age"
      }
    }
  },
  "size": 0
}

#聚合中还可以有子聚合
GET index/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "ageagg": {
      "terms": {
        "field": "age",
        "size": 10
      },
      "aggs": {
        "ageAvg": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  },
  "size": 0
}


上一篇下一篇

猜你喜欢

热点阅读