Skip to main content

Getting Started With Elasticsearch


Elasticsearch is open source search system based of Apache Lucene. this is a brief introduction, you can find more information on Elasticsearch Official Site or  Wiki.

What are features of Elasticsearch?

Here are list of features of Elasticsearch.
  • Real Time Search and Analytic.
  • Distributed Data (Scales Horizontally) 
  • Based on Apache Lucene
  • High Availability
  • Full Text Search
  • JSON Based Documents
  • RESTful API
  • Multi Facets
  • Geo Location Search
  • Open Source

How to install ElasticSearch?


Download the latest version from elasticsearch.org/download.
Then unzip it or use following command in console to download Elasticsearch.

curl -L -O http://download.elasticsearch.org/PATH/TO/LATEST/$VERSION.zip
unzip elasticsearch-$VERSION.zip
cd  elasticsearch-$VERSION

Now move to unzip folder and run following command

{elasticsearch unzip folder path}/bin/elasticsearch

It will start Elasticsearch

How to use ElasticSearch?

To interactive with data using Elasticsearch we will use head plugin of Elasticsearch.

To install head plugin in your system run following command.

{elasticsearch unzip folder path}/bin/plugin --install mobz/elasticsearch-head

After successful installation open your favorite browser and  type following URL to use head plugin.


It will open head plugin user interface, now we can manipulate Elasticsearch records.

To start with data manipulation, first we need to create Index and Type.

Index acts as Database and Type as Table, therefore we can have multiple Type inside single Index.

First create new index, to create new index click button New Index from homepage of head plugin.




Then type name of index (for this blog we type "database" as Index and use Type as "Table") and click Ok.
It will create new index on Elasticsearch.

How to perform CRUD operation on Elasticsearch?

To perform CRUD operation on Elasticsearch select Any Request Tab
It will open user interface as shown below


There are several section defined in above image. lets see them one by one
  • Index: Index on which you want to perform action, it is located just after localhost:9200/
  • Type: Type within selected Index, it will come after Index value.
  • Action: which type of action you want to perform like search, mapping, etc.
  • Action Method: it is REST API action methods like POST, GET, PUT, DELETE.

Following is record or document structure that we are going to create.

{
   name : String,
   description: String,
   profile_image : String,
   birthdate : Date,
   address: String
}

Create Mapping:

First new need to define structure of data, by default it would be string for all fields but we can set different data type as date, geo point, etc.

To define mapping, select Index and Type, then type _mapping in Action area to perform mapping related tasks, then select Action Method as POST and type following mapping structure and click on Request button. 

{
  "table": {
   "properties":{
      "name": {"type" : "string"},
      "description": {"type" : "string"},
      "profile_image":{"type" : "string"},
      "birthdate":{"type" : "date"},
      "address":{"type" : "string"}
    }
  }
}

To ensure mapping is created , just change Action Method to GET and click Request button.



Create Record:

To create record on selected Index and Type, make Action  value empty 
and Action Method as POST after that add following JSON record.

{
  "name": "Zainul",
  "description" : "Software Developer",
  "birthdate": "1985-03-13T00:00:00",
  "profile_image" : "image1.png"
}

And click on Request button. It will index the one record.




Read Indexed Records:

To fetch indexed records from Elasticsearch, select Index and Type then Action as _search and Action Method as POST , make query body empty then click on Request button.
It will fetch 10 records from selected Index and Type.





Update Records:


To update value of name field, select Index and Type then Action as _update and Action Method as PUT and add following JSON record in query section then click on Request button.

{
 "script": "ctx._source.name=\"Zainul Abedin \""
}



to check record is updated or not, fire search query.


Delete Records:

To delete record from selected Index and Type, make Action value empty and select Action Method as DELETE and click on Request button.



This is a brief introduction to CRUD operations. in next blog post you will see how to access Elasticsearch via Java Client.

Comments

Subscribe for latest tutorial updates

* indicates required

Popular posts from this blog

How to enable SQL logs in Spring Boot application?

This tutorial will demonstrate how to enable and disable SQL log statements in a Spring Boot application in order to debug SQL flow. Problem You will frequently need to debug the SQL statement while developing a Spring Boot application with a SQL database. SQL debug logs can assist you figure out what's wrong with JPA statements and whether or not there's a problem with database connectivity. Example  If you've built custom Spring data JPA methods and need to know what SQL statement is being utilized behind them, return repository . findByUsernameIn ( usernames ); Then you can enable Hibernet debug mode to log SQL statements. Solution Update the application.yml file with the Hibernet configuration as logging: level: org: hibernate: DEBUG or application.properties as logging.level.org.hibernate=DEBUG The SQL statement will appear in the application logs after modifying the configuration file and restarting the application. 2022-04-07 08:41...

Preload Images Using Javascript

Preload Image is technique which helps browser to render images with minimum delay. Today we will see example of image gallery. Gallery contain 5 images which load images one after another whenever user clicks next and back button. This is a basic image gallery scenario which is used in all possible website, then why we need preloaded images for this gallery? Now a day’s most of website becoming faster and user expectation is increasing. Suppose your website doesn’t use preload image technique for gallery and a user visits any image gallery from Google Plus or Facebook and visits your website gallery. Then that user always prefer those websites rather than yours. Why? Because your website load one image at a time. When user click on next button, then only gallery load image and user has wait till it get loaded. To avoid this situation gallery needs to download all images ...

JSON Tutorials : Getting Started

JSON is widely accepted text formatted structured data. JSON stands for " JavaScript Object Notation ". In general JSON can represent 1. Object of database record. 2. Object to represent a list of HTML elements. 3. Result of search query. 4. Response of an Ajax call. Here you can see JSON is used in many different areas and for many different scenarios. This means it has simple data structure. most of programming languages adopt it and it can flow easily from one connection to another. You can find JSON office definition here JSON Official Site . JSON is represented by two structural types, which includes two primitive types. Structural types Array : A sequential list of primitive data types between square brackets [ ] Object : Collection of key, value pair stored inside curly braces { }, where value would be primitive data type Primitive types : There are two primitive types key and value. " key " should be string and " value (data type)...