-
Notifications
You must be signed in to change notification settings - Fork 70
Ch 5. Document Operations
Indexing a movie document with an identifier:
//Request
PUT movies/_doc/1
{
"title":"The Godfather",
"synopsis":"The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son"
}
// Response:
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "The Godfather",
"synopsis" : "The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son"
}
}
Indexing a movie review document - the document isn't expected to have an ID provided by the user (note the POST method invocation):
// Request
POST movies_reviews/_doc
{
"movie":"The Godfather",
"user":"Peter Piper",
"rating":4.5,
"remarks":"The movie started with a .."
}
// Response:
{
"_index" : "movies_reviews",
"_type" : "_doc",
"_id" : "6HPnfXoBW8A1B2am0B5U",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 4,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
The ID was not provided by the user. System generated one (6HPnfXoBW8A1B2am0B5U
) and assigned to the document
Let's add another document to the movies index:
PUT movies/_doc/3
{
"title":"The Shawshank Redemption",
"synopsis":"Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency",
"rating":"9.3",
"certificate":"15",
"genre":"drama",
"actors":["Morgan Freeman","Tim Robbins"]
}
To include title
, rating
,genre
, run the following query:
GET movies/_source/3?_source_includes=title,rating,genre
The output would be:
{
"rating" : "9.3",
"genre" : "drama",
"title" : "The Shawshank Redemption"
}
To exclude fields, run the following query:
GET movies/_source/3?_source_excludes=actors,certificate
This will return:
{
"rating" : "9.3",
"certificate" : "15",
"genre" : "drama",
"title" : "The Shawshank Redemption"
}
Let's first update our movie document:
PUT movies/_doc/3
{
"title":"The Shawshank Redemption",
"synopsis":"Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency",
"rating":"9.3",
"certificate":"15",
"genre":"drama",
"actors":["Morgan Freeman","Tim Robbins"],
"rating_amazon":4.5,
"rating_rotten_tomatoes":80,
"rating_metacritic":90
}
DELETE movies/_doc/1
This will result in:
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"result" : "deleted",
"_shards" : {
"total" : 4,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1
}
Let's update a couple of fields to our existing movie document:
PUT movies/_doc/1
{
"title":"The Godfather",
"synopsis":"The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son"
}
Update the document with additional fields actors and director:
POST movies/_update/1
{
"doc": {
"actors":["Marldon Brando","Al Pacino","James Cann"],
"director":"Frances Ford Coppola"
}
}
If the operation is successful, the response would be:
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 4,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}