Reader small image

You're reading from  Learning Elastic Stack 6.0

Product typeBook
Published inDec 2017
PublisherPackt
ISBN-139781787281868
Edition1st Edition
Right arrow
Authors (2):
Pranav Shukla
Pranav Shukla
author image
Pranav Shukla

Pranav Shukla is the founder and CEO of Valens DataLabs, a technologist, husband, and father of two. He is a big data architect and software craftsman who uses JVM-based languages. Pranav has diverse experience of over 14 years in architecting enterprise applications for Fortune 500 companies and start-ups. His core expertise lies in building JVM-based, scalable, reactive, and data-driven applications using Java/Scala, the Hadoop ecosystem, Apache Spark, and NoSQL databases. He is a big data engineering, analytics, and machine learning enthusiast.
Read more about Pranav Shukla

Sharath Kumar M N
Sharath Kumar M N
author image
Sharath Kumar M N

Sharath Kumar M N did his master's in computer science at the University of Texas, Dallas, USA. He is currently working as a senior principal architect at Broadcom. Prior to this, he was working as an Elasticsearch solutions architect at Oracle. He has given several tech talks at conferences such as Oracle Code events. Sharath is a certified trainer Elastic Certified Instructor one of the few technology experts in the world who has been certified by Elastic Inc. to deliver their official from the creators of Elastic training. He is also a data science and machine learning enthusiast. In his free time, he likes playing with his lovely niece, Monisha; nephew, Chirayu; and his pet, Milo.
Read more about Sharath Kumar M N

View More author details
Right arrow

Chapter 2. Getting Started with Elasticsearch

In the first chapter we looked at the reasons for learning about and using Elastic Stack, and the use cases of Elastic Stack. In this chapter, we will start our journey of learning about Elastic Stack, starting at the core of Elastic Stack—Elasticsearch. Elasticsearch is the search and analytics engine behind Elastic Stack. We will learn the core concepts of Elasticsearch while doing some hands-on practice; we will learn about querying, filtering, and searching.

We will cover the following topics in this chapter:

  • Using the Kibana Console UI
  • Core concepts
  • CRUD (Create, Read, Update, Delete) operations
  • Creating indexes and taking control of mapping
  • REST API overview

Using the Kibana Console UI


Before we start writing our first queries to interact with Elasticsearch, we should familiarize ourselves with a very important tool: Kibana Console. This is important because Elasticsearch has a very rich REST API, allowing you to do all possible operations with Elasticsearch. Kibana Console has an editor which is very capable and aware of the REST API. It allows for auto-completion, and for the formatting of queries as you write them.

Note

What is a REST API? REST stands for Representational State Transfer. It is an architectural style to make systems inter-operate and interact with each other. REST has evolved along with the HTTP protocol, and almost all REST-based systems use HTTP as their protocol. HTTP supports different methods including GET, POST, PUT, DELETE, HEAD, and so onwhich are used for different semantics. For example, GET is used for getting or searching for something. POST is used for creating a new resource, PUT may be used for creating or updating...

Core concepts


Relational databases have concepts such as rows, columns, tables, and schema. Elasticsearch and other document-oriented stores are based on different abstractions. Elasticsearch is a document-oriented store. JSON documents are first class citizens in Elasticsearch. These JSON documents are organized within different types and indexes. We will look at the following core abstractions of Elasticsearch:

  • Index
  • Type
  • Document
  • Cluster
  • Node
  • Shards and replicas
  • Mappings and types
  • Inverted index

Let us start learning these with an example:

PUT /catalog/product/1
{
    "sku": "SP000001",
    "title": "Elasticsearch for Hadoop",
    "description": "Elasticsearch for Hadoop",
    "author": "Vishal Shukla",
    "ISBN": "1785288997",
    "price": 26.99
}

Copy and paste this example into the editor of your Kibana Console UI and execute it. This will index a document which represents a product in the product catalog of a system. All examples written for the Kibana Console UI can be very easily converted...

CRUD operations


In this section we will look at how to perform basic CRUD operations, which are the most fundamental operations required by any data store. Elasticsearch has a very well designed REST API, and the CRUD operations are targeted at documents. 

To understand how to perform CRUD operations, we will cover the following APIs. These APIs fall under the category of Document APIs that deal with documents:

  • Index API
  • Get API
  • Update API
  • Delete API

Index API

In Elasticsearch terminology, adding (or creating) a document into a type within an index of Elasticsearch is called an indexing operation. Essentially, it involves adding the document to the index by parsing all fields within the document and building the inverted index. This is why this operation is known as an indexing operation.

There are two ways we can index a document:

  • Indexing a document by providing an ID
  • Indexing a document without providing an ID

Indexing a document by providing an ID

We have already seen this version of the indexing...

Creating indexes and taking control of mapping


In the previous section, we learnt how to perform CRUD operations with Elasticsearch. In the process, we saw how indexing the first document to an index which doesn't yet exist results in the creation of the new index and the mapping of the type.

Usually, you wouldn't want to let things happen automatically, as you would want to control how indices are created and also how mapping is created. We will see how you can take control of this process in this section and look at the following:

  • Creating an index
  • Create a mapping
  • Updating a mapping

Creating an index

You can create an index and specify the number of shards and replicas to create:

PUT /catalog
{
  "settings": {
    "index": {
      "number_of_shards": 5,
      "number_of_replicas": 2
    }
  }
}

It is possible to specify a mapping for a type at the time of index creation. The following command will create an index called catalog with five shards and two replicas. Additionally, it also defines...

REST API overview


We just looked at how to perform basic CRUD operations. Elasticsearch supports a wide variety of operation types. Some operations deal with documents, that is, creating, reading, updating, deleting, and so on. Some operations provide search and aggregations, while other operations are for providing cluster related operations, such as monitoring health. Broadly, the APIs that deal with Elasticsearch are categorized into the following types of APIs:

  • Document APIs
  • Search APIs
  • Aggregations APIs
  • Indices APIs
  • Cluster APIs
  • cat APIs

The Elasticsearch reference documentation has documented these APIs very nicely. In this book, we will not go into the APIs down to the last detail. We will conceptually understand, with examples, how the APIs can be leveraged to get the best out of Elasticsearch and other components of Elastic Stack.

We will look at the search and aggregation APIs in Chapter 3, Searching-What is Relevant and Chapter 4, Analytics with Elasticsearch respectively.

In the following...

Summary


In this chapter, we learned about the essential Kibana Console UI and curl commands to interact with Elasticsearch with the REST API. Then we looked at the core concepts of Elasticsearch. We performed customary CRUD operations that are required as support for any data store. We took a closer look at how to create indexes, and how to create and manage mappings. We ended the chapter with an overview of the REST API in Elasticsearch, and the common conventions used in most APIs.

In the next chapter, we will take a deep dive into the search capabilities of Elasticsearch to understand the maximum benefits of Elasticsearch as a search engine.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Elastic Stack 6.0
Published in: Dec 2017Publisher: PacktISBN-13: 9781787281868
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Pranav Shukla

Pranav Shukla is the founder and CEO of Valens DataLabs, a technologist, husband, and father of two. He is a big data architect and software craftsman who uses JVM-based languages. Pranav has diverse experience of over 14 years in architecting enterprise applications for Fortune 500 companies and start-ups. His core expertise lies in building JVM-based, scalable, reactive, and data-driven applications using Java/Scala, the Hadoop ecosystem, Apache Spark, and NoSQL databases. He is a big data engineering, analytics, and machine learning enthusiast.
Read more about Pranav Shukla

author image
Sharath Kumar M N

Sharath Kumar M N did his master's in computer science at the University of Texas, Dallas, USA. He is currently working as a senior principal architect at Broadcom. Prior to this, he was working as an Elasticsearch solutions architect at Oracle. He has given several tech talks at conferences such as Oracle Code events. Sharath is a certified trainer Elastic Certified Instructor one of the few technology experts in the world who has been certified by Elastic Inc. to deliver their official from the creators of Elastic training. He is also a data science and machine learning enthusiast. In his free time, he likes playing with his lovely niece, Monisha; nephew, Chirayu; and his pet, Milo.
Read more about Sharath Kumar M N