A quick guide to using the basic functions of Elasticsearch 5.4.
Mostly stolen wholesale from Elastic's own Getting Started page.
GET _cat/indices?h=i
PUT legends?pretty
GET _cat/indices?h=i
PUT legends/jpl/1?pretty
{
"name": "Randy Moss"
}
GET legends/jpl/1?pretty
DELETE legends?pretty
GET _cat/indices?h=i
PUT legends/jpl/1?pretty
{
"name": "Randy Moss"
}
PUT legends/jpl/1?pretty
{
"name": "Jeff Liu"
}
PUT legends/jpl?pretty
{
"name": "Randy Moss"
}
POST legends/jpl/1/_update?pretty
{
"doc": { "name": "Randal Moss" }
}
POST legends/jpl/1/_update?pretty
{
"doc": { "name": "Randal Moss", "bootcamps": 2 }
}
POST legends/jpl/1/_update?pretty
{
"script": "ctx._source.bootcamps += 1"
}
DELETE legends/jpl/2?pretty
GET legends/_search
Download this sample dataset. Save it to your current directory as accounts.json
.
In your terminal (replace localhost
with your ES host):
curl -H "Content-Type: application/json" -XPOST -u elastic:changeme 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
curl -u elastic:changeme 'localhost:9200/_cat/indices?h=i'
GET bank/_search?q=*&sort=account_number:asc&pretty
GET bank/_search
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
GET bank/_search
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}
GET bank/_search
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}
GET bank/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
GET bank/_search
{
"query": { "match": { "address": "mill" } }
}
GET bank/_search
{
"query": { "match": { "address": "mill lane" } }
}
GET bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}
GET bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
GET bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
GET bank/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
GET bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
GET bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
GET bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
GET bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
GET bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
GET bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}