Just Elasticsearch: 4/n. Document APIs

  1. Introducing Elasticsearch
  2. Architecture
  3. Indexing Operations

Overview

Documents

{
"title":"Elasticsearch for Java Developers",
"author":"Madhusudhan Konda",
"synopsis":"A hands-on book for Java developers",
"release_date":2020-08-01,
"price":9.99
}

Creating Document using PUT

PUT books/_doc/1
{
"title":"Elasticsearch for Java Developers",
"author":"Madhusudhan Konda"
}
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}

Creating a document using the POST method

POST books/_doc
{
"title":"All About Java 8 Lambdas",
"author":"Madhusudhan Konda"
}
{
"_index" : "books",
"_type" : "_doc",
"_id" : "jOj6d3ABTpmmoDQOlC6_",
...
}

When To Use PUT or POST

Reading Documents

GET <index_name>/<doc_type>/<id>
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "Elasticsearch for Java Developers",
"author" : "Madhusudhan Konda",
...
}
}
{
"_index": "books",
"_type": "_doc",
"_id": "99",
"found": false
}

Manipulating Responses

Suppressing Source Fields

GET books/_source/1
{
"title": "Elasticsearch for Java Developers",
"author": "Madhusudhan Konda",
...
}
GET books/_doc/1?_source=false

Include/Exclude fields

GET books/_doc/1?_source_includes=title,synopsis,price
GET books/_doc/1?_source_includes=price_usd_,price_gbp,price_eur & _source_exludes=price_usd
GET books/_doc/1?_source_includes=price*

Document Existence

Deleting Documents

Delete With an ID

DELETE <index>/<type>/<id>

Deleting by Query

POST books/_delete_by_query
{
"query":{
"match":{
"author":"Madhusudhan Konda"
}
}
}

Delete By Range Query

POST books/_delete_by_query
{
"query":{
"range":{
"price_usd":{
"gt":9
}
}
}
}

Delete All Documents

POST books/_delete_by_query
{
"query":{
"match":{
"match_all":{}
}
}
}

Delete Documents across Multiple Indices

POST old_books,tech_books,classic_books/_delete_by_query
{
"query":{
"match":{
"match_all":{}
}
}
}

Updating Documents

  • The document is fetched for the given ID
  • The document is updated with the relevant fields
  • The document is re-indexed (essentially it’s replaced with the new document we are sending in).

Update Scenarios

  • Adding additional fields to an existing document
  • Modifying fields to an existing document
  • Replacing the whole document
POST <index_name>/_update/<id>
POST /books/_update/1
{
"doc":{
"pages":200
}
}
POST books/_update/1
{
"doc": {
"pages":350
}
}

Scripted Updates

POST books/_update/1
{
"script":{
"source":{
"ctx._source.pages = 295"
}
}
}
POST books/_update/1
{
"script":{
"source":{

"ctx._source.price_usd = 9.99"
}
}
}
POST  books/_update/1
{
"script": {
"source": """
ctx._source.release_year = 2020;
ctx._source.price_usd = 9.99
"""
}
}
POST books/_update/1
{
"script" : {
"source": "if(ctx._source.ratings > params.ratings)
{ ctx._source.top_seller=true }
else
{ctx._source top_seller=false}",
"lang": "painless",
"params" : {
"ratings" : 4.5
}
}
}

Replacing Documents

// PUT a new document with a ISBN but with the same ID
PUT books/_doc/1
{
"isbn":123456
}

Upsert

POST books/_update/100
{
"script":{
"source":"ctx._source.title='Just Spring 2e'"
},
"upsert":{
"title":"Just Spring 2e",
"author":"Madhusudhan Konda"
}
}

Summary

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Madhusudhan Konda

Madhusudhan Konda

47 Followers

Madhusudhan Konda is a full-stack lead engineer, mentor, and conference speaker. He delivers live online training on Elasticsearch, Elastic Stack &Spring Cloud