常用操作命令
原创大约 6 分钟
索引相关命令
# 删除单个索引
> curl --user elastic:company2020 -XDELETE 'localhost:9200/blink?pretty'
# 删除多个索引
> curl --user elastic:company2020 -XDELETE 'localhost:9200/blink,blog?pretty'
# 创建索引
> curl --user elastic:company2020 -XPUT -H 'Content-Type: application/json' 'localhost:9200/blink?pretty' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
},
"mappings": {
"properties": {
"guid": {
"type": "long"
},
"bizline": {
"type": "keyword"
},
"resid": {
"type": "keyword"
},
"block": {
"type": "keyword"
},
"username": {
"type": "keyword"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"url": {
"type": "keyword"
},
"kind": {
"type": "integer"
},
"status": {
"type": "integer"
},
"suggestion": {
"type": "keyword"
},
"again": {
"type": "integer"
},
"senstitle": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"senstext": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"sensimage": {
"type": "keyword"
},
"createtime": {
"type": "long"
},
"updatetime": {
"type": "long"
}
}
}
}'
# 创建产品索引(不带分词)
> curl --user elastic:company2020 -XPUT 'localhost:9200/product?pretty' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}'
# 创建产品索引(带分词)
> curl --user elastic:company2020 -XPUT 'localhost:9200/twitter?pretty' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
},
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "pattern",
"pattern": "\\,"
}
}
}
}
}'
# 创建产品mapping(数据表)
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/_mapping/product_plan?pretty' -d'
{
"properties": {
"id" : { "type": "keyword" },
"name" : { "type": "text","analyzer": "ik_max_word" },
"desc" : { "type": "text","analyzer": "ik_max_word", "store": false },
"price" : { "type": "float" },
"productid" : { "type": "keyword" },
"productname" : { "type": "text","analyzer": "ik_max_word" },
"categorys" : {
"type" : "nested",
"properties" : {
"id" : { "type": "keyword" },
"name" : { "type": "text" , "analyzer": "ik_max_word" },
"parentid" : { "type": "keyword" }
}
}
}
}'
# 给索引赋值一个文档(记录值)
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/product_plan/1?pretty' -d'
{
"id" : "1",
"name" : "儿童保险计划",
"desc" : "保险产品计划",
"price" : 200,
"productid" : "1",
"productname" : "少儿益佳保险",
"categorys" : [
{
"id" : "1",
"name" : "儿童保险",
"parentid" : "3"
}
]
}'
# 查看单个索引的创建结果
> curl --user elastic:company2020 -XGET 'http://localhost:9200/blink/_mapping?pretty'
# 查看多个索引的创建结果
> curl --user elastic:company2020 -XGET 'http://localhost:9200/blink,blog/_mapping?pretty'
# 查看全部索引
> curl --user elastic:company2020 -XGET 'http://localhost:9200/_cat/indices?v&pretty'
# 获取索引
> curl --user elastic:company2020 -XGET 'localhost:9200/product/_mapping/product_plan?pretty'
# 删除索引
> curl --user elastic:company2020 -XDELETE 'localhost:9200/product?pretty'
集群相关命令
# 查看集群健康状况
> curl --user elastic:company2020 -XGET 'http://localhost:9200/_cluster/health?pretty'
> curl --user elastic:company2020 -XGET 'http://localhost:9200/_cluster/health?level=indices&pretty'
# 查看插件信息
> curl --user elastic:company2020 http://localhost:9200/_cat/plugins?pretty
# 查看本机信息
> curl --user elastic:company2020 http://localhost:9200?pretty
# 查看本机健康状况
> curl --user elastic:company2020 http://localhost:9200/_cat/health?pretty
查询相关命令
# 关键词索引
> curl --user elastic:company2020 -XGET 'localhost:9200/product/product_plan/_search?pretty' -d'
{
"query": {
"multi_match" : {
"query" : "this is a test",
"fields" : [ "subject^3", "message" ]
}
}
}'
# 正则表达式:使用逗号","分割
> curl --user elastic:company2020 -XPUT 'localhost:9200/my_index?pretty' -d'
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "pattern",
"pattern": "\\,"
}
}
}
}
}'
# 在查询中加入正则表达式
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/_mapping/product_plan?pretty' -d'
{
"properties": {
"id" : { "type": "keyword" },
"name" : { "type": "text","analyzer": "ik_max_word" },
"desc" : { "type": "text","analyzer": "ik_max_word", "store": false },
"areas" : { "type": "text","analyzer": "my_analyzer", "search_analyzer": "my_analyzer", "store": true },
"price" : { "type": "float" },
"productid" : { "type": "keyword" },
"productname" : { "type": "text","analyzer": "ik_max_word" },
"categorys" : {
"type" : "nested",
"properties" : {
"id" : { "type": "keyword" },
"name" : { "type": "text" , "analyzer": "ik_max_word" },
"parentid" : { "type": "keyword" }
}
}
}
}'
> curl --user elastic:company2020 -XPOST 'localhost:9200/twitter/_analyze?pretty' -d'
{
"analyzer": "my_analyzer",
"text": "000000,000010,000200"
}'
> curl --user elastic:company2020 -XGET 'localhost:9200/product/product_plan/_search?pretty' -d'
{
"query": {
"bool": {
"must": [
{ "match" : { "areaIds" : "990000" }}
]
}
}
}'
# 同义词
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/product_plan/1?pretty' -d'
{
"id" : "1",
"name" : "儿童旅游计划",
"desc" : "旅游产品计划",
"price" : 200,
"productid" : "1",
"productname" : "少儿益佳旅游",
"categorys" : [
{
"id" : "1",
"name" : "儿童旅游",
"parentid" : "3"
}
]
}'
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/product_plan/1?pretty' -d'
{
"id" : "1",
"name" : "儿童旅行计划",
"desc" : "旅行产品计划",
"price" : 200,
"productid" : "1",
"productname" : "少儿益佳旅行",
"categorys" : [
{
"id" : "1",
"name" : "儿童旅行",
"parentid" : "3"
}
]
}'
> curl --user elastic:company2020 -XPUT 'localhost:9200/product?pretty' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}'
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/_mapping/product_plan?pretty' -d'
{
"properties": {
"name" : { "type": "completion","analyzer": "ik_max_word", "search_analyzer": "ik_max_word" },
"tags" : { "type": "text","analyzer": "ik_max_word", "store": false }
}
}'
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/product_plan/1?pretty' -d'
{
"name" : [
{
"input": "Nevermind",
"weight" : 10
},
{
"input": "Nirvana",
"weight" : 3
}
],
"tags" : "个人,团队,员工,技术"
}'
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/product_plan/2?pretty' -d'
{
"name" : "旅游险",
"tags" : "综合,交通,公司,企业,职工,户外,风景"
}'
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/product_plan/3?pretty' -d'
{
"name" : "儿童险",
"tags" : "少儿,意外,保险,保障"
}'
# 搜索
> curl --user elastic:company2020 -XPOST 'localhost:9200/product/_search?pretty' -d'
{
"suggest": {
"product_plan" : {
"prefix" : "nir",
"completion" : {
"field" : "name"
}
}
}
}'
# curl --user elastic:company2020 -XPOST 'localhost:9200/product/_search?pretty' -d'
{
"suggest": {
"product_plan" : {
"prefix" : "旅",
"completion" : {
"field" : "name"
}
}
}
}'
# 模糊查询
> curl --user elastic:company2020 -XPOST 'localhost:9200/product/_search?pretty' -d'
{
"suggest": {
"product_plan" : {
"prefix" : "游",
"completion" : {
"field" : "name",
"fuzzy" : {
"fuzziness" : 2
}
}
}
}
}'
# 正则查询
> curl --user elastic:company2020 -XPOST 'localhost:9200/product/_search?pretty' -d'
{
"suggest": {
"product_plan" : {
"regex" : "n[ever|i]r",
"completion" : {
"field" : "name"
}
}
}
}'
# IN查询
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/product_plan/1?pretty' -d'
{
"id" : "1",
"name" : "儿童保险计划",
"desc" : "保险产品计划",
"price" : 200,
"productid" : "1",
"productname" : "少儿益佳保险",
"categorys" : [
{
"id" : "1",
"name" : "儿童保险",
"parentid" : "3"
}
]
}'
> curl --user elastic:company2020 -XPUT 'localhost:9200/product/product_plan/3?pretty' -d'
{
"id" : "3",
"name" : "儿童保险计划",
"desc" : "保险产品计划",
"price" : 200,
"productid" : "1",
"productname" : "少儿益佳保险",
"categorys" : [
{
"id" : "1",
"name" : "儿童保险",
"parentid" : "3"
}
]
}'
> curl --user elastic:company2020 -XGET 'localhost:9200/product/product_plan/_search?pretty' -d'
{
"size" : 10,
"query": {"terms": {"id" : ["1", "2"]}}
}'
# Sort排序
> curl --user elastic:company2020 -XGET 'localhost:9200/product/product_plan/_search?pretty' -d'
{
"size" : 10,
"query": {"terms": {"id" : ["1", "3"]}},
"sort" : [
{ "id" : {"order" : "desc"}}
]
}'
# 结果高亮显示
> curl --user elastic:company2020 -XGET 'localhost:9200/product/product_plan/_search?pretty' -d'
{
"size" : 10,
"query": {"terms": {"id" : ["1", "3"]}},
"sort" : [
{ "id" : {"order" : "desc"}}
],
"highlight" : {
"fields" : {
"id" : {}
}
}
}'
> curl --user elastic:company2020 -XGET 'localhost:9200/product/product_plan/_search?pretty' -d'
{
"size" : 10,
"query": {"match": {"name" : "儿童"}},
"highlight" : {
"pre_tags" : ["<tag1>"],
"post_tags" : ["</tag1>"],
"fields" : {
"name" : {}
}
}
}'
> curl --user elastic:company2020 -XGET 'localhost:9200/product/product_plan/_search?pretty' -d'
{
"size" : 10,
"query": {"match": {"name" : "儿童"}},
"highlight" : {
"fields" : {
"name" : {"type" : "plain/postings/fvh"}
}
}
}'
# 查询文档数量
> curl --user elastic:company2020 -XGET localhost:9200/_cat/indices/blink?v
# 在单个索引中查询数据
> curl --user elastic:company2020 -XGET -H 'Content-Type: application/json' 'localhost:9200/blink/_search?pretty' -d'
{
"query": {
"match" : {"type":1}
}
}'
# 在多个索引中查询数据
> curl --user elastic:company2020 -XGET -H 'Content-Type: application/json' 'localhost:9200/blink,blink_comment/_search?pretty' -d'
{
"from":0,
"size":100,
"query": {
"match_all" : {}
}
}'
# 在多个字段中查询数据
> curl --user elastic:company2020 -XGET -H 'Content-Type: application/json' 'localhost:9200/*/_search?pretty' -d'
{
"query": {
"multi_match": {
"query": "你民工",
"fields": [
"address",
"name"
]
}
}
}'
# 在多个索引中查询数据(方式一:使用,分隔多个索引名称)
> curl --user elastic:company2020 -XGET -H 'Content-Type: application/json' 'localhost:9200/blink/_search?pretty' -d'
{
"from":0,
"size":100,
"query": {
"match" : {
"content" : "用户"
}
},
"highlight": {
"fields": {
"content": {}
}
}
}'
# 在多个索引中查询数据(方式二:使用_all)
> curl --user elastic:company2020 -XGET -H 'Content-Type: application/json' 'localhost:9200/_all/_search?pretty' -d'
{
"from":0,
"size":100,
"query": {
"match" : {"content":"haha"}
}
}'
# 在多个索引中查询数据(方式三:使用*代替所有的)
> curl --user elastic:company2020 -XGET -H 'Content-Type: application/json' 'localhost:9200/*/_search?pretty' -d'
{
"from":0,
"size":100,
"query": {
"match" : {"content":"haha"}
}
}'
# 在多个索引中查询数据(方式四:将/index中的内容空出来)
> curl --user elastic:company2020 -XGET -H 'Content-Type: application/json' 'localhost:9200/_search?pretty' -d'
{
"from":0,
"size":100,
"query": {
"match" : {"content":"haha"}
}
}'
# 准备深分页查询
> curl --user elastic:company2020 -XGET "localhost:9200/blink/_search?scroll=5m&pretty" -H 'Content-Type: application/json' -d'
{
"size": 50,
"query": {
"bool": {
"must": [{
"match": {
"content": {
"query": "blink",
"operator": "OR",
"prefix_length": 0,
"max_expansions": 50,
"fuzzy_transpositions": true,
"lenient": false,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true
}
}
},
{
"range": {
"createtime": {
"from": 1592206680000,
"to": null,
"include_lower": true,
"include_upper": true
}
}
},
{
"range": {
"createtime": {
"from": null,
"to": 1592293080000,
"include_lower": true,
"include_upper": true
}
}
}]
}
},
"sort": [{
"createtime": {
"order": "asc"
}
}]
}'
# 通过深分页继续查询
> curl --user elastic:company2020 -XGET "localhost:9200/_search/scroll?pretty" -H 'Content-Type: application/json' -d'
{
"scroll" : "1m",
"scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAABN0WdXNvd3QyZUxUQUtJYUpQN213RllPdw=="
}'
# 清空type下所有doc
> curl --user elastic:company2020 -XPOST -H 'Content-Type: application/json' 'localhost:9200/blink/_delete_by_query?conflicts=proceed&pretty' -d'
{
"query": {
"match_all": {}
}
}'
分词器相关命令
# 分词
> curl --user elastic:company2020 -XGET -H 'Content-Type: application/json' 'http://localhost:9200/_analyze?pretty' -d'{"text":"中华人民共和国"}';
# 创建IK分词器
> curl --user elastic:company2020 -XPOST -H 'Content-Type: application/json' http://localhost:9200/blink/fulltext/_mapping?pretty -d'
{
"fulltext": {
"_all": {
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"term_vector": "no",
"store": "false"
},
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 8
}
}
}
}'
> curl --user elastic:company2020 -XPUT -H 'Content-Type: application/json' http://localhost:9200/blink/_doc/1?pretty -d'
{
"guid" : 1,
"content" : "哈哈,今天天气真好"
}'
> curl --user elastic:company2020 -XPUT -H 'Content-Type: application/json' http://localhost:9200/blink/_doc/2?pretty -d'
{
"guid" : 2,
"content" : "经过90天的奋战,中国取得了抗疫成功"
}'
> curl --user elastic:company2020 -XPUT -H 'Content-Type: application/json' http://localhost:9200/blog/_doc/1?pretty -d'
{
"guid" : 1,
"content" : "天空之城"
}'
聚合相关命令
precision
精度范围(1~12)。
1 5009.4km ~ 4992.6km
2 1252.3km ~ 624.1km
3 156.5km ~ 156km
4 39.1km ~ 19.5km
5 4.9km ~ 4.9km
6 1.2km ~ 609.4m
7 152.9m ~ 152.4m
8 38.2m ~ 19m
9 4.8m ~ 4.8m
10 1.2m ~ 59.5cm
11 14.9cm ~ 14.9cm
12 3.7cm ~ 1.9m
精度不能大于定义时给出的值,如果定义为10
,那么搜索时最大只能为10
。
聚合分组的结果取决于搜索时precision
给出的值。
定义
10
,搜索精度5
时的聚合结果:"aggregations" : { "areacode" : { "buckets" : [ { "key" : "wt3m7", "doc_count" : 3 }, { "key" : "wt3mh", "doc_count" : 1 } ] } }
定义
10
,搜索精度6
时的聚合结果:"aggregations" : { "areacode" : { "buckets" : [ { "key" : "wt3mhk", "doc_count" : 1 }, { "key" : "wt3m7t", "doc_count" : 1 }, { "key" : "wt3m7s", "doc_count" : 1 }, { "key" : "wt3m75", "doc_count" : 1 } ] } }
因此可以依据
precision
来调整热力图的显示数据。
感谢支持
更多内容,请移步《超级个体》。