Play Framework – Elastic Search Module
by Felipe Oliveira
http://mashup.fm(http://mashup.fm)
http://geeks.aretotally.in(http://geeks.aretotally.in)
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 9002 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.
Prerequisites
Play! 1.1
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 application.conf to enable it:
module.elasticsearch=${play.path}/modules/elasticsearch-0.0.4
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:9002,mynode2:9002@
Usage
You basically need to add annotation “@ElasticSearchable” to your Model class. It only works for JPA so far.
Example:
@ElasticSearchable
@Entity
public class Post extends Model {
public String title;
public Date postedAt;
}
Searching
import static org.elasticsearch.index.query.xcontent.FilterBuilders.*;
import static org.elasticsearch.index.query.xcontent.QueryBuilders.*;
org.elasticsearch.client.Client client = ElasticSearch.client();
SearchResponse response = client.prepareSearch(“test”)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(termQuery(“multi”, “test”))
.setFrom(0).setSize(60).setExplain(true))
.execute()
.actionGet();
See Elastic Search for more information.
User Interface
After you start your application (play run), you should have an admin interface automatically running on http://localhost:9000/es-admin/(http://localhost:9000/es-admin/).
Source Code
Fork it on Github https://github.com/feliperazeek/playframework-elasticsearch(https://github.com/feliperazeek/playframework-elasticsearch).
Roadmap
- Add different methods of notification (ElasticSearch River, AMQP, JMS)
- Adding support for non-Jpa models.
- Parent/Child support
- Customizations for mapping, etc
- Sample Search Page
Credits
- Shay Banon for the great work with Elastic Search and Compass, I have been following his work for a few years, great stuff.
- Ben Birch (https://github.com/mobz(https://github.com/mobz)) for the work on the User Interface.