Skip to content

Instantly share code, notes, and snippets.

@bcotter

bcotter/CRUD Secret

Created June 30, 2015 18:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bcotter/8a2371dd6a5945bd8865 to your computer and use it in GitHub Desktop.
Save bcotter/8a2371dd6a5945bd8865 to your computer and use it in GitHub Desktop.
Getting Started Webinar - CRUD Code
#__________________________________________________
# Document CRUD operations
#__________________________________________________
#__________________________________________________
# Index a JSON document
# ---- Index name
# |
# | -----Type name
# | |
# | | ---- Doc ID
# | | |
# V V V
PUT /library/books/1
{
"title": "A fly on the wall",
"name" : {
"first": "Drosophila",
"last": "Melanogaster",
"location": {
"zip": "123"
}
},
"publish_date": "2015-06-21T23:39:49-0400",
"price": 19.95
}
GET library/books/_search
{
"query": {
"nested": {
"path": "path_to_nested_doc",
"query": {}
}
}
}
}
#__________________________________________________
# Retrieve the same document via ID
GET /library/books/1
#__________________________________________________
# IDs are optional if you don't want it:
POST /library/books/
{
"title": "Adventures of Strange-Foot Smooth",
"name" : {
"first": "Xenopus",
"last": "laevis"
},
"publish_date": "2015-05-01T06:11:26-0400",
"price": 5.99
}
#__________________________________________________
# To retrieve, we need the ID that was autogenerated
GET /library/books/AU4mY5X76XBHOGqmhW-Z
#__________________________________________________
# To update a doc, we can overwrite it
PUT /library/books/1
{
"title": "A fly on the wall Part 2",
"name" : {
"first": "Drosophila",
"last": "Melanogaster"
},
"publish_date": "2015-06-21T23:39:49-0400",
"price": 29.95
}
GET library/books/1
#__________________________________________________
# Or we can use the Update API
POST /library/books/1/_update
{
"doc": {
"price" : 10
}
}
GET /library/books/1
POST /library/books/1/_update
{
"doc": {
"cn_price" : 13
}
}
#__________________________________________________
# We can also delete a document
DELETE /library/books/1
GET /library/books/1
#__________________________________________________
# We can even delete an entire index. Be careful!
DELETE /library
GET /library/books/2
# More info: https://www.elastic.co/guide/en/elasticsearch/guide/current/data-in-data-out.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment