Elasticsearch基础之快速入门

本文不介绍 Elasticsearch 和 Kibana的安装,文中 Elasticsearch简称为 ES,相关操作示例使用的是 Kibana工具。

文档 Document

用户存储在ES中的数据文档

  • Json Object,由字段(Field)组成,常见数据类型如下
  1. 字符串:text(分词),keyword(不分词
  2. 数值型:long,integer,short,byte,double,float,half_flot,scaled_float
  3. 布尔:boolean
  4. 日期:date
  5. 二进制:binary
  6. 范围类型:integer_range,float_range,long_range,double_range,date_range
  • 每个文档都有一个唯一的id标识,有以下两种生成方式
  1. 传入参数,自行指定
  2. 未指定时,ES自动生成
  • 元数据,用于标注文档的相关信息
  1. _index:文档所在的索引名
  2. _type:文档所在的类型名
  3. _id:文档的唯一id
  4. _uid:组合id,由_type和_id组成(6.0以后_type不再起作用,同_id一样)
  5. _source:文档的原始Json数据,可以从这里获取每个字段的内容
  6. _all:整合所有字段内容到该字段,默认禁用

索引 Index

具有相同字段的文档列表组成(ES6.0以后去除了type的概念,所以index相当于MySQL概念中的表)

  • 每个index所以都有自己的mapping定义,用于定义字段名和字段类型

节点 Node

一个ES的运行实例,是集群的构成单元

集群 Cluster

由一个或多个节点组成,对外提供服务

  • 一个集群可以有多个index索引
    比如我们可以按照不同日期把每天的日志都生成一个index索引来存储

REST API

ES集群对外提供RESTful API

  • REST (Representational State Transfer),即表述性状态传递
  • URI指定资源,如Index,Document等
  • HTTP Method 指定资源操作类型,如:
  1. GET:获取、查询资源
  2. POST:修改资源(某些情况下可用于删除)
  3. PUT:新增资源
  4. DELETE:删除资源
  5. ...

Index API

  • 创建:PUT/index_1
返回结果:
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "index_1"
}
  • 查看现有索引:GET _cat/indices
返回结果:
green  open .kibana  IQSzwqzNRfS98FCO92QDQw 1 0 2 0 13.8kb 13.8kb
yellow open index_1 6asShpW-RB285av-kDOACg 5 1 3 0  9.3kb  9.3kb
  • 删除:DELETE /index_1
返回结果:
{
  "acknowledged": true
}

Document API

  • 创建 PUT 索引/类型/id
    当索引和类型不存在是ES会自动根据传入的值创建,id不写时ES会自动生成id
创建:
PUT index_1/doc/1
{
  "name":"Tom",
  "age":"2"
}

返回结果:
{
  "_index": "index_1",
  "_type": "doc",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
  • 不写id创建文档(注意请求方式是POST)
创建:
POST index_1/doc
{
  "name":"Jerry",
  "age":"1"
}

返回结果:
{
  "_index": "index_1",
  "_type": "doc",
  "_id": "qO6-9WUByd1RwY2qH90K",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
  • 条件查询(以id为例)
GET index_1/doc/_search
{
  "query": {
    "term": {
      "_id": {
        "value": "1"
      }
    }
  }
}

返回结果:
{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "index_1",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "Tom",
          "age": "2"
        }
      }
    ]
  }
}
  • 查询所有
GET index_1/doc/_search

返回结果:
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "index_1",
        "_type": "doc",
        "_id": "qO6-9WUByd1RwY2qH90K",
        "_score": 1,
        "_source": {
          "name": "Jerry",
          "age": "1"
        }
      },
      {
        "_index": "index_1",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "Tom",
          "age": "2"
        }
      }
    ]
  }
}
  • 删除所有
POST index_1/doc/_delete_by_query
{
  "query": {
    "match_all": {}
  }
}

批量操作文档 API

在代码中使用字符串拼接json时,注意在每一行末尾加上\n换行符

action/metadata 行指定 哪一个文档 做 什么操作 。action 必须是以下选项之一:

  • create :
    如果文档不存在,那么就创建它;其之后一行跟请求体
  • index :
    创建一个新文档或者替换一个现有的文档;其之后一行跟请求体
  • update :
    部分更新一个文档;其之后一行跟请求体
  • delete :
    删除一个文档;其之后一行没有请求体
POST _bulk
{"index":{"_index":"index_1","_type":"doc","_id":"2"}}
{"name":"Tomson","age":"1"}
{"delete":{"_index":"index_1","_type":"doc","_id":"1"}}
{"update":{"_index":"index_1","_type":"doc","_id":"qO6-9WUByd1RwY2qH90K"}}
{"doc":{"age":"5"}}

返回结果:
{
  "took": 136,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "index_1",
        "_type": "doc",
        "_id": "2",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "delete": {
        "_index": "index_1",
        "_type": "doc",
        "_id": "1",
        "_version": 2,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 200
      }
    },
    {
      "update": {
        "_index": "index_1",
        "_type": "doc",
        "_id": "qO6-9WUByd1RwY2qH90K",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 1,
        "status": 200
      }
    }
  ]
}

文档批量查询 API

可查询不同index中的文档

GET _mget
{
  "docs":[
    {
      "_index":"index_1",
      "_type":"doc",
      "_id":"2"
    },
    {
      "_index":"index_1",
      "_type":"doc",
      "_id":"qO6-9WUByd1RwY2qH90K"
    }
    ]
}

返回结果:
{
  "docs": [
    {
      "_index": "index_1",
      "_type": "doc",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "name": "Tomson",
        "age": "1"
      }
    },
    {
      "_index": "index_1",
      "_type": "doc",
      "_id": "qO6-9WUByd1RwY2qH90K",
      "_version": 2,
      "found": true,
      "_source": {
        "name": "Jerry",
        "age": "5"
      }
    }
  ]
}

你可能感兴趣的:(Elasticsearch基础之快速入门)