@Searchable and Compass

2006 May 1
tags:
by Shay Banon

One of the nicest features in the new Compass version is support for annotations. Making your domain model searchable could not be simpler now if you are working with Java 5. Just how simple it is? Here is what you need to do:

First, let us assume that you have a class called Book. The book has a title, author and content. Here is the class with Compass searchable annotations:

@Searchable
public class Book {

    @SearchableId
    private Long id;

    @SearchableProperty
    private String title;

    @SearchableProperty
    private String author;

    @SearchableProperty
    private String content;

    // rest of class
}

When designing annotations support in Compass, one of the main goals was not to create annotations clutter. As you can see, the annotations have sensible defaults, and try to be short and to the point.

Now, adding the class to Compass could not be simpler. You can either use the xml based configuration file:

<compass name="default">

    <connection>
        <file path="target/test-index" />
    </connection>

    <mappings>
        <class name="Book" />
    </mappings>
</compass>

Or, add it programmatically to Compass Configuration:

CompassConfiguration conf = new CompassAnnotationsConfiguration()
        .addClass(Book.class);

That is it. You can now use the usual Compass API in order to use the Book object.

7 Responses leave one →
  1. 2006 May 1

    How does this work with the Hibernate integration? If I annotate my domain classes, can Compass query Hibernate for the mapped classes and inspect them for annotations to automatically register them as searchable?

  2. 2006 May 1

    …adding another comment to make sure I get emails…

  3. 2006 May 1

    The hibernate integration works exactly as it was with the xml OSEM definitions. You add your searchable classes to Compass, add your ORM enabled classes to Hibernate (which might not be a one to one mapping) and use Compass Hibernate Gps Device (initialized with Hibernate Session Factory) to integrate Compass with Hibernate.

    The annotations are just another way of configuring your OSEM definitions.

    You do raise an interesting feature with Compass, where by inspecting Hibernate session factory, Compass can automatically add all the ORM enabled classes that have OSEM definition (xml or annotations). If this feature is of interest to you, just add it to Jira, it should not be difficult to implement.

  4. 2006 May 7

    Hi,

    which version of the JPA API is Compass supposed to work with ?
    In fact, when trying the JpaGpsDevice to index my data, I get a NoSuchMethodError, which would typically be caused by a bad version…

    I am using Hibernate entity manager v3.1beta7 with the JPA v3.0-public_review API, and can’t find what compass requires ;)

    Thanks a lot,
    Sami Dalouche

    Exception in thread “main” java.lang.NoSuchMethodError: javax.persistence.EntityManagerFactory.getEntityManager()Ljavax/persistence/EntityManager;
    at org.compass.gps.device.jpa.DefaultEntityManagerWrapper.doGetEntityManager(DefaultEntityManagerWrapper.java:21)
    at org.compass.gps.device.jpa.AbstractEntityManagerWrapper.open(AbstractEntityManagerWrapper.java:40)
    at org.compass.gps.device.jpa.JpaGpsDevice.doIndex(JpaGpsDevice.java:168)
    at org.compass.gps.device.AbstractGpsDevice$1.doInCompassWithoutResult(AbstractGpsDevice.java:89)
    at org.compass.core.CompassCallbackWithoutResult.doInCompass(CompassCallbackWithoutResult.java:29)
    at org.compass.core.CompassTemplate.execute(CompassTemplate.java:133)
    at org.compass.gps.impl.SingleCompassGps.executeForIndex(SingleCompassGps.java:134)
    at org.compass.gps.device.AbstractGpsDevice.index(AbstractGpsDevice.java:87)
    at org.compass.gps.impl.SingleCompassGps$1.buildIndexIfNeeded(SingleCompassGps.java:114)
    at org.compass.core.lucene.engine.manager.DefaultLuceneSearchEngineIndexManager$1.firstStep(DefaultLuceneSearchEngineIndexManager.java:167)
    at org.compass.core.lucene.engine.manager.DefaultLuceneSearchEngineIndexManager.operate(DefaultLuceneSearchEngineIndexManager.java:124)
    at org.compass.core.lucene.engine.manager.DefaultLuceneSearchEngineIndexManager.replaceIndex(DefaultLuceneSearchEngineIndexManager.java:165)
    at org.compass.core.impl.DefaultCompass$TransactionalSearchEngineIndexManager$6.doInCompassWithoutResult(DefaultCompass.java:305)
    at org.compass.core.CompassCallbackWithoutResult.doInCompass(CompassCallbackWithoutResult.java:29)
    at org.compass.core.CompassTemplate.execute(CompassTemplate.java:133)
    at org.compass.core.CompassTemplate.execute(CompassTemplate.java:116)
    at org.compass.core.impl.DefaultCompass$TransactionalSearchEngineIndexManager.replaceIndex(DefaultCompass.java:303)
    at org.compass.core.lucene.engine.manager.ScheduledLuceneSearchEngineIndexManager.replaceIndex(ScheduledLuceneSearchEngineIndexManager.java:106)
    at org.compass.gps.impl.SingleCompassGps.doIndex(SingleCompassGps.java:109)
    at org.compass.gps.impl.AbstractCompassGps.index(AbstractCompassGps.java:102)
    at funala.ImportInitialData.main(ImportInitialData.java:78)

  5. 2006 May 7

    Sorry, I am using the final draft version, the one that hibernate entity manager v3.1beta7 ships with

  6. 2006 May 7

    Sorry for spamming, this is my last message ;-)

    readme.txt actually says :
    * jpa/persistence.jar
    - Java Persistence API (Publid Draft Version)
    - required for building compass

    According to public draft version, an entity manager is created using createEntityManger.
    however, :
    javax.persistence.EntityManagerFactory.getEntityManager()Ljavax/persistence/EntityManager;
    at org.compass.gps.device.jpa.DefaultEntityManagerWrapper.doGetEntityManager(DefaultEntityManagerWrapper.java:21)
    at

    gives the impression that the getENtityManager method is used instead..;

    in the java source code: DefaultEntityManagerWrapper.java :
    EntityManager entityManager;
    try {
    entityManager = entityManagerFactory.getEntityManager();
    isNew = false;
    if (log.isDebugEnabled()) {

    should I file a JIRA issue ?

  7. 2006 May 8
    Shay Banon permalink

    I saw that you filed a Jira Issue already :). I will have a look at this. The best place to ask questions about this is the forum…

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS