elasticsearch常用curl命令

引言

以下是记录学习elasticsearch中的基本知识;
主要是几个基本概念和一些简单的基本操作

基本概念

  1. Index:Elastic 数据管理的顶层单位就叫做 Index(索引)每个 Index (即数据库)的名字必须是小写。
  2. Document:Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。
    Document 使用 JSON 格式表示
  3. type:将document进行分组,这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document。(具体理解可能有偏差欢迎留言纠正)
    根据规划,Elastic 6.x 版只允许每个 Index 包含一个 Type,7.x 版将会彻底移除 Type。

常用操作命令

新建index

请求方式 PUT,其中index_01表示数据库名称(数据库名称必须为小写)

192.168.1.145:9200/index_01

返回结构:acknowledged:true表示操作成功

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "index_01"
}

删除index

请求方式 DELETE (与新建index的区别是请求方式使用了DELETE)

192.168.1.145:9200/index_01

返回结构:

{
    "acknowledged": true
}

新增记录

请求方式: PUT
index_01: 为index(相当于关系型数据库中的数据库)
type_01:表示数据放在type_01类型下
1: 表示指定ID为1(可以为任意字符)

192.168.1.145:9200/index_01/type_01/1
参数:{
	"name":"张三",
	"sex":"男",
	"age":18
}

返回结构:

{
    "_index": "index_01", 
    "_type": "type_01",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

表示添加ID为1的数据创建成功;如果不想在添加的时候指定ID只需要把请求方式给成PUT,然后将路径下最后的1去除即可;

POST  192.168.1.145:9200/index_01/type_01
请求参数:
{
	"name":"李四",
	"sex":"女",
	"age":20
}

返回参数:

{
    "_index": "index_01",
    "_type": "type_01",
    "_id": "AWu2mg3fQX3MA7vSN8IN",   //表示elasticsearch自动生成的ID
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

请求记录

请求方式GET pretty=true表示以易读的格式化返回

192.168.1.145:9200/index_01/type_01/1?pretty=true

返回参数:

{
    "_index": "index_01",
    "_type": "type_01",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "name": "张三",
        "sex": "男",
        "age": 18
    }
}

如果Id不正确,查询不到数据时,found字段就是false

删除记录

请求方式DELETE

192.168.1.145:9200/index_01/type_01/1

返回参数:

{
    "found": true,
    "_index": "index_01",
    "_type": "type_01",
    "_id": "1",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}

表示删除成功

更新记录

请求方式PUT
和添加的时候请求方式一样(elasticsearch会检测有想相同ID自动更新)

192.168.1.145:9200/index_01/type_01/1
请求参数:
{
	"name":"张三01",
	"sex":"男",
	"age":19
}

返回参数:

{
    "_index": "index_01",
    "_type": "type_01",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": false
}

可以看到与添加时返回参数的区别 “created”: false, “result”: “updated”,"_version": 2,每次进行修改对应的版本都会加1

查询所有数据

请求方式GET
直接请求/Index/Type/_search,就会返回所有记录

192.168.1.145:9200/index_01/type_01/_search

返回结果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "index_01",
                "_type": "type_01",
                "_id": "AWu2mg3fQX3MA7vSN8IN",
                "_score": 1,
                "_source": {
                    "name": "李四",
                    "sex": "女",
                    "age": 20
                }
            },
            {
                "_index": "index_01",
                "_type": "type_01",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "name": "张三01",
                    "sex": "男",
                    "age": 19
                }
            }
        ]
    }
}

其中
took:表示查询消耗的时间(毫秒)
timed_out:是否超时
hits:表示命中的记录 ,里面的字段 total:表示返回的记录数,max_score:最高匹配程度,hits:返回查询出来的数组

条件查找

请求方式POST

192.168.1.145:9200/index_01/type_01/_search
请求参数:
{
  "query": {
    "match": {
      "name": "李四"
    }
  }
}

使用match查询方法,查找指定条件 name 字段为“李四”的所有数据
返回参数:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.51623213,
        "hits": [
            {
                "_index": "index_01",
                "_type": "type_01",
                "_id": "AWu2mg3fQX3MA7vSN8IN",
                "_score": 0.51623213,
                "_source": {
                    "name": "李四",
                    "sex": "女",
                    "age": 20
                }
            }
        ]
    }
}

elasticsearch默认一次返回10条记录,可以通过size字段改变这个设置

{
  "query": {
    "match": {
      "name": "李四"
    }
  },
  "size":1   //只返回一条数据
}

还可以通过from字段(默认从位置0开始查询),指定开始查询位置
从第一条开始查询,查询一条数据(相当于mysql中limit(from,size))

{
  "query": {
    "match": {
      "name": "李四"
    }
  },
  "size":1,
  "from":1
}

逻辑运算

请求方式POST
从位置0开始查询查询5条匹配的数据 name中包含 “李四”或者“张三”的数据

192.168.1.145:9200/index_01/type_01/_search
请求参数:
{
  "query": {
    "match": {
      "name": "李四 张三"
    }
  },
  "size":5,
  "from":0
}

返回结构

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.51623213,
        "hits": [
            {
                "_index": "index_01",
                "_type": "type_01",
                "_id": "AWu2mg3fQX3MA7vSN8IN",
                "_score": 0.51623213,
                "_source": {
                    "name": "李四",
                    "sex": "女",
                    "age": 20
                }
            },
            {
                "_index": "index_01",
                "_type": "type_01",
                "_id": "1",
                "_score": 0.5063205,
                "_source": {
                    "name": "张三01",
                    "sex": "男",
                    "age": 19
                }
            }
        ]
    }
}

这种查询方式相当于mysql中的 or查询
如果需要and条件查询 需要使用bool查询方式

192.168.1.145:9200/index_01/type_01/_search
请求参数:
{
  "query": {
  	"bool":{
  		"must":[
	  		 {"match": { "name": "李" }},
			 {"match": { "name": "四"}}
  		]
  	}
  },
  "size":5,
  "from":0
}

返回结果:

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.51623213,
        "hits": [
            {
                "_index": "index_01",
                "_type": "type_01",
                "_id": "AWu2mg3fQX3MA7vSN8IN",
                "_score": 0.51623213,
                "_source": {
                    "name": "李四",
                    "sex": "女",
                    "age": 20
                }
            }
        ]
    }
}

至上是elasticsearch基本的操作功能;

你可能感兴趣的:(elasticsearch)