Red Hat Training

A Red Hat training course is available for Red Hat JBoss Enterprise Application Platform

14.2.4.2.6. ClassBridge

特定のエンティティーの複数のプロパティーを組み合わせて、これを特定の方法で Lucene インデックスにインデックスを設定すると便利です。@ClassBridge および @ClassBridges アノテーションは、プロパティーレベルではなくクラスレベルで定義できます。この場合、カスタムフィールドブリッジ実装は、特定のプロパティーの代わりに、エンティティーインスタンスを値のパラメーターとして受信します。に表示されていませんが例14.29「クラスブリッジの実装」@ClassBridgetermVectorセクションで説明されている属性「基本的なマッピング」

例14.29 クラスブリッジの実装

@Entity
@Indexed
@ClassBridge(name="branchnetwork",
             store=Store.YES,
             impl = CatFieldsClassBridge.class,
             params = @Parameter( name="sepChar", value=" " ) )
public class Department {
    private int id;
    private String network;
    private String branchHead;
    private String branch;
    private Integer maxEmployees
    ...
}

public class CatFieldsClassBridge implements FieldBridge, ParameterizedBridge {
    private String sepChar;

    public void setParameterValues(Map parameters) {
        this.sepChar = (String) parameters.get( "sepChar" );
    }

    public void set( String name, Object value, Document document, LuceneOptions luceneOptions) {
        // In this particular class the name of the new field was passed
        // from the name field of the ClassBridge Annotation. This is not
        // a requirement. It just works that way in this instance. The
        // actual name could be supplied by hard coding it below.
        Department dep = (Department) value;
        String fieldValue1 = dep.getBranch();
        if ( fieldValue1 == null ) {
            fieldValue1 = "";
        }
        String fieldValue2 = dep.getNetwork();
        if ( fieldValue2 == null ) {
            fieldValue2 = "";
        }
        String fieldValue = fieldValue1 + sepChar + fieldValue2;
        Field field = new Field( name, fieldValue, luceneOptions.getStore(),
            luceneOptions.getIndex(), luceneOptions.getTermVector() );
        field.setBoost( luceneOptions.getBoost() );
        document.add( field );
   }
}
この例では、特定の CatMissionClassBridgedepartment インスタンスに適用され、フィールドブリッジはブランチとネットワークの両方を連結し、この連結をインデックス化します。