curl operate elasticsearch
export elasticsearchwebaddress=localhost:9200
# 1. Add document
curl -X PUT "$elasticsearchwebaddress/megacorp/employee/1" -d '
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
curl -X DELETE "$elasticsearchwebaddress/megacorp/employee/1" -d '
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
curl -X PUT "$elasticsearchwebaddress/megacorp/employee/2" -d '
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}'
curl -X PUT "$elasticsearchwebaddress/megacorp/employee/3" -d '
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}'
# 2. Retrive information
curl -X GET "$elasticsearchwebaddress/megacorp/employee/1"
#
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search"
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search?q=last_name:Smith"
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search" -d '
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}'
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search" -d '
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 }
}
},
"query" : {
"match" : {
"last_name" : "smith"
}
}
}
}
}'
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search" -d '
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}'
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search" -d '
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}'
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search" -d '
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}'
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search" -d '
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
} '
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search" -d '
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests"
}
}
}
}'
curl -X GET "$elasticsearchwebaddress/megacorp/employee/_search" -d '
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}'
curl -X GET "$elasticsearchwebaddress/_cluster/health"
curl -X GET "$elasticsearchwebaddress/_cluster/health?pretty"
curl -X PUT "$elasticsearchwebaddress/blogs" -d '
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}'
curl -X PUT "$elasticsearchwebaddress/website/blog/123" -d '
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}'
curl -i -X GET http://localhost:9200/website/blog/124?pretty
curl -i -X GET http://localhost:9200/website/blog/123?pretty\&_source=title,text
curl -i -X GET http://localhost:9200/website/blog/123?pretty
curl -i -X GET http://localhost:9200/website/blog/123/_source
#Checking whether document exists.
curl -i -XHEAD http://localhost:9200/website/blog/123
#Updating a document
curl -X PUT $elasticsearchwebaddress/website/blog/123 -d '
{
"title": "My first blog entry",
"text": "I am starting to get the hang of this...",
"date": "2014/01/02"
}'
curl -X PUT "$elasticsearchwebaddress/website/blog/124" -d '
{
"title": "My first blog entry",
"text": "Just trying this out...",
"date": "2014/01/01"
}'
curl -X DELETE $elasticsearchwebaddress/website/blog/123
curl -X DELETE $elasticsearchwebaddress/website/blog/1232
curl -X PUT $elasticsearchwebaddress/website/blog/1232 -d '
{
"title": "My first blog entry",
"text": "I am starting to get the hang of this...",
"date": "2014/01/02"
}'
curl -X PUT $elasticsearchwebaddress/website/blog/1/_create -d '
{
"title": "My first blog entry",
"text": "Just trying this out..."
}'
curl -X PUT $elasticsearchwebaddress/website/blog/1?version=1 -d '
{
"title": "My first blog entry",
"text": "Starting to get the hang of this..."
}'
curl -X PUT $elasticsearchwebaddress/website/blog/2?version=5\&version_type=external -d '
{
"title": "My first external blog entry",
"text": "Starting to get the hang of this..."
}'
curl -X PUT $elasticsearchwebaddress/website/blog/2?version=10\&version_type=external -d '
{
"title": "My first external blog entry",
"text": "This is a piece of cake..."
}'
#Partial update document
curl -X GET $elasticsearchwebaddress/website/blog/1?pretty
curl -X POST $elasticsearchwebaddress/website/blog/1/_update -d '
{
"doc" : {
"tags" : [ "testing" ],
"views": 0
}
}'
#Use scripts to update the value
curl -X POST $elasticsearchwebaddress/website/blog/1/_update -d '
{
"script" : "ctx._source.views+=1"
}'
curl -X GET $elasticsearchwebaddress/website/blog/1?pretty
curl -X POST $elasticsearchwebaddress/website/blog/1/_update -d '
{
"script" : "ctx._source.tags+=new_tag",
"params" : {
"new_tag" : "search"
}
}'
curl -X GET $elasticsearchwebaddress/website/blog/1?pretty
curl -X POST $elasticsearchwebaddress/website/blog/1/_update -d "
{
\"script\" : \"ctx.op = ctx._source.views == count ? 'delete' : 'none'\",
\"params\" : {
\"count\": 1
}
}"
#upset
#cannot found
curl -X GET $elasticsearchwebaddress/website/pageviews/1?pretty
curl -X POST $elasticsearchwebaddress/website/pageviews/1/_update -d '
{
"script" : "ctx._source.views+=1",
"upsert": {
"views": 1
}
}'
curl -X GET $elasticsearchwebaddress/website/pageviews/1?pretty
curl -X POST $elasticsearchwebaddress/website/pageviews/1/_update?retry_on_conflict=5 -d '
{
"script" : "ctx._source.views+=1",
"upsert": {
"views": 0
}
}'
#Retrieve multiple documents through multiple requests.
curl -X GET $elasticsearchwebaddress/_mget?pretty -d '
{
"docs" : [
{
"_index" : "website",
"_type" : "blog",
"_id" : 2
},
{
"_index" : "website",
"_type" : "pageviews",
"_id" : 1,
"_source": "views"
}
]
}'
curl -X GET $elasticsearchwebaddress/website/blog/_mget?pretty -d '
{
"docs" : [
{ "_id" : 2 },
{ "_type" : "pageviews", "_id" : 1 }
]
}'
curl -X GET $elasticsearchwebaddress/website/blog/_mget?pretty -d '
{
"ids" : [ "2", "1" ]
}'
#bulk API allows us to make multiple create, index, update or delete requests in a single step.
curl -X POST $elasticsearchwebaddress/_bulk -d '
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
{ "doc" : {"title" : "My updated blog post"} }
'
curl -X GET $elasticsearchwebaddress/website/blog