Community contributed extensions

Play Framework – Elastic Search Module

by Felipe Oliveira


Demo Source Code

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 and Fannie Mae’s,’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!

Elastic Search is a Distributed Search Solution based on Apache Lucene.

The main advantages are:

Integrate Elastic Search in a Play! Framework Application. This module uses JPA events to notify Elastic Search of events of their own. In Local Mode it embeds a running Elastic Search instance (port 9200 by default), a good choice for development. In Client Mode it connects to an external instance of Elastic Search, that might be your setup in production.


Play! 1.2

Install the module

Install the elasticsearch module from the modules repository:

play install elasticsearch

Enable the module

After installing the module, add the following to your conf/dependencies.yml to enable it (don’t forget to run play dependencies):

	- play -> elasticsearch 0.2

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)
# Option 2) Elastic Search (Client Model)



You basically need to add annotation “@ElasticSearchable” to your Model class. It only works for JPA so far. If a model has that annotation our module we’ll watching for JPA events for instances of that class and route messages to Elastic Search to make sure Elastic Search index has the latest information available on the database.


public class Post extends Model {
    public String title;
    public Date postedAt;

It’s possible to prevent fields from being included in the index by marking them with @ElasticSearchIgnore.

By default an index name is generated for you, however you can override this by setting the indexName field:



Since 0.0.9 it’s possible to embed properties by using “@ElasticSearchEmbedded”.

class Institution {
	public String name;
	public String type;
class Education {
	Institution institution;

This will add and to education.
By default all fields are embedded, which can be overriden by specifying the fields to embed:

class Education {
	Institution institution;


Simple Searching

SearchResults<Post> list ="title", "what a search"), Post.class);
See "Elastic Search documentation": for more example.


The biggest change on this release is a nicer search interface. We are providing a very simple way to get started, inspired by Play!'s CRUD module.
Basically you need to create a controller class and extend ElasticSearchController. Use annotation @ElasticSearchController.For(YOURMODELCLASS.class) to tell our module what model you want to search on, here’s an example:

public class ElasticSearchExample extends ElasticSearchController {


You should be able to search on http://localhost:9000/elasticSearchExample/index. If you want to customize the views, just create a directory ELASTIC_SEARCH under views and change whatever you need to change.

Custom search

After you’ve created your XContentQueryBuilder object, you can search in two ways:

1) Call
2) Call ElasticSearch.query() and subsequently set query parameters (e.g. paging)

If you are searching for JPA entities and need referenced entities to be available (e.g. @ManyToOne or @OneToMany relationships), use


or call hydrate(true) on your Query:

Query query = ElasticSearch.query(...);

This will take care of hydrating your entities so proxies and collections work.

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




Version 0.2

Version 0.0.9

Version 0.0.8