Elasticsearch Cloud + Java REST Client Fuzzy Search


The new Elasticsearch JAVA REST client is here.

Let’s get right into it!

The Elastisearch JAVA REST client doesn’t seem to lend itself to dot notation a much as I’d prefer.

examples of dot notation:




I had to construct much of the search request using a handmade JSON string. I made use of the Apache HttpEntity to pass the constructed JSON to Elasticsearch. Since I took this approach, let’s look at how to first create the pure JSON query.

In the screenshot below, we have the Kibana search screen on the left, and on the right is the response. We perform a search by first and last name. Because we are using the fuzziness option we don’t have to properly spell the names. Below we left out a letter in the first name, and yet we are able to find a matching record, which you see on the right side of the image.


Now this next part is not difficult, it’s just a bit annoying. We need to construct this request string in Java. Our goal is a query to find a person named “Sean Wu”. Notice how we butcher the first name passed in and we are still able to locate the record, thanks to fuzzy matching!

String encodedBytes = Base64.getEncoder().encodeToString(“username:password”.getBytes());

Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, “application/json”), new BasicHeader(“Authorization”, “Basic ” + encodedBytes) };

RestClient r = RestClient .builder(new HttpHost(“url_to_elastic_cloud_endpoint”, 9243, “https”)) .setDefaultHeaders(headers).build();


String firstname =”San”;

String lastname=”Wu”;

HttpEntity entity = new NStringEntity(


“\”query\”: {“+

” \”bool\”: {“+

“\”must\”: [“+

“{“+          “\”match\”: {“+

“\”firstname\”: {“+

“\”query\”: \””+


“\”fuzziness\”: \”AUTO\””+


“}”+        “},”+


“\”match\”: {“+

“\”lastname\”: {“+

“\”query\”: \””+


“\”fuzziness\”: \”AUTO\””+









Map<String, String> paramMap = new HashMap<String, String>();

paramMap.put(“pretty”, “true”);

Response response = r.performRequest(“GET”, “/*/_search”, paramMap,entity);



System.out.println(“Host -” + response.getHost());

System.out.println(“RequestLine -” + response.getRequestLine()); System.out.println(response.toString());


try {


} catch (IOException e) {




There you have it!

Java on Linux with multiple JDKs (Old, 2010)

Recently one of our linux servers at work crashed and died. I decided this time to skip the packages that come with the system and just come up with a more flexible way of compiling and running Java code.

May as well have multiple versions of Java. At the time I was coding in 1.5 so I needed a jdk5. But as I moved into the later testing phases of an application, I wanted to try some of the applications monitoring features in Java 6. So, to cover multiple bases, multiple JDKs.

First you want to download, I assume for all of us here, Java 5 and Java 6, jdk, sdk, the whole kit and caboodle!

Since the is all about options I like to:
1. Install the different versions of Java in /opt at the root level. You should be able to do this by typing ./java-file-you-downloaded.bin

2. Write a shell script that will set the Java version and location at run time. Just add, if using bash shell, export JAVA_HOME=/opt/jdkversion, and other path and classpath exports you need.

Using bash shell:

#! /bin/bash

export JAVA_HOME=/opt/jdk1.5.0_18
export PATH=$JAVA_HOME/bin:$PATH

3. Add to the shell script a call to your Java class.
java -classpath .:./moreDir:./anotherDir package.javaclass.class or no .class depending on your jdk

Now you can write shell scripts to compile, and run your code with different versions of Java just by creating a shell script!

The Beauty of Java Hashtable (Old, 2010)

Sometimes newbies can’t quite grasp how to use Java to get data from a database in an easy way.
A simple hashtable goes a long way in web development. for instance:

String sql = “select name,age,dob from employee”;

//notice the use of String key and Object value, since data from the database comes in many forms:Decimal,varchar,boolean
Hashtable<String,Object><string,object><string,object><string,object> employees = new Hashtable<string,object><string,object><string,object><String,Object><string,object>();

int count 0;

ResultSet rs = statement.executeQuery();

This simple routine will give the novice developer a new weapon to his her arsenal.
Here we have a generic way store database data in an array.
If the data was all varchar, which is Java String, we could have created a hashtable with a signature of <String,String><string,string>. However, <String,Object>String,Object> gives the flexibility we need.

Don’t forget to increment that counter!

Catch The Cloud (Old, 2010)

Cloud Computing is slowly establishing itself as the backbone of large corporations, startup companies and independents all alike. The timeline of online technology as it descended into the dotcom bomb and arose from the ashes like a phoenix is a classic store of nothing to something.

In the beginning there was the Internet with new websites popping up daily. Can you recall all the early ‘Social Media’ sites, like ubo.net? It had everything I wanted but at the same time nothing. There were links to things I knew people like me were interested; the local event, restaurants, music, etc… The problem with these companies is that there was no true value in their web presence. Think about having a staff of information technology specialists, marketing department and the whole nine yards, but you have no true stream of revenue. That is what many companies faced in the mid to late 90s. No bottom line, hence, the dotcom bomb destroyed companies these companies.

Fast-forward to 2010 and businesses have a better understanding of technology and how it does more than act as a showpiece. In the financial services industry we have Cloud Computing serving as virtual work staff. Salesforce.com, one of, if not the largest, Customer Relationship Management (CRM) platform and has grown to over a billion dollars in assets in less than 10 years reaching into all facets of business like Lead Management for E-Trade Financial and global collaboration with business partners for Dell Computers. Cloud Computing allows smaller entities to live a better existence in their industry with Cloud based services. The cost of Cloud based services is dwarfed when compared to keeping an in house staff of information technology professionals. This is news golden for startups and independents.