Play Framework Module: Elastic Search - Distributed Searching With JSON, HTTP, REST or Java

Searching is hard, just ask Google! Thankfully Lucene came about to provide incredible searching capabilities to the Java community. I have personally been using Lucene since 1999 and I am a huge fan, probably my very favorite Java project ever.

Lucene provides some great functionality but you still have a lot of work to do to integrate your app into Lucene; you need to map your models into a Lucene document, you need to write an indexer, you need to manage your indexes, cluster them, keep them in sync, write the search interface, etc. That’s a lot of work! To solve this problem there was a generation of frameworks built on top of Lucene, the most popular ones were Solr and Compass.

Compass and Solr are great frameworks, I have used them in production for very large deployments. I have built Foreclosure.com and Fannie Mae’s HomePath.com, Zillow.com’s Foreclosure Channel and an internal application to support HUD’s broker network for Florida, New York, New Jersey and Georgia entirely with that technology. There are still problems, it’s hard to cluster it, maintain real-time updates, backups, not have a single point of failure meaning failover which brings us to the next generation.

We finally come to Elastic Search from the awesome guys responsible for Compass! Elastic Search is the next generation of Compass, it’s a really great searching framework addressing a lot of the things that are so hard to deal with. It’s distributed, it supports Geo Spatial queries such bounding box searches, radius searches (around me type of search), backups on a slower data storages such as EC2, schema mapping, REST/JSON interface; like I said it’s really great stuff!

So I decided to build a Play! Framework module to integrate Play! apps with Elastic Search.

Documentation is available on a PDF format.

A screencast is available here.

A demo application is available here.

Installation

play install elasticsearch

Enable the module

After installing the module, add the following to your application.conf to enable it:

module.elasticsearch=${play.path}/modules/elasticsearch-0.0.6

Configure the Module

You need to configure the module by setting these properties in your application.conf. There are two ways to run your Play! app with Elastic Search, local mode or client mode. Local mode works well for development purposes, an Elastic Search instance will run on the same JVM as your Play! application automatically. You won’t need to setup another service, etc. The second option is client mode which fits better in a production environment. The default option is local mode.

Option 1) Elastic Search (Local Model)

elasticsearch.local=true

Option 2) Elastic Search (Client Model)

elasticsearch.local=false

elasticsearch.client=mynode1:9200,mynode2:9200

Usage

You basically need to add annotation “@ElasticSearchable” to your Model class. It only works for JPA so far.

Example:

<br></br>  
@ElasticSearchable<br></br>
@Entity<br></br>
public class Post extends Model {```

`public String title;`

`public Date postedAt;`

 

` }<br></br>`

 

**Searching **




import static org.elasticsearch.index.query.xcontent.FilterBuilders.;


import static org.elasticsearch.index.query.xcontent.QueryBuilders.
;```

``

org.elasticsearch.client.Client client = ElasticSearch.client();<br></br> SearchResponse response = client.prepareSearch("test")<br></br>
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)<br></br> .setQuery(termQuery("multi", "test"))<br></br> .setFrom(0).setSize(60).setExplain(true))<br></br> .execute()<br></br> .actionGet();<br></br>

See Elastic Search for more information.

Step-By-Step Tutorial Available

Step by Step tutorial is available here.

*User Interface *

After you start your application (play run), you should have an admin interface automatically running on http://localhost:9000/es-admin/.

Source Code

Fork it on Github https://github.com/feliperazeek/playframework-elasticsearch.

Credits

  1. Shay Banon for the great work with Elastic Search and Compass, I have been following his work for a few years, great stuff.
  2. Ben Birch (https://github.com/mobz) for the work on the User Interface.