生产上那些常用的ES命令(含

生产上那些常用的ES命令(含,第1张

生产上那些常用的ES命令(含

直入正题,我们从三个方面(ES集群信息、搜索文档、 *** 作文档)归类整理一下常用的ES命令。

1、ES集群信息(_cat命令) 0> 命令属性

v:显示表头

h:选择要显示的列,多个列以,分隔

format:设置返回的内容格式(json、yaml、text)

s:排序

注意:多个参数可以一起使用,用&连接

示例

GET _cat/indices?v&format=json&h=epoch,cluster&s=store.size:desc,docs.count:asc 1> 索引/集群 1)ES集群健康状态

GET _cat/health?v

示例:

http://127.0.0.1:9200/_cat/health?v
2)查看ES集群下所有索引信息

GET _cat/indices?v

示例:

http://127.0.0.1:9200/_cat/indices?v
3)查看所有索引的文档总数量

GET _cat/count?v

示例:

http://127.0.0.1:9200/_cat/count?v
4)查看集群的插件信息

GET _cat/plugins?v

示例:

http://127.0.0.1:9200/_cat/plugins?v
5)查看索引的分片信息

GET _cat/shards?v

示例:

http://127.0.0.1:9200/_cat/shards?v
6)查看索引分片中的分段信息

GET _cat/segments?v

示例:

http://127.0.0.1:9200/_cat/segments?v
2> 节点 1)查看集群机器节点信息

GET _cat/nodes?v

示例:

http://127.0.0.1:9200/_cat/nodes?v
2)查看集群中每个节点的分片数量、磁盘占用情况

GET _cat/allocation?v

示例:

http://127.0.0.1:9200/_cat/allocation?v
3)查出集群中master节点

GET _cat/master?v

示例:

http://127.0.0.1:9200/_cat/master?v
3> 其他 1)线程池信息

GET _cat/thread_pool?v

示例:

http://127.0.0.1:9200/_cat/thread_pool?v
2)正在等待的任务

GET _cat/pending_tasks?v

示例:

http://127.0.0.1:9200/_cat/pending_tasks?v

空白表示没有正在等待的任务。 3)模板信息

GET _cat/templates?v

示例:

http://127.0.0.1:9200/_cat/templates?v
2、搜索文档

我们这里主要讲结构化查询,可以参考ES中文文档:https://books.didispace.com/elasticsearch-definitive-guide-cn/054_Query_DSL/60_Query_DSL.html。

注意:下面的indexName标识索引名称,可自定义;type表示索引类型,可自定义;id表示文档的唯一标识;

1> 请求模板

GET指令 http://localhost:9200/indexName/type/_search

请求体:

{
	"query": {
		"bool": {
			"must": [{
				"term": {
					"id": "4028e581617f945f01617f9dabc40000"
				}
			}],
			"must_not": [],
			"should": []
		}
	},
	"from": 0,
	"size": 10,
	"sort": [],
	"aggs": {}
}
2> 查询结果分析
{
    "took": 37,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 2.0714705,
        "hits": [
            {
                "_index": "xc_course",
                "_type": "doc",
                "_id": "4028e581617f945f01617f9dabc40000",
                "_score": 2.0714705,
                "_source": {
                    "studymodel": "201001",
                    "users": "无开发经验",
                }
            }
        ]
    }
}

took:本次 *** 作花费的时间,单位为毫秒;timed_out:请求是否超时;_shards:说明本次 *** 作共搜索了哪些分片;hits:搜索命中的记录;hits.total : 符合条件的文档总数 hits.hits :匹配度较高的前N个文档;hits.max_score:文档匹配得分,这里为最高分;_score:每个文档都有一个匹配度得分,按照降序排列;_source:显示了文档的原始内容;

想要做分页查询、范围查询、指定获取到的字段、指定排序规则等 *** 作只需要在请求体中进行修改。

3> 几个示例 1)针对某个字段做in查询
{
  "query": {
		"bool": {
			"must": [{
				"terms": {
					"shopId": [
						38299,
						38287,
						38296,
						38238,
						32076,
						32091,
						32178,
						32075,
						34026,
						44012,
						32192,
						34027,
						44014,
						42135,
						38252,
						38288,
						38333,
						38332,
						38247,
						38170
					]
				}
			}],
			"must_not": [],
			"should": []
		}
	},
  "from": 0,
  "size": 20,
  "_source": [
    "id",
    "shopId"
  ],
  "sort": {
    "shopId": {
      "order": "asc"
    }
  }
}
2)分页查询所有的文档,只返回部分字段(id、shopId)并按shopId正序排序
{
  "query": {
		"bool": {
			"must": [{
				"match_all": {}
			}],
			"must_not": [],
			"should": []
		}
	},
  "from": 0,
  "size": 20,
  "_source": [
    "id",
    "shopId"
  ],
  "sort": {
    "shopId": {
      "order": "asc"
    }
  }
}

更多使用方式,参考Elasticsaerch权威指南:https://books.didispace.com/elasticsearch-definitive-guide-cn/。

3、 *** 作文档

注意:下面的indexName标识索引名称,可自定义;type表示索引类型,可自定义;id表示文档的唯一标识;

1> 创建索引库

Put指令 http://127.0.0.1:9200/indexName

在请求Body中添加JSON数据:

{
	"index":{
		"number_of_replicas":0,
        "number_of_shards":1
	}
}

其中的index属性如下:

number_of_shards:分片的数量,将一个索引库拆分为多片存储在不同的节点;number_of_replicas:副本的数量; 2> 创建映射

POST指令 http:127.0.0.1:9200/indexName/type/_mapping

{
	"properties":{
		"name":{
			"type":"text",
            "analyzer": "ik_max_word",
            "index": true
		},
		"description":{
			"type":"text"
		},
		"studymodel":{
			"type":"keyword"
		}
	}
}

"index": true --> 表示相应的字段可以被检索;如果为false,则相应的字段不可被检索。

3> 创建文档

PUT/POST指令 http://127.0.0.1:9200/indexName/type/id

{
	"name":"BootStrap开发框架",
	"description":"Bootstrap是由Twitter推出的一个前台页面开发框架。",
	"studymodel":"201001"
}
4> 修改文档

**POST指令 ** http://127.0.0.1:9200/indexName/type/id

然后直接在body里改数据;

{
	"name":"BootStrap开发框架",
	"description":"Bootstrap是一个前台页面开发框架。",
	"studymodel":"201001"
}
5> 删除文档/索引库

全部使用DELETE指令

删除索引库:http://127.0.0.1:9200/indexName删除文档:http://127.0.0.1:9200/indexName/type/id

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5709582.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存