ElasticSearch常用语句

  • 前言
  • 一、使用kibana操作es语句记录
  • 二、kibana索引管理示意图



前言


一、使用kibana操作es语句记录

kibana如何查看es中数据 kibana操作es数据_字段

#分词器使用
GET _analyze
{
  "analyzer": "ik_smart",
  "text":"我是zhangzhikai"
}

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "我是zhangzhikai"
}
GET _analyze
{
  "analyzer": "ik_smart",
  "text":"主席毛泽东"
}

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "主席毛泽东"
}
GET _analyze
{
  "analyzer": "ik_smart",
  "text":"他是张三"
}

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "他是张三"
}


# 创建test1索引 type1类型 id为1的文档 内容为 name,age,birthday  系统会默认给字段映射(类型)
PUT /test1/type1/2
{
  "name":"张志凯",
  "age":22,
  "birthday":"1998-01-02"
}


# 创建test2索引规则 mappings下properties中设置字段的type
PUT /test2
{
  "mappings": {
    "properties": {
      "name":{
       "type": "text" 
      },
      "age":{
        "type": "long"
      },
      "birthday":{
        "type": "date"
      }
      
    }
  }
}
#添加数据
POST /test2/type2/1
{
  "name":"站是",
  "age":11,
  "birthday":"2010-10-01"
  
}

# 获取test1索引type1类型id为2的信息
GET /test1/type1/2

# 获取test1索引的信息(未指定字段类型es会字段分配)
GET /test1

# 获取test2索引的信息
GET /test2/type2/1

# 创建test3索引 类型为_doc(8以后默认为_doc相当于是不每个类型下只能有这一个类型了)
PUT /test3/_doc/1
{
  "name":"zhang",
  "age":1,
  "birthdat":"2001-02-01"
}

# 获取test3索引的信息
GET /test3

# _cat/... 查看所有索引信息
GET _cat/indices

#修改索引信息put方式
PUT /test1/type1/1
{
  "name":"张三",
  "age":21,
  "birthday":"1998-02-02"
}

#修改索引信息
POST /test1/type1/1/_update
{
  "doc":{
    "name":"法内狂徒王五"
  }
}

#修改索引信息pos方式
POST /test1/type1/1
{
  "name":"李四",
  "age":22,
  "birthday":"1998-02-02"
}

#修改索引信息 _update 只修改当前指定字段其他原来的不动  
POST /test1/type1/1/_update
{
  "doc":{
    "name":"法外狂徒张三"
  }
}

#删除文档
DELETE test1/type1/2

#删除索引
DELETE test2/

# 查看索引中的信息
POST /test1/type1/_search
POST /goods/_doc/_search

#搜索test1索引下type1下搜索条件为name包含张三得
GET /test1/type1/_search?q=name:"张三"


#建立索引以及字段映射(字段类型)
PUT /test2
{
  "mappings": {
    "properties": {
      "name":{
        "type": "keyword"
      },
      "age":{
        "type": "long"
      }
    }
  }
}

#test2索引添加数据
PUT /test2/_doc/1
{
  "name":"花花爱Java",
  "age":34
}

#查看索引信息
GET /test2

#查看文档信息
GET /test2/_doc/1

#条件查询  keyword类型无法进行拆分即无法模糊查询
GET /test2/_search?q=name:"花花爱Java"

#条件查询与上面的结果一致
GET /test2/_doc/_search
{
  "query": {
    "match": {
      "name": "花花爱Java"
    }
  }
}
#精准查询不会对字段对应值进行分词
GET /test2/_search
{
  "query": {
    "term": {
      "name": "花花爱Java"
    }
  }
}

#创建用户索引
PUT /db1/user/1
{
  "name":"张三",
  "age":3,
  "desc":"法外狂徒张三",
  "hobby":["抽烟","喝酒","烫头"]
}

PUT /db1/user/2
{
  "name":"王五",
  "age":5,
  "desc":"豹子头王五",
  "hobby":["打架","吃肉","练兵"]
}
PUT /db1/user/3
{
  "name":"李四",
  "age":22,
  "desc":"嘻哈天王李四",
  "hobby":["学习","学习Java","工作"]
}
PUT /db1/user/4
{
  "name":"田七",
  "age":21,
  "desc":"傻妞田七",
  "hobby":["玩手机","刷抖音","睡觉"]
}
#查看索引信息
GET /db1

#条件搜索 参数系列
GET /db1/user/_search?q=name:"李"

#条件搜索json系列
GET /db1/_search
{
  "query": {
    "match": {
      "name": "张"
    }
  }
}

#搜索出来只显示指定的字段
GET /db1/_search
{
  "query": {
    "match": {
      "name": "张"
    }
  }
  , "_source": "name"
}

#排序指定每页多少(指定排序字段后_score的值变为null)
GET /db1/_search
{
  "query": {
    "match": {
      "name": "张"
    }
  }
  , "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ],
  "from": 0,
  "size": 2
}

#多条件精确查询    条件1 and 条件2都满足
GET /db1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "张"
          }
        },
        {
          "match": {
            "age": "22"
          }
        }
      ]
    }
  }
}

#条件1或者条件2 满足
GET /db1/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "张"
          }
        },
        {
          "match": {
            "age": "21"
          }
        }
      ]
    }
  }
}

#条件1 和条件2都不满足
GET /db1/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "张"
          }
        },
        {
          "match": {
            "age": "5"
          }
        }
      ]
    }
  }
}

#条件查询并过滤  名字中带张字并且年龄4<= ? <=25
GET /db1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "张"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 1,
            "lte": 25
          }
        }
      }
    }
  }
}

#过滤年龄在5到23之间的
GET /db1/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 5,
            "lte": 23
          }
        }
      }
    }
  }
}

#设置高亮
GET /db1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "李四"
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

#精确查找age是22和21的
GET /db1/_search
{
  "query": {
    "terms": {
      "age":[22,21]
    }
  }
}
#查询年龄为22或21
GET /db1/_search
{
  "query": {
    "bool": {
      "should": [
        {
            "term": {
                "age":{
                  "value": "22"
                } 
              }
        }
        ,
            {
              "term": {
              "age": {
                "value": "21"
              }
            }
            }
      ]
    }
  }
}

# 查看文档
GET /spring_index/_doc/1

#删除索引
DELETE /jd_goods

#搜索
GET /goods/_search
{
  "query": {
    "term": {
      "name": {
        "value": "java"
      }
    }
  }
  , "from": 0
  , "size": 10
}

# 创建索引并添加映射
PUT /goods3
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "price":{
        "type": "text"
      },
      "imgPath":{
          "type": "keyword"
        }
      }
    }
  }
  
# 查看索引信息
GET /goods3/_search
{
  "query": {
    "match": {
      "name": "权威指南"      
    }
  }
  , "from": 0
  , "size": 10
}

二、kibana索引管理示意图

kibana如何查看es中数据 kibana操作es数据_搜索_02