es初探

es官网介绍

kibana操作es

Elaticsearch,简称为es,对es的操作都是基于REST风格的操作;
基本的操作有:PUT(创建,修改) POST(创建,修改) DELETE(删除) GET(查询) POST(查询) ;就是我们平常干的最多的增删改查;查询是最主要最复杂的.
我贴的都是可以直接复制到kibana里面的,然后数据的大家可以自己多造一点,可以自己根据查询的造.

简单操作

# 获取ElasticSearch的当前的很多信息!
GET _cat/
GET /_cat/plugins

## 分词
GET _analyze
{
  "analyzer": "ik_smart",
  "text": "年轻人不讲武德"
}

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "年轻人不讲武德"
}

# 创建索引,创建过后不能修改
PUT /test1
{
  
  "mappings": {
    "properties": {
      "name":{
        "type": "text"
      },
      "age":{
        "type": "long"
      },
      "birthday":{
        "type": "date"
      },
       "tag":{
        "type": "text"
      }
    }
  }

}
# 获取索引信息
GET test1
## 删除索引
DELETE test1

# 新增文档  指定id,跟随机id
PUT /test1/_doc/1 
{
  "name":"张益达",
  "age":11,
  "birthday": "1990-08-09"
}

POST /test1/_doc/
{
  "name":"张益达",
  "age":11,
  "birthday": "1990-08-09"
}

# 修改文档
# put覆盖,_version会+1 如果不传为空(不建议,会丢失字段,除非确实想全部覆盖)
PUT /test1/_doc/1
{
  "name" : "张益达",
  "age" : 183
}
GET /test1/_doc/1
# post _update 建议,_version不变(建议,注意doc字段)
POST /test1/_update/1/
{
  "doc": {
  "name" : "张益达2"
  }
}
GET /test1/_doc/1

查询操作

查询基本操作

## 查询
##  Deprecation: [types removal] Specifying types in search requests is deprecated.会让你删掉type,_doc
GET /test1/_doc/_search?q=name:益
GET /test1/_search?q=name:益
GET /test1/_search?q=age:11

##查询匹配
##match:匹配(会使用分词器解析(先分析文档,然后进行查询))
## term: 直接通过倒排索引指定词条,精准查询,不进行分词,适合的字段类型有number,date,keyword,不适合text
##_source:过滤字段
##sort:排序
##form、size 分页
GET /test1/_search
{
  "query": {
    "match": {
      "name": "张"
    }
  }
   ,
  "_source": ["name","age"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ],
  "from": 1
  ,
  "size": 1
}

# 多条件查询
#must 相当于 and
#should 相当于 or
#must_not 相当于 not (... and ...)
#filter 过滤
GET /test1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "1"
          }
        },
        {
           "match": {
            "age": "1"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 1,
              "lte": 2
            }
          }
        }
      ]
    }
  }
}

# 创建带数组的索引,索引不存在会自动创建

POST /test_arr/_doc
{
  "name" : "张益达2",
  "age" : 18,
  "birthday":"1990-09-08",
  "tag":["律师","搞笑","自信"]
}

POST /test_arr/_doc
{
  "name" : "张益达3",
  "age" : 18,
  "birthday":"1990-09-08",
  "tag":["律师" ,"男"]
}
GET /test_arr/_doc/1

GET /test_arr/_search
{
  "query": {
    "match": {
      "tag": "男 师"
    }
  }
}
#  match 会使用分词器解析(先分析文档,然后进行查询)
GET /test_arr/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "张"
          }
        },
        {
           "match": {
            "tag": "男律师搞笑女自信人"
          }
        }
      ]
    }
  }
}

# term精准查询
GET /test1/_search
{
  "query": {
    "term": {
      "age": 1
    }
  }
}

批量操作

bulk语法
bulk对JSON串的有着严格的要求。每个JSON串不能换行,只能放在同一行,同时,相邻的JSON串之间必须要有换行。bulk的每个操作必须要一对JSON串(delete语法除外)
例如若增加一个文档如下所示:


## 批量操作

POST _bulk
{"create": {"_index": "yida", "_id": 11}}
{"name": "test_bulk", "age":"100"}

## 批量删除
POST _bulk
{"delete": {"_index": "yida","_id": "1"}}
{"delete": {"_index": "yida","_id": "2"}}
{"delete": {"_index": "yida","_id": "3"}}
{"delete": {"_index": "yida","_id": "4"}}

## 可以又新增又删除
POST _bulk
{"index": {"_index": "yida","_id": 11}}
{"name": "test_bulk", "age":"101"}
{"index": {"_index": "yida","_id": 12}}
{"name": "test_bulk", "age":"101"}
{"delete": {"_index": "yida","_id": "11"}}
{"delete": {"_index": "yida","_id": "12"}}

操作类型

  • create 如果文档不存在就创建,但如果文档存在就返回错误
  • index 如果文档不存在就创建,如果文档存在就更新
  • update 更新一个文档,如果文档不存在就返回错误
  • delete 删除一个文档,如果要删除的文档id不存在,就返回错误
    从以上可以看出index是比较常用的,因为bulk操作失败不会影响其他文档操作,我们可以从他的返回结果中查看失败的详细原因。

text和keyword

  • text:
    支持分词,全文检索、支持模糊、精确查询,不支持聚合,排序操作;
    text类型的最大支持的字符长度无限制,适合大字段存储
  • keyword:
    不进行分词,直接索引、支持模糊、支持精确匹配,支持聚合、排序操作。
    keyword类型的最大支持的长度为——32766个UTF-8类型的字符,可以通过设置ignore_above指定自持字符长度,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果。
## 测试keyword和text是否支持分词
PUT /test
{
  "mappings": {
    "properties": {
      "text":{
        "type":"text"
      },
      "keyword":{
        "type":"keyword"
      }
    }
  }
}

PUT /test/_doc/1
{
  "text":"测试keyword和text是否支持分词",
  "keyword":"测试keyword和text是否支持分词"
}

# text 支持分词
# keyword 不支持分词

# 查得到
GET /test/_doc/_search
{
  "query":{
   "match":{
      "text":"测试"
   }
  }
}
## 查不到
GET /test/_doc/_search
{
  "query":{
   "match":{
      "keyword":"测试"
   }
  }
}

# 分词分析
GET _analyze
{
  "analyzer": "keyword",
  "text": ["测试liu"]
}

GET _analyze
{
  "analyzer": "standard",
  "text": ["测试liu"]
}

GET _analyze
{
  "analyzer":"ik_max_word",
  "text": ["测试liu"]
}

高亮查询


## 高亮查询,主要就是在查询的内容加前缀后缀
GET test1/_doc/_search
{
  "query": {
    "match": {
      "name":"张"
    }
  }
  ,
  "highlight": {
    "pre_tags": "<p class='key' style='color:red'>",
    "post_tags": "</p>", 
    "fields": {
      "name": {}
    }
  }
}


至此kibana操作es的一些常用操作就完成,更详细的可以看官方文档:

https://www.elastic.co/guide/cn/elasticsearch/guide/current/full-body-search.html

下一节使用java代码操作es

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐