Dogukan Sonmez

Currently in Munich, Germany

Java Groovy Python

No-Sql TDD Scrum

Django Shell Cloud

Sept 24 2012

JEST - Java HTTP Rest client for ElasticSearch

In this post you will find information about ElasticSearch, Searchbox.io and Jest.

I am not going to rewrite existing information; instead I will link some resources to deep in for each topic.

ElasticSearch

It is an open source search server developed by Shay Banon author of Compass.

It has really cool features and very easy to start and develop. Check out following links for more information.

http://www.elasticsearch.org/

http://www.elasticsearch.com/products/elasticsearch/

SearchBox.io

Like its name it is a search box for your application. It is fastest way to search your documents. Visit following page to get more information.

https://searchbox.io/

JEST

It is a Java based REST API to communicate ElasticSearch server via HTTP.

It is an open source project hosted on Sonatype

As previous coolest tools it is also very easy to use.

Let’s assume you have a java based project and you want to use ElasticSearch because you want to index your data and you want to make it simple and easy.

To start add following dependency to your pom.xml


<dependency>
  <groupId>io.searchbox</groupId>
  <artifactId>jest</artifactId>
  <version>0.0.1</version>
</dependency>

Add Sonatype repository definition to your root pom.xml


<repositories>
.
.
 <repository>
   <id>sonatype</id>
   <name>Sonatype Groups</name>
   <url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
.
.
</repositories>

To start get an instance of Jest.


JestClient client = new JestClientFactory().getObject();

Then let’s create an index (it is like a ‘database’ in a relational database.)

There is couple of ways to create index via Jest. Let’s assume you want to create index from configuration file

configuration file:


{"settings" : {
        "number_of_shards" : 1
},
"mappings" : {
        "type1" : {
            "_source" : { "enabled" : false },
            "properties" : {
                "field1" : { "type" : "string", "index" : "not_analyzed" }
            }
        }
}}


client.execute(new CreateIndex("indexName", "/config/elasticsearch-simple.json"));

After that it’s up to your needs.

If you want to add document to your index Then


Index index = new Index.Builder(source).index("indexName ").type("typeName").build();

client.execute(index);

type is a like table in relational database

check out elasticsearch glassory http://www.elasticsearch.org/guide/appendix/glossary.html

source might be your domain object , a map, or a json string

search your data


JestResult result = client.execute(new Search(query))

List list = result.getSourceAsObjectList(DomainObject.class);

Get a document

Get get = new Get.Builder("1").index("indexName").type("typeName").build();

JestResult result = client.execute(get);

DomainObject myObject = result.getSourceAsObject(DomainObject.class);

Delete a document


client.execute(new Delete.Builder("1").index("indexName ").type("typeName ").build());

For more information about Jest visit https://github.com/searchbox-io/Jest

Furthermore check out following example projects.

https://github.com/searchbox-io/java-jest-sample

https://github.com/dogukansonmez/CVBANK