Hi
I've knocked a small example for you to show how the classbridge might work for your usecase. The following shows the bridge class
Code:
public class ExposeDateOfBirthClassBridge implements FieldBridge {
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
Author user = (Author)value;
if (user.isExposeDateOfBirth()) {
Field dateOfBirth = new Field("authors.dateOfBirth", DateTools.dateToString(user.getDateOfBirth(), DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED);
document.add(dateOfBirth);
}
}
}
And it is applied on the author class like this:
Code:
@Entity
@Table(name = "AUTHOR")
@ClassBridge( impl = ExposeDateOfBirthClassBridge.class)
public class Author implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Long id;
@Field(index = Index.TOKENIZED, store = Store.NO)
@Column(name = "F_NAME")
private String firstName;
@Field(index = Index.TOKENIZED, store = Store.NO)
@Column(name = "M_NAME")
private String middleName;
@Field(index = Index.TOKENIZED, store = Store.NO)
@Column(name = "L_NAME")
private String lastName;
@Column(name = "DOB")
private Date dateOfBirth;
@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
mappedBy = "authors"
)
@ContainedIn
private List<Book> publications;
@Column(name = "EXPOSE_DOB")
private boolean exposeDateOfBirth = false;
If you set the exposeDateOfBirth to true then it is indexed whereas false doesn't. I've tested this and verified it works. The thing to note is that I haven't added @Field to the dateOfBirth field, as it looks as though this field is indexed despite the classbridge. Anyway this should give you an idea. Let me know if this works for you.
Thanks