In this
post, I am going to explain how to insert data into elastic search. The process
of storing data into elastic search is called indexing.
Before
storing a document, we need to decide where to store a document. I mean if you
are storing an employee record into RDBMS, you have to know the database and table
details. Same like database, we store a document into a type, and these types
live inside an index.
Relational DB => Databases => Tables =>
Rows => Columns
Elasticsearch
=> Indices => Types =>
Documents => Fields
In elastic
search
a.
Index
is similar to database in RDBMS
b.
Type
is similar to table
c.
Document
is similar to row in a table
d.
Field
of document is similar to column of a row.
Elastic
search cluster can contain multiple indexes, each index can have multiple types,
each type can store number of documents, each document can have multiple
fields.
When you
insert a document into elastic search, by default every field in the document
is indexed.
For our
learning purpose, I am going to create document store for all employees of
company xyz.
Index : xyz
Type :
employees
Type
employees contain all information about employees.
Following
syntax is used to insert document.
PUT
/{index}/{type}/{id}
{
"field": "value",
...
}
It is time
to insert first employee into type employees.
PUT /xyz/employees/1 { "firstName": "Phalgun", "lastName": "Garimella", "hobbies": [ "Watching movies", "Stamp collection", "Reading books", "Playing Cricket" ], "age": 30 }
Observe the
path “/xyz/employees/1”. It has three pieces of information.
xyz : The
index name
employee :
The type name
1 : The ID
of this particular employee
Request body
contains information about employee, we used PUT request to insert employee
information into type employees.
Try to
insert some more employee documents into employees type.
PUT /xyz/employees/2 { "firstName": "Sankalp", "lastName": "Dubey", "hobbies": [ "Shopping", "Swimming", "Reading books" ], "age": 32 }
Above
snippet insert second document into type employees.
Note:
a. Index
name is always in lower case, otherwise, you will get following error.
{
"error": "InvalidIndexNameException[[XYZ]
Invalid index name [XYZ], must be lowercase]",
"status": 400
}
No comments:
Post a Comment