← 返回首页
Elasticsearch-Mapping操作
发表时间:2021-10-19 10:41:01
Elasticsearch-Mapping操作

1.type逐步取消 type在6.x中只能有一个,可自定义。 type在7.x中只能有一个,且只能是_doc。 type在8.x(未来版本)彻底取消,API会大变,以前的INDEX/TYPE/DOC的方式会变成INDEX/DOC。

2.字段类型 |分类|具体类型| |-|-| |字符串类型|text,keyword| |整数类型|byte ,short, integer, long| |浮点类型|float,double| |逻辑类型|boolean| |日期类型|date| |逻辑类型|boolean| |范围类型|range| |二进制类型|binary| |数组类型|array| |对象类型|object| |嵌套类型|nested| |IP类型|ip| |令牌计数类型|token_count|

3.创建mapping

#创建索引
PUT /students
#给索引创建mapping
PUT /students/_mapping/_doc
{
  "properties": {
    "sid": {
      "type": "keyword"
    },
    "sname": {
      "type": "text"
    },
    "gender": {
      "type": "text"
    }
  }
}
#查询索引
GET /students

返回结果:

# PUT /students
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "students"
}

# PUT /students/_mapping/_doc
{
  "acknowledged": true
}

# GET /students
{
  "students": {
    "aliases": {},
    "mappings": {
      "_doc": {
        "properties": {
          "gender": {
            "type": "text"
          },
          "sid": {
            "type": "keyword"
          },
          "sname": {
            "type": "text"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1634611154408",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "b7HWdVIsT3y61zuB76KL6Q",
        "version": {
          "created": "6020499"
        },
        "provided_name": "students"
      }
    }
  }
}

4.数据CRUD

#插入数据
POST /students/_doc/1
{
  "sid": "s0001",
  "sname": "张三",
  "gender": "男"
}
#查询
GET /students/_doc/1

#修改
POST /students/_doc/1
{
  "sid": "s0001",
  "sname": "张三",
  "gender": "女"
}

#查询
GET /students/_doc/1

#删除
DELETE /students/_doc/1

返回结果:

# POST /students/_doc/1
{
  "_index": "students",
  "_type": "_doc",
  "_id": "1",
  "_version": 9,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 8,
  "_primary_term": 1
}

# GET /students/_doc/1
{
  "_index": "students",
  "_type": "_doc",
  "_id": "1",
  "_version": 9,
  "found": true,
  "_source": {
    "sid": "s0001",
    "sname": "张三",
    "gender": "男"
  }
}

# POST /students/_doc/1
{
  "_index": "students",
  "_type": "_doc",
  "_id": "1",
  "_version": 10,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 9,
  "_primary_term": 1
}

# GET /students/_doc/1
{
  "_index": "students",
  "_type": "_doc",
  "_id": "1",
  "_version": 10,
  "found": true,
  "_source": {
    "sid": "s0001",
    "sname": "张三",
    "gender": "女"
  }
}

# DELETE /students/_doc/1
{
  "_index": "students",
  "_type": "_doc",
  "_id": "1",
  "_version": 11,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 10,
  "_primary_term": 1
}

# GET /students/_doc/1
{
  "_index": "students",
  "_type": "_doc",
  "_id": "1",
  "found": false
}

5.使用自动生成主键

#插入数据使用自动生成主键
POST /students/_doc
{
  "sid": "s0001",
  "sname": "李四",
  "gender": "男"
}

#查询所有记录
GET /students/_doc/_search

6.条件更新

根据条件更新某个字段的值。

GET /students/_doc/qe-BlnwBtWRssnrTfKtw

#更新单个字段
POST students/_doc/_update_by_query
{
  "query": {
    "match": {
      "_id": "qe-BlnwBtWRssnrTfKtw"  
    }
  },
  "script": {
    "source": "ctx._source['sname'] = '郭靖'"
  }
}

GET /students/_doc/qe-BlnwBtWRssnrTfKtw

# 更新多个字段
POST students/_doc/_update_by_query
{
  "query": {
    "match": {
      "_id": "qe-BlnwBtWRssnrTfKtw"  
    }
  },
  "script": {
    "source": "ctx._source['sname'] = '乔峰';ctx._source['sid']='100'"
  }
}

GET /students/_doc/qe-BlnwBtWRssnrTfKtw

返回结果:

# GET /students/_doc/qe-BlnwBtWRssnrTfKtw
{
  "_index": "students",
  "_type": "_doc",
  "_id": "qe-BlnwBtWRssnrTfKtw",
  "_version": 8,
  "found": true,
  "_source": {
    "gender": "男",
    "sname": "洪七公",
    "sid": "1"
  }
}

# POST students/_doc/_update_by_query
{
  "took": 8,
  "timed_out": false,
  "total": 1,
  "updated": 1,
  "deleted": 0,
  "batches": 1,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1,
  "throttled_until_millis": 0,
  "failures": []
}

# GET /students/_doc/qe-BlnwBtWRssnrTfKtw
{
  "_index": "students",
  "_type": "_doc",
  "_id": "qe-BlnwBtWRssnrTfKtw",
  "_version": 9,
  "found": true,
  "_source": {
    "gender": "男",
    "sname": "郭靖",
    "sid": "1"
  }
}

# POST students/_doc/_update_by_query
{
  "took": 12,
  "timed_out": false,
  "total": 1,
  "updated": 1,
  "deleted": 0,
  "batches": 1,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1,
  "throttled_until_millis": 0,
  "failures": []
}

# GET /students/_doc/qe-BlnwBtWRssnrTfKtw
{
  "_index": "students",
  "_type": "_doc",
  "_id": "qe-BlnwBtWRssnrTfKtw",
  "_version": 10,
  "found": true,
  "_source": {
    "gender": "男",
    "sname": "乔峰",
    "sid": "100"
  }
}