-
-
Save bcotter/8a2371dd6a5945bd8865 to your computer and use it in GitHub Desktop.
Getting Started Webinar - CRUD Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#__________________________________________________ | |
# 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