ElasticSearch命令

发布时间:2018-01-17 17:06:29   来源:文档文库   
字号:

ElasticSearch命令

1.查询命令

匹配查询

curl -XGET 192.168.244.1:9200/my_index8/my_type8/_search -d '

{

"query": {

"wildcard": {

"name.keyword": "*?"

}

}

}

'

分析器查询

curl -XGET 192.168.244.1:9200/my_index8/_analyze -d '

{

"field": "desc",

"text": "java工程师,从事大数据软件开发,对nosql数据库有研究"

}

'

curl -XGET 192.168.244.1:9200/my_index8/_analyze -d '

{

"field": "name",

"text": "张三丰"

}

'

curl -XGET 192.168.244.128:9200/_analyze?analyzer=ik_max_word -d '{"text":"张三丰"}'

http://192.168.244.128:9200/_analyze?analyzer=ik_max_word&text=张三丰

集群:查询内存是否被锁定

curl -XGET 192.168.244.128:9200/_nodes?filter_path=**.mlockall

聚集查询

curl -XGET 192.168.244.1:9200/cars/transactions/_search -d '

{

"size" : 0,

"aggs" : {

"popular_colors" : {

"terms" : {

"field" : "color.keyword"

}

}

}

}

'

curl -XGET 192.168.244.1:9200/cars/transactions/_search -d '

{

"size" : 0,

"aggs": {

"colors": {

"terms": {

"field": "color.keyword"

},

"aggs": {

"avg_price": {

"avg": {

"field": "price"

}

}

},

"make": {

"terms": {

"field": "make"

}

}

}

}

}

'

curl -XGET 192.168.244.1:9200/cars/transactions/_search -d '

{

"size" : 0,

"aggs": {

"colors": {

"terms": {

"field": "color"

},

"aggs": {

"avg_price": {

"avg": {

"field": "price"

}

},

"make": {

"terms": {

"field": "make"

}

}

}

}

}

}

'

2.修改命令

别名_alias

curl -XPUT 192.168.244.130:9200/my_index8/_alias/index_alias

curl -XGET 192.168.244.128:9200/my_index8/_alias/*

curl -XGET 192.168.244.128:9200/*/_alias/index_alias

无缝删除添加别名,原子操作

curl -XPOST 192.168.244.130:9200/_aliases -d '

{

"actions": [

{ "remove": { "index": "my_index8", "alias": "index_alias" }},

{ "add": { "index": "my_index3", "alias": "index_alias" }}

]

}

'

3.删除命令

curl -XDELETE 192.168.244.1:9200/my_index/my_type/AV3E0qBK9W2J9WuRt8D3

curl -XPOST 192.168.244.1:9200/_bulk -d '

{ "delete": { "_index": "my_index", "_type": "my_type", "_id": "AV3E0qBK9W2J9WuRt8D4" }}

{ "delete": { "_index": "my_index", "_type": "my_type", "_id": "AV3E0qBK9W2J9WuRt8D5" }}

{ "delete": { "_index": "my_index", "_type": "my_type", "_id": "AV3E0qBK9W2J9WuRt8D6" }}

{ "delete": { "_index": "my_index", "_type": "my_type", "_id": "AV3E0qBK9W2J9WuRt8D7" }}

{ "delete": { "_index": "my_index", "_type": "my_type", "_id": "AV3BQz3xJ5KYRov5JoNA" }}

'

删除索引

curl -XDELETE 192.168.244.1:9200/book

4.新增命令

新增一条记录

curl -XPOST 192.168.244.1:9200/my_index/my_type -d '

{

"id": 1,

"name": "张小明",

"desc": "java工程师,从事大数据软件开发",

"age": 23

}

'

新增多条记录,注意后面空一行

curl -XPOST 192.168.244.1:9200/_bulk -d '

{ "index": { "_index": "my_index", "_type": "my_type" }}

{"id":5,"name":"张三丰","desc":"java工程师,从事大数据软件开发,对nosql数据库有研究","age":123}

{ "index": { "_index": "my_index", "_type": "my_type" }}

{"id":2,"name":"jack","desc":"php工程师,从事网站开发,对java也有一定了解","age":30}

{ "index": { "_index": "my_index", "_type": "my_type" }}

{"id":3,"name":"李晓红","desc":"前端工程师,对reactangularjs有研究","age":22}

{ "index": { "_index": "my_index", "_type": "my_type" }}

{"id":4,"name":"王晓燕","desc":"UI设计师,目前处于实现阶段","age":19}

{ "index": { "_index": "my_index", "_type": "my_type" }}

{"id":6,"name":"jannes","desc":"数据库工程师,对oraclemysql有一定的经验","age":35}

'

重新索引_reindex

curl -XPOST 192.168.244.130:9200/_reindex -d '

{

"source": {

"index": "my_alias"

},

"dest": {

"index": "my_index"

}

}

'

//新增或覆盖

curl -XPOST 192.168.244.130:9200/_reindex -d '

{

"source": {

"index": "my_alias8"

},

"dest": {

"index": "my_alias",

"version_type": "internal"

}

}

'

//version版本有关

curl -XPOST 192.168.244.130:9200/_reindex -d '

{

"source": {

"index": "my_alias8"

},

"dest": {

"index": "my_alias",

"version_type": "external"

}

}

'

//有选择地进行索引

curl -XPOST 192.168.244.130:9200/_reindex -d '

{

"source": {

"index": "my_alias8",

"type": "my_type8",

"query": {

"constant_score": {

"filter": {

"range": {

"age": {

"gte": 20,

"lt": 40

}

}

}

}

}

},

"dest": {

"index": "my_alias"

}

}

'

//带有indextype

curl -XPOST 192.168.244.130:9200/_reindex -d '

{

"source": {

"index": ["my_alias8"],

"type": ["my_type8"]

},

"dest": {

"index": "my_alias"

}

}

'

//排序和size记录数

curl -XPOST 192.168.244.130:9200/_reindex -d '

{

"size": 3,

"source": {

"index": "my_alias8",

"sort": { "age": "desc" }

},

"dest": {

"index": "my_alias"

}

}

'

//指定字段索引

curl -XPOST 192.168.244.130:9200/_reindex -d '

{

"source": {

"index": "my_alias8",

"_source": ["id", "name", "desc"]

},

"dest": {

"index": "my_alias"

}

}

'

5.修改mapping

修改mapping

curl -XPUT 192.168.244.1:9200/my_index/_mapping/my_type -d '

{

"properties": {

"name": {

"analyzer": "ik_smart",

"type": "text",

"fields": {

"keyword": {

"ignore_above": 256,

"type": "keyword"

}

}

},

"id": {

"type": "long"

},

"age": {

"type": "integer"

},

"desc": {

"analyzer": "ik_smart",

"type": "text",

"fields": {

"keyword": {

"ignore_above": 256,

"type": "keyword"

}

}

}

}

}

'

6. cat APIs

查询各索引信息

curl 192.168.244.128:9200/_cat/indices?format=json&pretty

查询可以分片的磁盘空间

curl 192.168.244.128:9200/_cat/allocation?v

7. Cluster APIs

节点相关信息

curl -XGET 192.168.244.128:9200/_nodes/_local

curl -XGET 192.168.244.128:9200/_nodes/192.168.244.128,192.168.244.130

curl -XGET 192.168.244.128:9200/_nodes/node-128

集群健康信息

curl -XGET 192.168.244.128:9200/_cluster/health

curl -XGET 192.168.244.128:9200/_cluster/health/my_index,my_index8

8.聚合查询aggs

curl -XGET 192.168.244.128:9200/my_index2/my_type2/_search?size=0 -d '

{

"aggs": {

"popular_colors": {

"terms": {

"field": "color.keyword"

}

}

}

}

'

//平均值

curl -XGET 192.168.244.128:9200/my_index2/my_type2/_search?size=0 -d '

{

"aggs": {

"price_avg": {

"avg": {

"field": "price"

}

}

}

}

'

//使用plainless脚本

curl -XGET 192.168.244.129:9200/my_index2/my_type2/_search?size=0 -d '

{

"aggs" : {

"avg_grade" : {

"avg" : {

"script" : {

"inline" : "doc.price.value"

}

}

}

}

}

'

curl -XGET 192.168.244.129:9200/my_index2/my_type2/_search?size=0 -d '

{

"aggs" : {

"avg_corrected_grade" : {

"avg" : {

"field" : "price",

"script" : {

"lang": "painless",

"inline": "_value * params.correction",

"params" : {

"correction" : 1.2

}

}

}

}

}

}

'

//嵌套

curl -XGET 192.168.244.128:9200/my_index2/my_type2/_search -d '

{

"size" : 0,

"aggs": {

"colors": {

"terms": {

"field": "color.keyword"

},

"aggs": {

"avg_price": {

"avg": {

"field": "price"

}

}

}

}

}

}

'

//最大值和最小值

curl -XGET 192.168.244.128:9200/my_index2/my_type2/_search -d '

{

"size" : 0,

"aggs": {

"colors": {

"terms": {

"field": "color.keyword"

},

"aggs": {

"avg_price": { "avg": { "field": "price" }

},

"maker" : {

"terms" : {

"field" : "make.keyword"

},

"aggs" : {

"min_price" : { "min": { "field": "price"} },

"max_price" : { "max": { "field": "price"} }

}

}

}

}

}

}

'

条形图

//histogram 桶要求两个参数:一个数值字段以及一个定义桶大小间隔

//sum 度量嵌套在每个售价区间内,用来显示每个区间内的总收入

curl -XGET 192.168.244.128:9200/my_index2/my_type2/_search -d '

{

"size" : 0,

"aggs":{

"price":{

"histogram":{

"field": "price",

"interval": 20000

},

"aggs":{

"revenue": {

"sum": {

"field" : "price"

}

}

}

}

}

}

'

//以最受欢迎 10 种汽车以及它们的平均售价、标准差这些信息创建一个条形图

curl -XGET 192.168.244.128:9200/my_index2/my_type2/_search -d '

{

"size" : 0,

"aggs": {

"makes": {

"terms": {

"field": "make.keyword",

"size": 10

},

"aggs": {

"stats": {

"extended_stats": {

"field": "price"

}

}

}

}

}

}

'

时间条形图

curl -XGET 192.168.244.1:9200/cars/transactions/_search -d '

{

"size" : 0,

"aggs": {

"sales": {

"date_histogram": {

"field": "sold",

"interval": "month",

"format": "yyyy-MM-dd"

}

}

}

}

'

// min_doc_count这个参数强制返回空 buckets

// extended_bounds这个参数强制返回整年

curl -XGET 192.168.244.128:9200/my_index2/my_type2/_search -d '

{

"size" : 0,

"aggs": {

"sales": {

"date_histogram": {

"field": "sold",

"interval": "month",

"format": "yyyy-MM-dd",

"min_doc_count" : 0,

"extended_bounds" : {

"min" : "2014-01-01",

"max" : "2014-12-31"

}

}

}

}

}

'

附录1-目录树

查询目录树下文件

curl -XPUT 192.168.244.129:9200/fs -d '

{

"settings": {

"analysis": {

"analyzer": {

"paths": {

"tokenizer": "path_hierarchy"

}

}

}

}

}

'

curl -XPUT 192.168.244.129:9200/fs/file/_mapping -d '

{

"properties": {

"name": {

"type": "keyword",

"ignore_above": 256

},

"path": {

"type": "keyword",

"ignore_above": 256,

"fields": {

"tree": {

"type": "text",

"analyzer": "paths"

}

}

}

}

}

'

curl -XPUT 192.168.244.129:9200/fs -d '

{

"settings": {

"analysis": {

"analyzer": {

"paths": {

"tokenizer": "path_hierarchy"

}

}

}

},

"aliases": {

"fs_alias": {}

},

"mappings": {

"file": {

"properties": {

"name": {

"type": "keyword",

"ignore_above": 256

},

"path": {

"type": "keyword",

"ignore_above": 256,

"fields": {

"tree": {

"type": "text",

"analyzer": "paths"

}

}

}

}

}

}

}

'

curl -XPUT 192.168.244.129:9200/fs/file/1 -d '

{

"name": "my-application.log",

"path": "/home/hous/develop/elasticsearch-data/logs",

"contents": "Starting a new Elasticsearch project is easy..."

}

'

curl -XGET 192.168.244.128:9200/fs/file/_search -d '

{

"query": {

"bool": {

"must": [

{ "match": { "contents": "Elasticsearch" }}

],

"filter": [

{ "term": { "path": "/home/hous" }}

]

}

}

}

'

附录2-嵌套查询

嵌套查询(nested数据类型)

curl -XPUT 192.168.244.129:9200/my_index5 -d '

{

"mappings": {

"blogpost": {

"properties": {

"title": {

"type": "text"

},

"body": {

"type": "text"

},

"tags": {

"type": "text"

},

"comments": {

"type": "nested",

"properties": {

"name": {

"type": "text"

},

"comment": {

"type": "text"

},

"age": {

"type": "short"

},

"stars": {

"type": "short"

},

"date": {

"type": "date"

}

}

}

}

}

}

}

'

curl -XPUT 192.168.244.129:9200/my_index5/blogpost/1 -d '

{

"title": "Nest eggs",

"body": "Making your money work...",

"tags": [ "cash", "shares" ],

"comments": [

{

"name": "John Smith",

"comment": "Great article",

"age": 28,

"stars": 4,

"date": "2014-09-01"

},

{

"name": "Alice White",

"comment": "More like this please",

"age": 31,

"stars": 5,

"date": "2014-10-22"

}

]

}

'

curl -XPUT 192.168.244.129:9200/my_index5/blogpost/2 -d '

{

"title": "Investment secrets",

"body": "What they dont tell you ...",

"tags": [

"shares",

"equities"

],

"comments": [

{

"name": "Mary Brown",

"comment": "Lies, lies, lies",

"age": 42,

"stars": 1,

"date": "2014-10-18"

},

{

"name": "John Smith",

"comment": "Youre making it up!",

"age": 28,

"stars": 2,

"date": "2014-10-16"

}

]

}

'

curl -XGET 192.168.244.129:9200/my_index5/blogpost/_search -d '

{

"query": {

"bool": {

"must": [

{

"match": {

"title": "eggs"

}

},

{

"nested": {

"path": "comments",

"query": {

"bool": {

"must": [

{

"match": {

"comments.name": "john"

}

},

{

"match": {

"comments.age": 28

}

}

]

}

}

}

}

]

}

}

}

'

//排序

curl -XGET 192.168.244.129:9200/my_index5/blogpost/_search -d '

{

"query": {

"nested": {

"path": "comments",

"query": {

"bool": {

"filter": {

"range": {

"comments.date": {

"gte": "2014-10-01",

"lt": "2014-11-01"

}

}

}

}

}

}

},

"sort": {

"comments.stars": {

"order": "asc",

"mode": "min",

"nested_path": "comments",

"nested_filter": {

"range": {

"comments.date": {

"gte": "2014-10-01",

"lt": "2014-11-01"

}

}

}

}

}

}

'

//聚合

curl -XGET 192.168.244.129:9200/my_index5/blogpost/_search -d '

{

"size" : 0,

"aggs": {

"comments": {

"nested": {

"path": "comments"

},

"aggs": {

"by_month": {

"date_histogram": {

"field": "comments.date",

"interval": "month",

"format": "yyyy-MM"

},

"aggs": {

"avg_stars": {

"avg": {

"field": "comments.stars"

}

}

}

}

}

}

}

}

'

//反向聚合-根目录聚合

curl -XGET 192.168.244.129:9200/my_index5/blogpost/_search -d '

{

"size" : 0,

"aggs": {

"comments": {

"nested": {

"path": "comments"

},

"aggs": {

"age_group": {

"histogram": {

"field": "comments.age",

"interval": 10

},

"aggs": {

"blogposts": {

"reverse_nested": {},

"aggs": {

"tags": {

"terms": {

"field": "tags"

}

}

}

}

}

}

}

}

}

}

'

附录3-父子文档

curl -XPUT 192.168.244.129:9200/company -d '

{

"mappings": {

"branch": {},

"employee": {

"_parent": {

"type": "branch"

}

}

},

"aliases": {

"company_alias": {}

}

}

'

curl -XPOST 192.168.244.129:9200/company/branch/_bulk -d '

{ "index": { "_id": "london" }}

{ "name": "London Westminster", "city": "London", "country": "UK" }

{ "index": { "_id": "liverpool" }}

{ "name": "Liverpool Central", "city": "Liverpool", "country": "UK" }

{ "index": { "_id": "paris" }}

{ "name": "Champs Élysées", "city": "Paris", "country": "France" }

'

//创建子文档时,用户必须要通过 parent 参数来指定该子文档的父文档 ID

curl -XPUT 192.168.244.129:9200/company/employee/1?parent=london -d '

{

"name": "Alice Smith",

"dob": "1970-10-24",

"hobby": "hiking"

}

'

curl -XPOST 192.168.244.129:9200/company/employee/_bulk -d '

{ "index": { "_id": 2, "parent": "london" }}

{ "name": "Mark Thomas", "dob": "1982-05-16", "hobby": "diving" }

{ "index": { "_id": 3, "parent": "liverpool" }}

{ "name": "Barry Smith", "dob": "1979-04-01", "hobby": "hiking" }

{ "index": { "_id": 4, "parent": "paris" }}

{ "name": "Adrien Grand", "dob": "1987-05-11", "hobby": "horses" }

'

//通过子文档查询父文档

curl -XGET 192.168.244.129:9200/company/branch/_search -d '

{

"query": {

"has_child": {

"type": "employee",

"query": {

"range": {

"dob": {

"gte": "1980-01-01"

}

}

}

}

}

}

'

//score_mode模式默认none,其它的有:avg,min,max,sum

curl -XGET 192.168.244.129:9200/company/branch/_search -d '

{

"query": {

"has_child": {

"type": "employee",

"score_mode": "max",

"query": {

"match": {

"name": "Alice Smith"

}

}

}

}

}

'

// has_child接受参数:min_children,max_children,只有当子文档数量在指定范围内时,才会返回父文档, 例如:至少有两个雇员的分公司才会符合查询条件

curl -XGET 192.168.244.129:9200/company/branch/_search -d '

{

"query": {

"has_child": {

"type": "employee",

"min_children": 2,

"query": {

"match_all": {}

}

}

}

}

'

//通过父文档查询子文档

curl -XGET 192.168.244.129:9200/company/employee/_search -d '

{

"query": {

"has_parent": {

"type": "branch",

"query": {

"match": {

"country": "UK"

}

}

}

}

}

'

//-子文档中支持 子文档聚合

curl -XGET 192.168.244.129:9200/company/branch/_search -d '

{

"size" : 0,

"aggs": {

"country": {

"terms": {

"field": "country.keyword"

},

"aggs": {

"employees": {

"children": {

"type": "employee"

},

"aggs": {

"hobby": {

"terms": {

"field": "hobby.keyword"

}

}

}

}

}

}

}

}

'

本文来源:https://www.2haoxitong.net/k/doc/3ad33003905f804d2b160b4e767f5acfa1c7836c.html

《ElasticSearch命令.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式