Day1 ElasticSearch: install + setup

Arthur Lee
3 min readJan 18, 2019

--

Recently I am going to involve in elastic search team.

I am very excited about that but I do not have experiences for that.
As a result, I decide to learn how to build it by myself even we already have the whole project but building a simple project from scratch let me learn much more!

First Download ELASTICSEARCH

I would download that from docker!
Docker is very great tool to setup all environment. In this way, we don’t have to take care too much!

docker pull docker.elastic.co/elasticsearch/elasticsearch:6.5.4

launch the Elastic Search

Now, we already launch the docker on 9200 port.

docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.5.4

We can verify that with on the chrome:

type: http://localhost:9200/, you will see

Also, we can type command in the terminal:

curl http://localhost:9200/_nodes/http?pretty

Connect to Elastic Search

Here, I try Java code because my team using Java, you can use any language you like.

Creating a Java gradle project

you also can create a maven project. Either is fine.

Import dependency from <build.gradle>

adding the following libraries in <dependencies>

compile group: 'org.elasticsearch', name: 'elasticsearch', version: '6.5.4'compile group: 'org.elasticsearch.client', name: 'transport', version: '6.5.4'

the result will be here.

Writing a simple test case

First, we have to create a client.

Notice: in different version, the code would be different because the class is different, please make sure your ES version and google it!

client = new PreBuiltTransportClient(
Settings
.builder().put("client.transport.sniff", false)
.put("cluster.name","docker-cluster").build())
.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));

cluster.name is the <cluster_name> you see in <http://localhost:9200/> or in terminal. It is important because I failed several times without typing cluster.name

creating a example object to store into Elastic Search

XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("fullName", "Test")
.field("dateOfBirth", new Date())
.field("age", "10")
.endObject();

And then, building the index! (store the data into ES)

IndexResponse response = client.prepareIndex("member", "Doe")
.setSource(builderObject).get();

In the end, please not forget the close the connection.

Check the data available

We can use postman with chrome web store to check the data!

GET http://localhost:9200/member/_search

You will find the result

Notice: when we restart our computer and turn on again, the data will disappear because the data in ES is in memory.

client.close()

This is a simple example for Elastic Search. In the next, we will explore more on query!

--

--

Arthur Lee
Arthur Lee

Written by Arthur Lee

An machine learning engineer in Bay Area in the United States

No responses yet