Community contributed extensions

Search module

The Search module allows you to have basic full text search functionalities to your JPA Model. It is based on Lucene, and requires a real file system to store its indexes.

Version

Search module versions 1.x require playframework version 1.0.x. If you use playframework 1.1.x, use search-head or search-2.x.

Enable the module

In the /conf/application.conf file, enable the Search module by adding this line:

# The search module
module.search=${play.path}/modules/search

Indexing your objects

Use the @Indexed annotation to mark your Model, and then use the @Field to mark the fields to be indexed.

@Indexed
public class Folder extends Model {
    @Field
    @Column(unique=true)
    public Integer poseidonNumber; 
    @Field
    public String object;

The @Field annotation currently supports only primitive types.

Search the objects

Use the Search helper to build your queries:

Search.search("object:dogs", Folder.class)

The first parameter is a lucene query and the second is your Model class. You may want to refer to the Lucene query documentation at this point, knowing that the Search maintains a separated index per class and adds the properties marked with @Field as a Lucene Field.

The Search.search returns a query object that you can tweak:

Query q = Search.search("object:dogs", Folder.class);
q.orderBy("object")
    .page(2,5)
    .reverse();
    

To finish your query, if you wish to retrieve your Model objects, use

List<Folder> folders = q.fetch();
    

or to get only ids (to use in a JPA query by example...):

List<Long> folderIds = q.fetchIds();
    

To get full informations (like relevance), you would use:

List<QueryResult> results = q.executeQuery();
    

Maintaining the indexes

Each time you create, update or delete your Model objects, the corresponding index is automatically updated.

Should you need to re-index your objects (like if you have manually modified your database), you will reboot your application with:

play.search.reindex=enabled

or

play.search.reindex=true

In your conf/application.conf. Don’t forget to remove it after!

Since version 1.1, the create/update/delete of a JPA object is synchronous. It means that once you made a change to an object, the corresponding reader will automaticly be re-opened to reflect the changes. This behaviour should be ok most of the time, but should you want to increase performances, like for large updates, you could use the property

play.search.sync=false

To have the auto re-opening suspended. Use Search.dirtyReader with a className to re-open when you’re done with your massive updates.

Misc configuration

You can use the following properties in your conf/application.conf file:

play.search.path=/tmp/myDevApplication
play.search.analyser=org.apache.lucene.analysis.standard.StandardAnalyzer

play.search.path is where the module stores it’s indexes
play.search.analyser is the lucene analyzer class used for indexation.