Skip to content

Elasticsearch – Index, document, and search

I have recently wrote blogs about ELK Stack and more specifically around Elasticsearch, this blog comes as a complement of the previous blog (Data lifecycle management).

What is an index?

To understand what is an index in Elasticsearch, first we have to know that Elasticsearch is a distributed document store. Instead of storing information as rows of columnar data, Elasticsearch stores complex data structures that have been serialized as JSON documents, each document consists of fields and values. For example, if we want to store dbi services blogs in Elasticsearch, the below could be the document of my previous blog:

{
  "title": "Index data with Elastic",
  "category": "User Stories",
  "author": {
    "first_name": "David",
    "last_name": "Diab"
  }
}

Each document is indexed into an index, which is a logical way of grouping data.

So an index can be thought of as an optimized collection of documents and each document is a collection of fields, which are the key-value pairs that contain your data. By default, Elasticsearch indexes all data in every field and each indexed field has a dedicated, optimized data structure.

How to create an index?

To create an index you can send a REST request and use PUT.

Questions about your own setup? A dbi services consultant can look at your case.

Talk to an expert (opens in a new tab)
curl -X PUT "localhost:9200/dbi-services-blogs?pretty"

Please note that if the Elasticsearch security features are enabled, you must have the create_index or manage index privilege for the target index.

But, using curl all the time can be a bit tedious, that’s why Kibana has Console, which is a developer tool for creating and submitting Elasticsearch requests more easily.

To access this tool from Kibana, from the left menu, click on Dev Tools.

Console will be the tab by default, I made a first query to get nodes list, result is visible on the right.

Now, you are familiar with the console tool, we can come back to our index creation.

The index dbi-services-blog has been created successfully, please note that here we keep default settings (shards, replicas, aso), don’t worry we will talk about in a separate blog.

How to create a document?

Let’s create a document inside our index, e.g. my last blog:

You noticed, the history of your queries stay until you delete them, which is really cool!

It is time to say that you don’t need to create an index before creating a document. In fact, create a document means index creation if not exists, let’s recall the last query change the name of the index to new-dbi-services-blogs:

How to find a document?

Now, my document (blog) has been indexed in dbi-services-blogs index, let’s do a quick search to retrieve it:

Easy, there is only one document in my index, but what to do in case of millions of documents? Here we are in the search side of Elasticsearch, you know it is for search 😉

There are some query options to search for a document, I will not go in deep in this blog to keep it readable. Below is a Domain Specific Language (DSL) query to search for any document having “Index” in its title filed.

Conclusion

In this blog we saw how to create an index, a document, and how to find it, we discovered the Kibana Console and some queries. Hope that this blog will help you to start with Elasticsearch 🙂

This is not the end of the blog series, it is just the beginning! See you in the next blog.

Let’s work together

Ready to operate at
the next level?

Tell us about your challenge. We’ll bring the depth, the expertise, and the Swiss precision.

David Diab

David Diab

David Diab has more than 10 years of experience deploying and administering enterprise applications. His expertise spans Elastic Stack, WebLogic, JBoss, Tomcat, and Documentum, plus DevOps tooling (Kubernetes, CI/CD), scheduling solutions (Control-M, Dollar Universe), backup (NetBackup, Backup Exec), and file transfer (CFT). He holds certifications including Elastic Certified Engineer, OpenText Certified Documentum Administrator, Oracle Certified Professional WebLogic Server 12c, Certified Kubernetes Administrator, and ITIL V3 Foundation. Prior to dbi services, David worked as Integration and Operations Engineer at PSA Peugeot Citroën and at Atos. He holds an Engineering degree in Computer Science from UTBM. His industry experience covers automotive, banking, cosmetics, pharmaceutical, insurance, and chemical sectors.View Author posts

75 posts since January 2019

Most read in Elastic Stack

The Elastic Stack posts our readers open most: the all-time favourites, and what is trending right now.

2 thoughts on “Elasticsearch – Index, document, and search”

  1. Hi webtechie,

    As far as I know, this is not possible because there is always one and only one Tokenizer, which will behave exactly the same way for all entries.
    If you find a way to do that don’t hesitate to share it here.

    Regards,
    David

  2. I need to search for ‘the’ and ‘THE’ both, but i want first ‘the’ to search for case insensitive and ‘The’ to search for exact value as case- sensitive.
    My collection has below values:
    [
    {“title”:”the sample value”},
    {“title”:”The sample value”},
    {“title”:”THE sample value”}
    ]

    I want ‘THE’ to search only the last one, and ‘the’ to search for all.
    Can you please guide me how to achieve this. I have not used any tokenizer or analyzer or anyhing, just added the data as it is (as shown in above example).
    Can you please share some example as well.
    Your help is highly appreciated.
    Thank you in advance.

Leave a Comment

Your email address will not be published. Required fields are marked *