Archive for September 6th, 2006

Dynamic Meta Data in Compass

Wednesday, September 6th, 2006

Dynamic meta data feature in the latest Compass build (1.1 M2 SNAPSHOT) allows to define syntactic meta-data definition using dynamic/expression language meta data values. There are many use cases for using dynamic meta-data, usually revolving around using Resources for displaying search results, where a property name used for displaying results is the combination of several properties or objects within the object tree.

If for example an application would like to iterate through a Resource based search results and use a single meta-data name (property name) called “display” in order to show it to the user, “display” can be a dynamic meta-data constructed out of several other Object properties.

There are many libraries/languages out there that can evaluate a dynamic expression, Compass comes with built in support for jakarta commons el/jexl, velocity, ognl, and groovy. If we use the following class:

public class A {

    private Long id;

    private String value;

    private String value2;

    private Date date;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getValue2() {
        return value2;
    }

    public void setValue2(String value2) {
        this.value2 = value2;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

We can use the following mappings definition (we use the same mapping for several different types of dynamic expression):

<compass-core-mapping package=“org.compass.core.test.dynamic.el”>

    <class name=“A” alias=“a1″>

        <id name=“id”/>

        <dynamic-meta-data name=“test” converter=“el”>
            ${data.value}${data.value2}
        </dynamic-meta-data>

        <dynamic-meta-data name=“test” converter=“groovy”>
            return data.value + data.value2
        </dynamic-meta-data>

        <dynamic-meta-data name=“test” converter=“ognl”>
            data.value + data.value2
        </dynamic-meta-data>
    </class>
</compass-core-mapping>

Compass will set under the “data” key the instance of the class the mappings refer to. Note, that dynamic meta data is used only for marshalling (the process of persisting an Object to the search engine).

What other scripting/expression language libraries do you think Compass should support?