04 elasticsearch学习笔记-Rest风格说明

Rest风格说明

Rest风格说明

method url地址 描述
PUT localhost:9200/索引名称/类型名称/文档id 创建文档(指定文档id)
POST localhost:9200/索引名称/类型名称 创建文档(随机文档id)
POST localhost:9200/索引名称/类型名称/文档id/_update 修改文档
DELETE localhost:9200/索引名称/类型名称/文档id 删除文档
GET localhost:9200/索引名称/类型名称/文档id 通过文档id查询文档
POST localhost:9200/索引名称/类型名称/_search 查询所有的数据

关于文档的基本操作

添加数据PUT

PUT /haima/user/1
{
     "name":"狂神说123",
     "age":13,
     "desc":"我是描述",
     "tags":["技术宅","温暖","直男"]
}

返回数据

#! Deprecation: [types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead.
{
  "_index" : "haima",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 7,
  "found" : true,
  "_source" : {
    "name" : "狂神说123",
    "age" : 13,
    "desc" : "我是描述",
    "tags" : [
      "技术宅",
      "温暖",
      "直男"
    ]
  }
}

查询

最简单的搜索是GET

GET /haima/user/1

返回数据

#! Deprecation: [types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead.
{
  "_index" : "haima",
  "_type" : "user",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 7,
  "found" : true,
  "_source" : {
    "name" : "狂神说123",
    "age" : 13,
    "desc" : "我是描述",
    "tags" : [
      "技术宅",
      "温暖",
      "直男"
    ]
  }
}

多添加几条数据

PUT /haima/user/2
{
     "name":"张三说",
     "age":22,
     "desc":"我是描述22",
     "tags":["技术宅","温暖","直男"]
}


PUT /haima/user/3
{
     "name":"李四说",
     "age":33,
     "desc":"我是描述33",
     "tags":["技术宅","温暖","直男"]
}

PUT /haima/user/4
{
     "name":"王五说",
     "age":44,
     "desc":"我是描述44",
     "tags":["技术宅","温暖","直男"]
}

  1. 搜索功能search

GET /haima/user/_search?q=name:李四

这边name是text 所以做了分词的查询 如果是keyword就不会分词搜索了


  1. 复杂操作搜索select(排序,分页,高亮,模糊查询,精准查询)
//测试只能一个字段查询
GET lisen/user/_search
{
  "query": {
    "match": {
      "name": "李森"
    }
  }
}

结果过滤,就是只展示列表中某些字段

GET /haima/user/_search
{
  "query": {
    "match": {
      "name": "李四"
    }
  },
  "_source":["name","age"]
}

字符串包涵 或者查询

es里存的数据为字符串
c_other_tags:"70,80,90"

c_other_tags包涵"70," || c_other_tags包涵"70," || c_other_tags包涵"*,70" || c_other_tags="70"

{
  "query": {
    "bool": {
      "should": [
        {
          "wildcard": {
            "c_other_tags": "70,*"
          }
        },
        {
          "wildcard": {
            "c_other_tags": "*,70,*"
          }
        },
        {
          "wildcard": {
            "c_other_tags": "*,70"
          }
        },
        {
          "match": {
            "c_other_tags.keyword": "70"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 10
}

包含(只返回指定字段)

不包含(不返回指定字段,排除字段)

GET /haima/user/_search
{
  "query": {
    "match": {
      "name": "李四"
    }
  },
  "_source":{"excludes":["name","age"]}
}

排序 asc(正序) / desc(倒序)

分页


GET /haima/user/_search
{
  "query": {
    "match": {
      "name": "说"
    }
  },
  "sort":{
    "age":{
      "order":"asc" 
    }
  },
  "from":0,
  "size":2
}

多条件查询

布尔值查询

must(and),所有的条件都要符合

GET /haima/user/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "name": "说"
          }
        },{
          "match": {
            "age": "22"
          }
        }
      ]
    }
  }
}

should(or)或者的关系,有一个条件成立即可

GET /haima/user/_search
{
  "query":{
    "bool": {
      "should": [
        {
          "match": {
            "name": "王五"
          }
        },{
          "match": {
            "age": "44"
          }
        }
      ]
    }
  }
}

must_not(not)不等于

GET /haima/user/_search
{
  "query":{
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "王五"
          }
        }
      ]
    }
  }
}

条件区间

在查询的结果上,过滤年龄区间符合条件的

GET /haima/user/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "name": "王五"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 50
          }
        }
      }
    }
  }
}

  • 大于 gt
  • 大于等于 gte
  • 小于 lt
  • 小于等于 lte

匹配多个条件(数组)

GET /haima/user/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "tags": "技术 女"
          }
        }
      ]
    }
  }
}

match没用倒排索引 这边改正一下

精确查找

更多term和match的区别参考下面文档:

https://www.jianshu.com/p/d5583dff4157

term查询是直接通过倒排索引指定的词条进程精确查找的
关于分词

- term,直接查询精确的
- match,会使用分词器解析!(先分析文档,然后通过分析的文档进行查询)

GET _analyze
{
  "analyzer":"standard",
  "text":"李一封看看"
}

GET _analyze
{
  "analyzer":"standard",
  "text":"张三说"
}

默认的是被分词了

GET _analyze
{
  "analyzer":"keyword",
  "text":"狂神说"
}


keyword没有被分词

精确查询多个值

高亮

还能自定义高亮的样式

修改文档

  1. 修改我们可以还是用原来的PUT的命令,根据id来修改


但是如果没有填写的字段 会重置为空了 ,相当于java接口传对象修改,如果只是传id的某些字段,那其他没传的值都为空了。

  1. 还有一种update方法 这种不设置某些值 数据不会丢失
POST /test3/_doc/1/_update
{
  "doc":{
    "name":"212121"
  }
}




GET /test3/_doc/1

  1. 访求三
#修改文档 es7中推荐这种,因为默认只有_doc类型,所以可以省略
POST /test3/_update/1
{
  "doc":{
    "name":"张三"
  }
}

返回结果:

{
  "_index" : "test3",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 8,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 8,
  "_primary_term" : 2
}

带doc修改 查询也是带doc的(document)

//下面两种都是会将不修改的值清空的


POST /test3/_doc/1
{
    "name":"212121"
}

POST /test3/_doc/1
{
  "doc":{
    "name":"212121"
  }
}

删除索引或者文档

关于删除索引或者文档的操作

  1. DELETE /test1 删除索引

  1. DELETE /test1/_doc/1 删除test1索引里主键为1的文档

通过DELETE命令实现删除,根据你的请求来判断是删除索引还是删除文档记录

使用RESTFUL的风格是我们ES推荐大家使用的!

  1. 按搜索条件删除某一索引下所有数据
POST http://127.0.0.1:9200/index_name/type_name/_delete_by_query
{
  "query": {"match_all": {}}
}

curl:

curl -u用户名:密码 -XPOST '127.0.0.1:9200/index_name/type_name/_delete_by_query?refresh&slices=5&pretty' -H 'Content-Type: application/json' 
-d'{
  "query": {
    "match_all": {}
  }
}'

返回数据:

{
    "took": 21832,
    "timed_out": false,
    "total": 27008,
    "deleted": 27008,
    "batches": 28,
    "version_conflicts": 0,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1.0,
    "throttled_until_millis": 0,
    "failures": []
}

参考笔记:
https://blog.csdn.net/lisen01070107/article/details/108288037
https://blog.csdn.net/mgdj25/article/details/105740191
https://www.jianshu.com/p/eca8ddb812eb

posted @ 2021-01-03 23:37  HaimaBlog  阅读(170)  评论(0编辑  收藏  举报