ES命令汇总

一、集群管理

1、查看集群健康状态

curl -X GET "localhost:9200/_cat/health?v"

2、查看集群健康状态

curl -X GET "localhost:9200/_cluster/health?pretty"

3、查看集群中的节点

curl -X GET "localhost:9200/_cat/nodes?v"

4、列出当前所有的索引

curl -X GET "localhost:9200/_cat/indices?v"

5、创建索引

curl -X PUT "localhost:9200/customer?pretty"

6、删除索引

curl -X DELETE "localhost:9200/customer?pretty"

7、设置数据类型

curl -X PUT "localhost:9200/allcountry?pretty" -H "Content-Type:application/json" -d '
{
    "settings": {
        "index.number_of_replicas": 0
    },
    "mappings": {
        "country_info": {
            "properties": {
                "geonameid": {
                    "type": "long"
                },
                "name": {
                    "type": "text"
                },
                "latitude": {
                    "type": "double"
                },
                "longitude": {
                    "type": "double"
                },
                "population": {
                    "type": "long"
                }
            }
        }
    }
}'

二、数据管理

1、添加Document

curl -X PUT "localhost:9200/customer/customer_info/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'

2、查询Document

curl -X GET "localhost:9200/customer/customer_info/1?pretty"

3、替换Document

curl -X PUT "localhost:9200/customer/customer_info/1?pretty" -H "Content-Type:application/json" -d '
{
   "name":"Milton"
}
'

4、更新Document

curl -X POST "localhost:9200/customer/customer_info/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Apple" }
}
'

5、更新Document---添加新字段

curl -X POST "localhost:9200/customer/customer_info/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Tom", "age": 20 }
}
'

6、更新Document---使用Scripts脚本更新

curl -X POST "localhost:9200/customer/customer_info/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.age += 5"
}
'

注意:ctx代表被更新的对象本身

7、删除Document

curl -X DELETE "localhost:9200/customer/customer_info/2?pretty"

三、批量处理

1、批量添加Document

curl -X POST "localhost:9200/customer/customer_info/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"11"}}
{"name": "Milton" }
{"index":{"_id":"22"}}
{"name": "Cherish" }
'

2、批量更新和删除Document

curl -X POST "localhost:9200/customer/customer_info/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"11"}}
{"doc": { "name": "Milton Love Cherish" } }
{"delete":{"_id":"22"}}
'

3、批量创建/更新index

curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_index":"teacher","_type":"teacher_info","_id":1}}
{"name":"Miltom"}
{ "index" : { "_index" : "teacher", "_type" : "teacher_info", "_id" : "2" } }
{ "name" : "Cherish" }
{ "index" : { "_index" : "teacher", "_type" : "teacher_info", "_id" : "3" } }
{ "name" : "Evan" }
'

4、批量创建create

curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "create" : { "_index" : "teacher", "_type" : "teacher_info", "_id" : "4" } }
{ "name" : "yangp" }
{ "create" : { "_index" : "teacher", "_type" : "teacher_info", "_id" : "5" } }
{ "name" : "yangf" }
'

注意:index 和 create 都可以增加文档。使用index时,如果记录已存在,则会进行更新,如果不存在,则会新增;但是使用create时,如果记录已存在,则会创建失败!

5、批量更新update

curl -X POST "localhost:9200/teacher/teacher_info/_bulk?pretty" -H "Content-Type: application/json" -d '
{ "update" : { "_id" : "4" } }
{"doc":{ "name" : "yangp_update" }}
{ "update" : { "_id" : "5" } }
{"doc":{ "name" : "yangf_update" }}
'

6、批量删除delete

curl -X POST "localhost:9200/teacher/teacher_info/_bulk?pretty" -H "Content-Type: application/json" -d '
{"delete":{"_id":"4"}}
{"delete":{"_id":"5"}}
'

7、批量获取_mget

获取索引teacher中,id为1,2的document(三种方式)

curl -X GET "localhost:9200/_mget?pretty" -H "Content-Type: application/json" -d '
{
    "docs":[
        {
            "_index":"teacher",
            "_type":"teacher_info",
            "_id":"1",
	    "_source":["name"]
        },
        {
            "_index":"teacher",
            "_type":"teacher_info",
            "_id":"2",
	    "_source":["name"]
        }
        ]
}
'
curl -X GET "localhost:9200/teacher/teacher_info/_mget?pretty" -H "Content-Type: application/json" -d '
{
    "docs":[
        { "_id":"1"},
        { "_id":"2" }
        ]
}
'
curl -X GET "localhost:9200/teacher/teacher_info/_mget?pretty" -H "Content-Type: application/json" -d '
{
	"ids":["1","2"]
}
'

四、匹配删除与更新

1、匹配删除_delete_by_query

从索引teacher中删除name为“Evan”的document

curl -X POST "localhost:9200/teacher/_delete_by_query?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{
            "name":"Evan"
                }
            }
}
'

2、匹配更新_update_by_query

从索引teacher中,更新name包含“Miltom”的文档,设置其gener=“Boy”,age=100

curl -X POST "localhost:9200/teacher/teacher_info/_update_by_query?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{
            "name":"Miltom"
                }
    },
    "script":{
        "source":"ctx._source.gener=params.gener;ctx._source.age=params.age",
        "params":{
            "gener":"Boy",
            "age":10
            }
    }
}
'

五、数据导入

导入json文件accounts.json到es中

curl -X POST "localhost:9200/bank/bank_info/_bulk?pretty" -H "Content-Type: application/json" --data-binary "@accounts.json"

六、数据检索

  • query:指定查询条件,这里使用{ "match_all": {} }表示查询条件匹配所有记录
  • from:表示从第n条匹配记录开始取值,默认为0
  • size:表示匹配条数,默认10
  • sort:表示排序,这里使用{ "balance": { "order": "desc" }},表示按balance降序排序,这里也可以写成[{ "balance": "desc" }]
  • _source:表示查询字段,这里使用["account_number", "balance"]表示返回结果中,只需要返回"account_number", "balance"两个字段即可。默认返回所有字段。
curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{"match_all":{}},
    "from":10,
    "size":5,
    "sort":[
        {"balance":"desc"}
        ],
    "_source":["account_number","balance"]
}
'

1、match查询:查询 account_number=20 的document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{"account_number":20}
        }
}
'

2、match查询:查询 address 中包含 “mill” 的document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{"address":"mill"}
        }
}
'

3、match查询:查询 address 中包含 “mill” 或者 “lane” 的document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{"address":"mill lan"}
        }
}
'

4、match_phrase查询:查询 address 中包含短语 “mill lane” 的document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match_phrase":{"address":"mill lane"}
        }
}
'

注意:match与match_phrase的区别

  • 对于match的结果,我们可以可以看到,结果的Document中description这个field可以包含“He is”,“He”或者“is”;
  • match_phrased的结果中的description字段,必须包含“He is”这一个词组;
  • 所有的检索结果都有一个_score字段,看起来是当前这个document在当前搜索条件下的评分,而检索结果也是按照这个得分从高到低进行排序。

5、bool and关系查询:查询address中同时包含“mill”与“lane”的document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "must":[
                {"match":{"address":"mill"}},
                {"match":{"address":"lane"}}
                ]
            }
    }
}
'

6、 bool or关系查询:查询address中同时包含“mill”与“lane”的document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "should":[
                {"match":{"address":"mill"}},
                {"match":{"address":"lane"}}
                ]
            }
    }
}
'

7、bool not关系查询:查询 address 中即不存在 “mill” 也不存在 “lane” 的document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "must_not":[
                {"match":{"address":"mill"}},
                {"match":{"address":"lane"}}
                ]
            }
    }
}
'

8、bool组合查询:查询 age=40,state!="ID" 的document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "must":[
                {"match":{"age":40}}
                ],
            "must_not":[
                {"match":{"state":"ID"}}
                ]
            }
      }
}
'

9、bool filter查询:查询20000<=balance<=30000的document

curl -X GET "localhost:9200/bank/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "filter":{
                "range":{
                    "balance":{
                        "gte":20000,
                        "lte":30000
                        }
                    }
                }
            }
        }
}
'

七、在多索引中搜索

1、同时在bank,teacher两个索引中搜索

curl -X GET "localhost:9200/bank,teacher/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{"age":39}
        }                     
}'

2、在所有索引中搜索

curl -X GET "localhost:9200/_all/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match":{"age":39}
        },
    "size":100
}'

八、查询所有文件

curl -X GET "localhost:9200/website/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "match_all":{}
        }
}'

九、query查询条件与filter过滤条件

1、查询“title”中包含“Search”,“content”中包含“Elasticsearch”,“status”为“published”,“publish_date”>2015-01-01的文档

curl -X GET "localhost:9200/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "bool":{
            "must”:[
                {"match":{"title":"Search"}},
                {"match":{"content":"Elasticsearch"}}
                ],
            "filter":[
                {"term":{"status":"published"}},
                {"range":{"publish_date":{"gte":"2015-01-01"}}}
                ]
            }
    }
}'

十、从多个字段中搜索 multi_match

1、从"title","content"中搜索"learn"

curl -X GET "localhost:9200/website/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "multi_match":{
            "query":"learn",
            "fields":["title","content"]
            }
        }
}'

2、从"title","content"中搜索"here"

curl -X GET "localhost:9200/website/_search?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "multi_match":{
            "query":"here",
            "fields":["title","content"]
            }
        }
}'

十一、搜索统计_count

curl -X GET "localhost:9200/website/blog/_count?pretty" -H "Content-Type: application/json" -d '
{
    "query":{
        "term":{"title":"learn"}
    }
}'

 

你可能感兴趣的:(ES,ES)