一、Restful 风格说明

一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。 基本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 查询索引、类型下的所有文档

二、基础操作

2.1、指定字段类型创建index

2.1.1、字段类型

  • 字符串类型
    • text
    • keyword
  • 数值类型
    • long
    • integer
    • short
    • byte
    • double
    • float
    • half_float
    • scaled_float
  • 日期类型
    • date
  • 布尔值类型
    • boolean
  • 二进制类型
    • binary

2.1.2、指定字段类型创建索引

语法:

  1. PUT /索引名称
  2. {
  3. "mappings":{
  4. "properties"{
  5. "fieldName":{
  6. "type": ""
  7. }
  8. }
  9. }
  10. }

示例:

  1. PUT /tindex
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name":{
  6. "type": "text"
  7. },
  8. "age":{
  9. "type": "integer"
  10. },
  11. "sex":{
  12. "type": "text"
  13. },
  14. "tags":{
  15. "type": "text"
  16. }
  17. }
  18. }
  19. }

【Elasticsearch实践】(五)ES搜索 - 图1
查看index具体信息:
【Elasticsearch实践】(五)ES搜索 - 图2
注意:

  • es7.x版本,一个index仅仅支持一个type (ex8.x版本以后会废除type的概念),因此通过上述方式创建的index,会有一个默认的type: _doc
  1. 通过上述方式创建的index,在 PUT 数据时,需要指定type_doc,否则会报错:
  2. {
  3. "error" : {
  4. "root_cause" : [
  5. {
  6. "type" : "illegal_argument_exception",
  7. "reason" : "Rejecting mapping update to [tindex] as the final mapping would have more than 1 type: [_doc, user]"
  8. }
  9. ],
  10. "type" : "illegal_argument_exception",
  11. "reason" : "Rejecting mapping update to [tindex] as the final mapping would have more than 1 type: [_doc, user]"
  12. },
  13. "status" : 400
  14. }
  • 如果未指定类型,es会配置默认字段类型

2.2、添加数据

  1. # 添加数据
  2. PUT /tindex/_doc/1
  3. {
  4. "name": "wells",
  5. "age": 18,
  6. "sex": "man",
  7. "tags": [
  8. "技术宅",
  9. "足球",
  10. "直男"
  11. ]
  12. }
  13. PUT /tindex/_doc/2
  14. {
  15. "name": "wells学es",
  16. "age": 20,
  17. "sex": "man",
  18. "tags": [
  19. "宅男",
  20. "乒乓boy",
  21. "唱歌"
  22. ]
  23. }
  24. PUT /tindex/_doc/3
  25. {
  26. "name": "jerry",
  27. "age": 40,
  28. "sex": "woman",
  29. "tags": [
  30. "女强人",
  31. "唱歌",
  32. "看书"
  33. ]
  34. }
  35. PUT /tindex/_doc/4
  36. {
  37. "name": "tom",
  38. "age": 2,
  39. "sex": "man",
  40. "tags": [
  41. "sqlboy",
  42. "篮球",
  43. "吃货"
  44. ]
  45. }

通过 elasticsearch-head 查看当前index结果:
【Elasticsearch实践】(五)ES搜索 - 图3

2.3、获取数据

  1. # 获取数据
  2. GET /tindex/_doc/1

【Elasticsearch实践】(五)ES搜索 - 图4

2.4、更新数据

推荐使用POST _update方式,原因是:PUT如果不传递值会被覆盖

2.4.1、PUT

  1. # PUT更新数据
  2. PUT /tindex/_doc/1
  3. {
  4. "name": "wells_rename"
  5. }

【Elasticsearch实践】(五)ES搜索 - 图5

2.4.2、POST

  1. # POST 更新数据
  2. POST /tindex/_doc/1/_update
  3. {
  4. "doc": {
  5. "age": 17
  6. }
  7. }

【Elasticsearch实践】(五)ES搜索 - 图6

2.5、简单条件查询

  1. GET /tindex/_doc/_search?q=name:wells

【Elasticsearch实践】(五)ES搜索 - 图7

参考

ES基本操作一 :如何创建、修改、删除索引;批量创建索引;打开、关闭索引;冻结、解冻索引