# Create Index and check index
PUT /customer?pretty
GET /_cat/indices?v
# Insert a customer with id = 1 and name is "Bien Nguyen"
PUT /customer/_doc/1?pretty
{
"name": "Bien Nguyen"
}
# Get information of abow insert
GET /customer/_doc/1?pretty
# Insert with not specify id and ES will random an id
POST /customer/_doc/?pretty
{
"name": "Bien Nguyen"
}
# Delete an index and check index
DELETE /customer?pretty
GET /_cat/indices?v
# Create and insert again
PUT /customer
PUT /customer/_doc/1
{
"name": "Bien Nguyen"
}
# Replace content of name field
PUT /customer/_doc/1
{
"name": "Bien Nguyen ver 2"
}
# if we change id, it creates a new document with id = 2
PUT /customer/_doc/2
{
"name": "Bien Nguyen"
}
#To update document we use POST
# This will update customer have id =2 with name change to Mr. Sea
POST /customer/_doc/2/_update?pretty
{
"doc": {"name": "Mr. Sea"}
}
# We can change a name at the same time add an age field
POST /customer/_doc/2/_update?pretty
{
"doc": {
"name": "Mr. SeaSea",
"age": 22
}
}
# We also can change document with script
POST /customer/_doc/2/_update?pretty
{
"script": "ctx._source.age +=1"
}
# Delete document
DELETE /customer/_doc/1?pretty
DELETE /customer/_doc/2?pretty
DELETE /customer/_doc/K76gZGwBGxJL9xkwYav_?pretty
# Batch using _bulk
# Do 2 action in 1 bulk operation
POST /customer/_doc/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "Bien Nguyen" }
{"index":{"_id":"2"}}
{"name": "Mr. Sea" }
#
POST /customer/_doc/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "update Bien Nguyen become Biennt" } }
{"delete":{"_id":"2"}}
GET /customer/_search?pretty
###################End customer#########################
# Begin using a bank data
# Search bank data and sort by account_number
# Using query match_all to search all data
# Sort (arrray behind) all data with account_number field by acs
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"account_number": {
"order": "asc"
}
}
]
}
##### Query ###########3
# Query begin with GET -> index/_search -> query keywork
# -> condition
# Here is simplest query example - Search all document in index
GET /bank/_search
{
"query": {
"match_all": {}
}
}
# After query, we cam use size like sort in abow example
# default size = 10, here we set to 1
GET /bank/_search
{
"query": {
"match_all": {}
},
"size": 1
}
#Search and show 10 document from result have index 10
GET /bank/_search
{
"query": {
"match_all": {}
},
"from": 10,
"size": 10
}
#Get all and sort by balance desc, that like abow example
GET /bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"balance": {
"order": "desc"
}
}
]
}
# Search and show sepecific field: account_number and balance
GET /bank/_search?pretty
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
# Search with condition: account_number = 20
GET /bank/_search?pretty
{
"query": { "match": { "account_number": 20 } }
}
# Same with address = mill
GET /bank/_search?pretty
{
"query": { "match": { "address": "mill" } }
}
# Seach document has address mill or lane
GET /bank/_search?pretty
{
"query": { "match": { "address": "mill lane" } }
}
# Search phrase: document has mill and lane word
GET /bank/_search?pretty
{
"query": {
"match_phrase": {
"address": "mill lane"
}
}
}
# We can also use bool to group more condition
# must: and , should: or, must_not: not, filter: like must be score of query will be
ignorerd | require array behind
GET /bank/_search?pretty
{
"query": {
"bool": {
"must": [
{"match": {
"address": "mill"
}},
{
"match": {
"address": "lane"
}
}
]
}
}
}
GET /bank/_search?pretty
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
GET /bank/_search?pretty
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
# Using document must have age = 40 and must_not have state = ID
GET /bank/_search?pretty
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
#
POST /_search?pretty
{
"query": {
"bool" : {
"must" : {
"term" : { "user" : "kimchy" }
},
"filter": {
"term" : { "tag" : "tech" }
},
"must_not" : {
"range" : {
"age" : { "gte" : 10, "lte" : 20 }
}
},
"should" : [
{ "term" : { "tag" : "wow" } },
{ "term" : { "tag" : "elasticsearch" } }
],
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}
############3 Filter##############
# Search all data and filter balance between 20.000-30.000
GET /bank/_search?pretty
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
########Aggregations#############3
#SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*)
DESC LIMIT 10;
# Set size = 0 to just see aggregations
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"size": 10
}
}
}
}
# Calculate average balance by state
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"
}
}
}
}
}
}
# Aggregation with age (20-29)(30-39)(40-49), then gender, then calculate average
balance
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 29
},
{
"from": 30,
"to": 39
},
{
"from": 40,
"to": 49
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}