Kibana operation ES full coverage basic query DSL query

Kibana

concept

What is Kibana

  • KibanaIt is a target Elasticsearch mysql, 开源分析及可视化平台using Kibana to `query, view and interact with the data stored in the ES index

Install

install by yourself

instruction operation

index operation

  • View all indexes
GET /_cat/indices
  • View all indexes and parameter descriptions
GET /_cat/indices?v
//返回结果说明
health status   index  uuid pri rep docs.count docs.deleted 
健康状态  索引状态  索引   id  主分片 副本分片 文档数  删除         
store.size   pri.store.size
存储大小      主分片存储大小
  • create index
//  创建索引 
PUT /索引名
  • Create index custom shards
PUT /索引名
{
    
    
  "settings": {
    
    
    "number_of_shards": 1,  # 主分片数量
    "number_of_replicas": 0  # 副本分片数量
  }
}
  • Create indexes and maps
ES数据类型说明
//字符串类型         数字类型         小数类型            布尔类型   日期类型
keyword  text    integer long      float double       boolean    date

//创建索引及其映射
// 创建索引及映射  注:student为索引名  id、name、age、desc 为字段名
PUT /student
{
    
    
  "settings": {
    
    
    "number_of_shards": 1, 
    "number_of_replicas": 0
  },
  "mappings": {
    
    
    "properties": {
    
    
       "id":{
    
    
         "type": "integer"
       },
       "name":{
    
    
         "type":"keyword"
       },
       "age":{
    
    
         "type": "double"
       },
       "desc":{
    
    
         "type": "text"
       }
    }
  } 
}
  • View index and mapping details
GET /products/_mapping
  • delete index
//  删除索引
DELETE /索引名

document manipulation

  • query document
 GET /student/_doc/1  (1为文档id)
  • Query all documents in the index
GET /student/_doc/_search/
{
    
    
  "query":{
    
    
    "match_all":{
    
    }
  }
}
  • add document
  //        此处  1为文档id、查询用到的就是这个id 不是下面那个id字段
POST /student/_doc/1
{
    
    
  "id":1,
  "name":"小明",
  "age":"19",
  "desc":"小明是学渣"
}
  • delete document
DELETE /student/_doc/1 
  • update document
//更新文档 不保留原字段  
PUT /student/_doc/1
{
    
    
  "name":"ipone"
}

//更新文档 保留原字段
POST /products/_doc/1/_update
{
    
    
"doc":{
    
    
	"name":"明明"
	}
}
  • batch add
//注:json格式必须是这样 不然会报错
POST /student/_doc/_bulk
{
    
    "index":{
    
    "_id":3}}
{
    
    "id":3,"name":"小李","age":"22","desc":"小李是混混"}
{
    
    "index":{
    
    "_id":"4"}}
{
    
    "id":4,"name":"陈陈","age":"33","desc":"陈陈是老师"}

  • Batch Edit
POST /student/_doc/_bulk
{
    
    "update":{
    
    "_id":"2"}}
{
    
    "doc":{
    
    "name":"芳芳"}}
{
    
    "update":{
    
    "_id":"3"}}
{
    
    "doc":{
    
    "name":"李李"}}
  • batch deletion
POST /student/_doc/_bulk
{
    
    "delete":{
    
    "_id":"5"}}
{
    
    "delete":{
    
    "_id":"6"}}

Advanced query Query DSL

Introduction to DSL

Query DSL uses the Rest API to transfer JSON-formatted request body (Request Body) data to interact with ES. To put it
bluntly, this DSL is a complex query, including some paging queries, keyword queries, sorting queries, etc.

command operation

  • term (keyword query)
    here you need to pay attention to word segmentation because the process of ES storage is to store a piece of information according to the specified word segmentation rules. The information will be divided into two keywords "we" and "meal" for storage, so when you want to search for documents based on this field, you need to pass in "we" or "eat", if you pass in "me" or "we This information cannot be searched.
    Note: If you do not specify the Chinese word segmentation used by the default tokenizer ES, that is, "we eat" will be divided into we, we, eat, and rice for storage.
// 上文介绍到 ES的字符串类型分别为 keyword  text ,keyword是不会进行分词存储的,也就是只有字段类型是text的时候,
//才会出现上述情况。keyword必须用全部信息进行检索。

// desc字段是text类型,会进行分词, ES默认中文分词是按字分词 ,故文档中只要包含是这个字的都会返回
GET /student/_search
{
    
    
  "query": {
    
    
    "term": {
    
    
      "desc": {
    
    
        "value": "是"
      }
    }
  }
}

// name 是keyword类型,故不会进行分词 必须使用全部信息检索。
GET /student/_search
{
    
    
  "query": {
    
    
    "term": {
    
    
      "name": {
    
    
        "value": "小明"
      }
    }
  }
}


  • range (range query)
// 参数说明   gt   gte    lt   lte
//          大于 大于等于 小于 小于等于
GET /student/_search
{
    
    
  "query": {
    
    
    "range":{
    
    
      "age":{
    
    
        "gt":"19"
      }
    }
  }
}
  • prefix (prefix query)
//该行为与分词无关

GET /student/_search
{
    
    
  "query": {
    
    
    "prefix": {
    
    
      "name": {
    
    
        "value": "小"
      }
    }
  }
}
  • wildcard (wildcard)
// 参数说明    ?          *
//         匹配一个    匹配多个
GET /student/_search
{
    
    
  "query":{
    
    
    "wildcard": {
    
    
      "name": {
    
    
        "value": "小?"
      }
    }
  }
}
  • ids (multiple id query)
GET /student/_search
{
    
    
  "query": {
    
    
    "ids": {
    
    
      "values": ["1","2"]
    }
  }
}
  • bool (multi-condition query)
//bool关键字 用来组合多个条件实现复杂查询
//must:相当于&&
//should:相当于||
//must_not:相当于!不能满足任何一个

GET /student/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    
          "ids":{
    
    
            "values":[1,2,3,4]
          }
        },{
    
    
          "term":{
    
    
            "desc":{
    
    
              "value": "小"
            }
          }
        }
      ]
    }
  }
}


  • Multi-field query (multi_match)
//字段分词则按分词检索。

GET /student/_search
{
    
    
  "query": {
    
    
    "multi_match": {
    
    
      "query": "小",
      "fields": ["desc","name"]
    }
  }
}


  • Return the specified number of items (size)
// 默认10条

GET /student/_doc/_search/
{
    
    
  "query":{
    
    
    "match_all":{
    
    }
  }"size":5
}

  • Pagination (form)
//返回指定起始返回位置,和size实现分页
GET /student/_doc/_search/
{
    
    
  "query":{
    
    
    "match_all":{
    
    }
  }"size":5,
  "from":0
}

  • Specify field sorting (sort)
// 参数说明 desc 降序  asc 升序

GET /student/_search
{
    
    
  "query":{
    
    
      "match_all":{
    
    }
  },
  "sort":[
    {
    
    
        "age":{
    
    
        "order":"asc"
        }
    }
  ]
}

  • Only query the specified field (source)

GET /student/_doc/_search/
{
    
    
  "query":{
    
    
    "match_all":{
    
    }
  },
  "_source":["id","desc"]
}


springBoot integrates ES

Another article on the homepage

Guess you like

Origin blog.csdn.net/pgcdnameming/article/details/127936347