Software Development

elasticsearch for beginners part 1: how to create a document

In this Elasticsearch for beginners series, I will give you basics of Elasticsearch with real world examples. The first part is for teaching you how to create -that is index- a document in Elasticsearch.

I assume you managed to download the latest Elasticsearch zip to your local, extracted it and after that installed the marvel plugin and started a new node by running “elasticsearch” under your installation’s bin directory.

Now it is time for some action. We can choose to give custom id to our document or leave it to Elasticsearch so we won’t bother by generating a new one.

You can choose whatever content you want, but for keeping it simple I’ll consider the usual user example. Our user will have a  username, first name, last name and a contact phone.

1
2
3
4
5
6
{
   "username" : "kimchy",
   "fist_name" : "kimchy",
   "last_name" : "aloevera",
   "phone" : "00905332446578"
 }

Now lets open our Sense plugin. Go to “http://localhost:9200/_plugin/marvel/sense/index.html” after your node is up and running.

Before indexing anything I’ll tell you that we will need an index and a type information.

Index is like a database and type is like a table if we use  RDBMS analogies.

Please don’t get confused over the meaning of index. Indexing a document is creating a document, index information of a document is its database.

Lets insert our first document with custom id. We will use “elastic_101” as index and “user” as type.

1
2
3
4
5
6
7
PUT /elastic_101/user/1
 {
   "username" : "kimchy",
   "fist_name" : "kimchy",
   "last_name" : "aloevera",
   "phone" : "00905332446578"
 }

We can check if we are successful in indexing it.

The result to the following query

1
GET elastic_101/user/_search

is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
   "took": 16,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "elastic_101",
            "_type": "user",
            "_id": "1",
            "_score": 1,
            "_source": {
               "username": "kimchy",
               "fist_name": "kimchy",
               "last_name": "aloevera",
               "phone": "00905332446578"
            }
         }
      ]
   }
}

Now lets index a new document with autogenerated id.

1
2
3
4
5
6
7
POST /elastic_101/user/
 {
   "username" : "sezinkarli",
   "fist_name" : "sezin",
   "last_name" : "karli",
   "phone" : "00905322426528"
 }

Again let me check the result with

1
GET elastic_101/user/_search

Result is:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
   "took": 40,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "elastic_101",
            "_type": "user",
            "_id": "AVAFxb36kyTg7_oSOIVN",
            "_score": 1,
            "_source": {
               "username": "sezinkarli",
               "fist_name": "sezin",
               "last_name": "karli",
               "phone": "00905322426528"
            }
         },
         {
            "_index": "elastic_101",
            "_type": "user",
            "_id": "1",
            "_score": 1,
            "_source": {
               "username": "kimchy",
               "fist_name": "kimchy",
               "last_name": "aloevera",
               "phone": "00905332446578"
            }
         }
      ]
   }
}

As you can see both users have a username, first name, last name and a phone. Notice that if for kimchy is 1 which is the one we gave it and the other id is “AVAFxb36kyTg7_oSOIVN” which is generated by Elasticsearch.

In this part of my Elasticsearch tutorial, we learnt how to create documents with custom or auto-generated id. You can have more in depth information in my Elasticsearch in Action course and this link will make a discount of 60%.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Sezin Karli

Mathematics Engineer & Computer Scientist with a passion for software development. Avid learner for new technologies. Currently working as Senior Software Engineer at Sahibinden.com.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
kavya sahu
kavya sahu
7 years ago

informative info

Back to top button