Initially I need to order by the number of children.
I.e.
TParent contains a Set<TChild> childSet
I need to list TParent's ordered by the childSet.size().
I popped the question here: http://stackoverflow.com/questions/18827149/hibernate-search-order-by-child-count
Don Roby answered suggesting a bridge.
This question is a duplicate of my follow up question here: http://stackoverflow.com/questions/18847238/hibernate-search-custom-bridge-value-not-updating
(I'll populate answers when I have them).
I have a custom bridge. When I insert an entity, the value derived from the custom bridge doesn't appear to be getting updated in the index.
Code:
@Indexed
@Entity
public class TParent implements java.io.Serializable {
.....
private Set<TChild> TChildSet = new HashSet<TChild>(0);
@ContainedIn
@FieldBridge(impl = CollectionCountBridge.class)
@Field(analyze = Analyze.NO)
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="TParent")
public Set<TChild> getTChildSet() {
return this.TChildSet;
}
and
Code:
@Indexed
@Entity
public class TChild implements java.io.Serializable {
.....
private TParent TParent;
@IndexedEmbedded
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="parent_id", nullable=false)
public TParent getTParent() {
return this.TParent;
}
And the custom bridge
Code:
public class CollectionCountBridge extends IntegerBridge {
@Override
public String objectToString(Object object) {
if (object == null || (!(object instanceof Collection))) {
return null;
}
Collection<?> coll = (Collection<?>) object;
int size = coll.size();
System.out.println("col.size(): " + size);
return super.objectToString(size);
}
}
I'm listing TParents. I'm trying to order them by the TChildSet count. It works perfectly when I build the index first. I can list TParent's and order them by the by the number of TChild's.
Code:
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, TParent.class);
fullTextQuery.setSort(new Sort(new SortField("TChildSet", SortField.INT, true)));
Now...
When I add a new TChild, the CustomBridge code is executed. I'd expect to see the ordering change as I add TChild's.
I can see that the TChild count is increased (as per the System.out.println() in the CustomBridge code).
However, the query does not order them correctly. The original sort order from when the index was first built remains. Adding new TChild's has no effect on the sort order. I guess the index is not being updated but I'm not sure.
Any help or pointers would be great.
Thanks Very Much
John